jaxp/src/com/sun/org/apache/xalan/internal/lib/ExsltMath.java
author duke
Wed, 05 Jul 2017 19:04:46 +0200
changeset 18896 98b0babd0565
parent 12458 d601e4bba306
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: ExsltMath.java,v 1.1.2.1 2005/08/01 02:08:50 jeffsuttor Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xalan.internal.lib;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.xpath.internal.NodeSet;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import org.w3c.dom.Node;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import org.w3c.dom.NodeList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * This class contains EXSLT math extension functions.
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * It is accessed by specifying a namespace URI as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * <pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 *    xmlns:math="http://exslt.org/math"
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * </pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * The documentation for each function has been copied from the relevant
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * EXSLT Implementer page.
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * @see <a href="http://www.exslt.org/">EXSLT</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * @xsl.usage general
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
public class ExsltMath extends ExsltBase
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
  // Constants
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
  private static String PI = "3.1415926535897932384626433832795028841971693993751";
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
  private static String E  = "2.71828182845904523536028747135266249775724709369996";
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  private static String SQRRT2 = "1.41421356237309504880168872420969807856967187537694";
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
  private static String LN2 = "0.69314718055994530941723212145817656807550013436025";
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
  private static String LN10 = "2.302585092994046";
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
  private static String LOG2E = "1.4426950408889633";
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
  private static String SQRT1_2 = "0.7071067811865476";
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
   * The math:max function returns the maximum value of the nodes passed as the argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
   * The maximum value is defined as follows. The node set passed as an argument is sorted
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   * in descending order as it would be by xsl:sort with a data type of number. The maximum
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
   * is the result of converting the string value of the first node in this sorted list to
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
   * a number using the number function.
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
   * If the node set is empty, or if the result of converting the string values of any of the
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
   * nodes to a number is NaN, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
   * @param nl The NodeList for the node-set to be evaluated.
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   * @return the maximum value found, NaN if any node cannot be converted to a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   * @see <a href="http://www.exslt.org/">EXSLT</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
  public static double max (NodeList nl)
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    if (nl == null || nl.getLength() == 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
      return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    double m = - Double.MAX_VALUE;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    for (int i = 0; i < nl.getLength(); i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
      Node n = nl.item(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
      double d = toNumber(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
      if (Double.isNaN(d))
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
      else if (d > m)
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
        m = d;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    return m;
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
   * The math:min function returns the minimum value of the nodes passed as the argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
   * The minimum value is defined as follows. The node set passed as an argument is sorted
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
   * in ascending order as it would be by xsl:sort with a data type of number. The minimum
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
   * is the result of converting the string value of the first node in this sorted list to
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
   * a number using the number function.
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
   * If the node set is empty, or if the result of converting the string values of any of
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
   * the nodes to a number is NaN, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
   * @param nl The NodeList for the node-set to be evaluated.
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
   * @return the minimum value found, NaN if any node cannot be converted to a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
   * @see <a href="http://www.exslt.org/">EXSLT</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
  public static double min (NodeList nl)
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    if (nl == null || nl.getLength() == 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
      return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    double m = Double.MAX_VALUE;
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    for (int i = 0; i < nl.getLength(); i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
      Node n = nl.item(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
      double d = toNumber(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
      if (Double.isNaN(d))
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
      else if (d < m)
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        m = d;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
    return m;
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
   * The math:highest function returns the nodes in the node set whose value is the maximum
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
   * value for the node set. The maximum value for the node set is the same as the value as
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
   * calculated by math:max. A node has this maximum value if the result of converting its
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
   * string value to a number as if by the number function is equal to the maximum value,
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
   * where the equality comparison is defined as a numerical comparison using the = operator.
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
   * If any of the nodes in the node set has a non-numeric value, the math:max function will
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
   * return NaN. The definition numeric comparisons entails that NaN != NaN. Therefore if any
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
   * of the nodes in the node set has a non-numeric value, math:highest will return an empty
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
   * node set.
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
   * @param nl The NodeList for the node-set to be evaluated.
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
   * @return node-set with nodes containing the maximum value found, an empty node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
   * if any node cannot be converted to a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
  public static NodeList highest (NodeList nl)
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    double maxValue = max(nl);
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    NodeSet highNodes = new NodeSet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    highNodes.setShouldCacheNodes(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    if (Double.isNaN(maxValue))
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
      return highNodes;  // empty Nodeset
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    for (int i = 0; i < nl.getLength(); i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
      Node n = nl.item(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
      double d = toNumber(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
      if (d == maxValue)
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
        highNodes.addElement(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    return highNodes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
   * The math:lowest function returns the nodes in the node set whose value is the minimum value
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
   * for the node set. The minimum value for the node set is the same as the value as calculated
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
   * by math:min. A node has this minimum value if the result of converting its string value to
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
   * a number as if by the number function is equal to the minimum value, where the equality
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
   * comparison is defined as a numerical comparison using the = operator.
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
   * If any of the nodes in the node set has a non-numeric value, the math:min function will return
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
   * NaN. The definition numeric comparisons entails that NaN != NaN. Therefore if any of the nodes
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
   * in the node set has a non-numeric value, math:lowest will return an empty node set.
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
   * @param nl The NodeList for the node-set to be evaluated.
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
   * @return node-set with nodes containing the minimum value found, an empty node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
   * if any node cannot be converted to a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
  public static NodeList lowest (NodeList nl)
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
    double minValue = min(nl);
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    NodeSet lowNodes = new NodeSet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    lowNodes.setShouldCacheNodes(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    if (Double.isNaN(minValue))
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
      return lowNodes;  // empty Nodeset
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
    for (int i = 0; i < nl.getLength(); i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
      Node n = nl.item(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
      double d = toNumber(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
      if (d == minValue)
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
        lowNodes.addElement(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
    return lowNodes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
   * The math:abs function returns the absolute value of a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
   * @param num A number
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
   * @return The absolute value of the number
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
   public static double abs(double num)
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
     return Math.abs(num);
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
   * The math:acos function returns the arccosine value of a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
   * @param num A number
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
   * @return The arccosine value of the number
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
   public static double acos(double num)
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
     return Math.acos(num);
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
   * The math:asin function returns the arcsine value of a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
   * @param num A number
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
   * @return The arcsine value of the number
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
   public static double asin(double num)
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
     return Math.asin(num);
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
   * The math:atan function returns the arctangent value of a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
   * @param num A number
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
   * @return The arctangent value of the number
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
   public static double atan(double num)
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
     return Math.atan(num);
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
   * The math:atan2 function returns the angle ( in radians ) from the X axis to a point (y,x).
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
   * @param num1 The X axis value
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
   * @param num2 The Y axis value
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
   * @return The angle (in radians) from the X axis to a point (y,x)
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
   public static double atan2(double num1, double num2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
     return Math.atan2(num1, num2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
   * The math:cos function returns cosine of the passed argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
   * @param num A number
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
   * @return The cosine value of the number
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
   public static double cos(double num)
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
     return Math.cos(num);
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
   * The math:exp function returns e (the base of natural logarithms) raised to a power.
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
   * @param num A number
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
   * @return The value of e raised to the given power
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
   public static double exp(double num)
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
     return Math.exp(num);
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
   * The math:log function returns the natural logarithm of a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
   * @param num A number
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
   * @return The natural logarithm of the number
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
   public static double log(double num)
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
     return Math.log(num);
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
   * The math:power function returns the value of a base expression taken to a specified power.
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
   * @param num1 The base
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
   * @param num2 The power
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
   * @return The value of the base expression taken to the specified power
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
   public static double power(double num1, double num2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
     return Math.pow(num1, num2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
   * The math:random function returns a random number from 0 to 1.
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
   * @return A random double from 0 to 1
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
   public static double random()
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
     return Math.random();
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
   * The math:sin function returns the sine of the number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
   * @param num A number
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
   * @return The sine value of the number
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
   public static double sin(double num)
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
     return Math.sin(num);
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
   * The math:sqrt function returns the square root of a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
   * @param num A number
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
   * @return The square root of the number
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
   public static double sqrt(double num)
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
     return Math.sqrt(num);
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
   * The math:tan function returns the tangent of the number passed as an argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
   * @param num A number
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
   * @return The tangent value of the number
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
   public static double tan(double num)
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
     return Math.tan(num);
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
   * The math:constant function returns the specified constant to a set precision.
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
   * The possible constants are:
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
   * <pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
   *  PI
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
   *  E
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
   *  SQRRT2
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
   *  LN2
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
   *  LN10
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
   *  LOG2E
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
   *  SQRT1_2
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
   * </pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
   * @param name The name of the constant
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
   * @param precision The precision
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
   * @return The value of the specified constant to the given precision
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
   public static double constant(String name, double precision)
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
     String value = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
     if (name.equals("PI"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
       value = PI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
     else if (name.equals("E"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
       value = E;
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
     else if (name.equals("SQRRT2"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
       value = SQRRT2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
     else if (name.equals("LN2"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
       value = LN2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
     else if (name.equals("LN10"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
       value = LN10;
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
     else if (name.equals("LOG2E"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
       value = LOG2E;
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
     else if (name.equals("SQRT1_2"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
       value = SQRT1_2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
     if (value != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
     {
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
       int bits = new Double(precision).intValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
       if (bits <= value.length())
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
         value = value.substring(0, bits);
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   384
       return Double.parseDouble(value);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
     }
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
     else
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
       return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
}