Save This Page
Home » apache-harmony-6.0-src-r917296-snapshot » java » lang » [javadoc | source]
    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package java.lang;
   19   
   20   /**
   21    * Class Math provides basic math constants and operations such as trigonometric
   22    * functions, hyperbolic functions, exponential, logarithms, etc.
   23    */
   24   public final class Math {
   25   	
   26   	private static final int FLOAT_EXPONENT_BIAS = 127;
   27   
   28       private static final int FLOAT_EXPONENT_MASK = 0x7F800000;
   29   
   30       private static final int DOUBLE_NON_MANTISSA_BITS = 12;
   31   
   32       private static final int DOUBLE_MANTISSA_BITS = 52;
   33   
   34       private static final int FLOAT_NON_MANTISSA_BITS = 9;
   35   
   36       private static final int FLOAT_MANTISSA_BITS = 23;
   37   
   38       private static final int DOUBLE_EXPONENT_BIAS = 1023;
   39   
   40       private static final long DOUBLE_EXPONENT_MASK = 0x7ff0000000000000L;
   41   
   42       private static final int FLOAT_MANTISSA_MASK = 0x007fffff;
   43   
   44       private static final int FLOAT_SIGN_MASK = 0x80000000;
   45   
   46       private static final long DOUBLE_MANTISSA_MASK = 0x000fffffffffffffL;
   47   
   48       private static final long DOUBLE_SIGN_MASK = 0x8000000000000000L;
   49       
   50       /**
   51        * The double value closest to e, the base of the natural logarithm.
   52        */
   53       public static final double E = 2.718281828459045;
   54   
   55       /**
   56        * The double value closest to pi, the ratio of a circle's circumference to
   57        * its diameter.
   58        */
   59       public static final double PI = 3.141592653589793;
   60   
   61       private static java.util.Random random;
   62   
   63       /**
   64        * Prevents this class from being instantiated.
   65        */
   66       private Math() {
   67       }
   68   
   69       /**
   70        * Returns the absolute value of the argument.
   71        * <p>
   72        * Special cases:
   73        * <ul>
   74        * <li>{@code abs(-0.0) = +0.0}</li>
   75        * <li>{@code abs(+infinity) = +infinity}</li>
   76        * <li>{@code abs(-infinity) = +infinity}</li>
   77        * <li>{@code abs(NaN) = NaN}</li>
   78        * </ul>
   79        * 
   80        * @param d
   81        *            the value whose absolute value has to be computed.
   82        * @return the absolute value of the argument.
   83        */
   84       public static double abs(double d) {
   85           long bits = Double.doubleToLongBits(d);
   86           bits &= 0x7fffffffffffffffL;
   87           return Double.longBitsToDouble(bits);
   88       }
   89   
   90       /**
   91        * Returns the absolute value of the argument.
   92        * <p>
   93        * Special cases:
   94        * <ul>
   95        * <li>{@code abs(-0.0) = +0.0}</li>
   96        * <li>{@code abs(+infinity) = +infinity}</li>
   97        * <li>{@code abs(-infinity) = +infinity}</li>
   98        * <li>{@code abs(NaN) = NaN}</li>
   99        * </ul>
  100        * 
  101        * @param f
  102        *            the value whose absolute value has to be computed.
  103        * @return the argument if it is positive, otherwise the negation of the
  104        *         argument.
  105        */
  106       public static float abs(float f) {
  107           int bits = Float.floatToIntBits(f);
  108           bits &= 0x7fffffff;
  109           return Float.intBitsToFloat(bits);
  110       }
  111   
  112       /**
  113        * Returns the absolute value of the argument.
  114        * <p>
  115        * If the argument is {@code Integer.MIN_VALUE}, {@code Integer.MIN_VALUE}
  116        * is returned.
  117        * 
  118        * @param i
  119        *            the value whose absolute value has to be computed.
  120        * @return the argument if it is positive, otherwise the negation of the
  121        *         argument.
  122        */
  123       public static int abs(int i) {
  124           return i >= 0 ? i : -i;
  125       }
  126   
  127       /**
  128        * Returns the absolute value of the argument. If the argument is {@code
  129        * Long.MIN_VALUE}, {@code Long.MIN_VALUE} is returned.
  130        * 
  131        * @param l
  132        *            the value whose absolute value has to be computed.
  133        * @return the argument if it is positive, otherwise the negation of the
  134        *         argument.
  135        */
  136       public static long abs(long l) {
  137           return l >= 0 ? l : -l;
  138       }
  139   
  140       /**
  141        * Returns the closest double approximation of the arc cosine of the
  142        * argument within the range {@code [0..pi]}. The returned result is within
  143        * 1 ulp (unit in the last place) of the real result.
  144        * <p>
  145        * Special cases:
  146        * <ul>
  147        * <li>{@code acos((anything > 1) = NaN}</li>
  148        * <li>{@code acos((anything < -1) = NaN}</li>
  149        * <li>{@code acos(NaN) = NaN}</li>
  150        * </ul>
  151        * 
  152        * @param d
  153        *            the value to compute arc cosine of.
  154        * @return the arc cosine of the argument.
  155        */
  156       public static native double acos(double d);
  157   
  158       /**
  159        * Returns the closest double approximation of the arc sine of the argument
  160        * within the range {@code [-pi/2..pi/2]}. The returned result is within 1
  161        * ulp (unit in the last place) of the real result.
  162        * <p>
  163        * Special cases:
  164        * <ul>
  165        * <li>{@code asin((anything > 1)) = NaN}</li>
  166        * <li>{@code asin((anything < -1)) = NaN}</li>
  167        * <li>{@code asin(NaN) = NaN}</li>
  168        * </ul>
  169        * 
  170        * @param d
  171        *            the value whose arc sine has to be computed.
  172        * @return the arc sine of the argument.
  173        */
  174       public static native double asin(double d);
  175   
  176       /**
  177        * Returns the closest double approximation of the arc tangent of the
  178        * argument within the range {@code [-pi/2..pi/2]}. The returned result is
  179        * within 1 ulp (unit in the last place) of the real result.
  180        * <p>
  181        * Special cases:
  182        * <ul>
  183        * <li>{@code atan(+0.0) = +0.0}</li>
  184        * <li>{@code atan(-0.0) = -0.0}</li>
  185        * <li>{@code atan(+infinity) = +pi/2}</li>
  186        * <li>{@code atan(-infinity) = -pi/2}</li>
  187        * <li>{@code atan(NaN) = NaN}</li>
  188        * </ul>
  189        * 
  190        * @param d
  191        *            the value whose arc tangent has to be computed.
  192        * @return the arc tangent of the argument.
  193        */
  194       public static native double atan(double d);
  195   
  196       /**
  197        * Returns the closest double approximation of the arc tangent of {@code
  198        * y/x} within the range {@code [-pi..pi]}. This is the angle of the polar
  199        * representation of the rectangular coordinates (x,y). The returned result
  200        * is within 2 ulps (units in the last place) of the real result.
  201        * <p>
  202        * Special cases:
  203        * <ul>
  204        * <li>{@code atan2((anything), NaN ) = NaN;}</li>
  205        * <li>{@code atan2(NaN , (anything) ) = NaN;}</li>
  206        * <li>{@code atan2(+0.0, +(anything but NaN)) = +0.0}</li>
  207        * <li>{@code atan2(-0.0, +(anything but NaN)) = -0.0}</li>
  208        * <li>{@code atan2(+0.0, -(anything but NaN)) = +pi}</li>
  209        * <li>{@code atan2(-0.0, -(anything but NaN)) = -pi}</li>
  210        * <li>{@code atan2(+(anything but 0 and NaN), 0) = +pi/2}</li>
  211        * <li>{@code atan2(-(anything but 0 and NaN), 0) = -pi/2}</li>
  212        * <li>{@code atan2(+(anything but infinity and NaN), +infinity)} {@code =}
  213        * {@code +0.0}</li>
  214        * <li>{@code atan2(-(anything but infinity and NaN), +infinity)} {@code =}
  215        * {@code -0.0}</li>
  216        * <li>{@code atan2(+(anything but infinity and NaN), -infinity) = +pi}</li>
  217        * <li>{@code atan2(-(anything but infinity and NaN), -infinity) = -pi}</li>
  218        * <li>{@code atan2(+infinity, +infinity ) = +pi/4}</li>
  219        * <li>{@code atan2(-infinity, +infinity ) = -pi/4}</li>
  220        * <li>{@code atan2(+infinity, -infinity ) = +3pi/4}</li>
  221        * <li>{@code atan2(-infinity, -infinity ) = -3pi/4}</li>
  222        * <li>{@code atan2(+infinity, (anything but,0, NaN, and infinity))} {@code
  223        * =} {@code +pi/2}</li>
  224        * <li>{@code atan2(-infinity, (anything but,0, NaN, and infinity))} {@code
  225        * =} {@code -pi/2}</li>
  226        * </ul>
  227        * 
  228        * @param y
  229        *            the numerator of the value whose atan has to be computed.
  230        * @param x
  231        *            the denominator of the value whose atan has to be computed.
  232        * @return the arc tangent of {@code y/x}.
  233        */
  234       public static native double atan2(double x, double y);
  235   
  236       /**
  237        * Returns the closest double approximation of the cube root of the
  238        * argument.
  239        * <p>
  240        * Special cases:
  241        * <ul>
  242        * <li>{@code cbrt(+0.0) = +0.0}</li>
  243        * <li>{@code cbrt(-0.0) = -0.0}</li>
  244        * <li>{@code cbrt(+infinity) = +infinity}</li>
  245        * <li>{@code cbrt(-infinity) = -infinity}</li>
  246        * <li>{@code cbrt(NaN) = NaN}</li>
  247        * </ul>
  248        * 
  249        * @param d
  250        *            the value whose cube root has to be computed.
  251        * @return the cube root of the argument.
  252        */
  253       public static native double cbrt(double d);
  254   
  255       /**
  256        * Returns the double conversion of the most negative (closest to negative
  257        * infinity) integer value which is greater than the argument.
  258        * <p>
  259        * Special cases:
  260        * <ul>
  261        * <li>{@code ceil(+0.0) = +0.0}</li>
  262        * <li>{@code ceil(-0.0) = -0.0}</li>
  263        * <li>{@code ceil((anything in range (-1,0)) = -0.0}</li>
  264        * <li>{@code ceil(+infinity) = +infinity}</li>
  265        * <li>{@code ceil(-infinity) = -infinity}</li>
  266        * <li>{@code ceil(NaN) = NaN}</li>
  267        * </ul>
  268        * 
  269        * @param d
  270        *            the value whose closest integer value has to be computed.
  271        * @return the ceiling of the argument.
  272        */
  273       public static native double ceil(double d);
  274   
  275       /**
  276        * Returns the closest double approximation of the cosine of the argument.
  277        * The returned result is within 1 ulp (unit in the last place) of the real
  278        * result.
  279        * <p>
  280        * Special cases:
  281        * <ul>
  282        * <li>{@code cos(+infinity) = NaN}</li>
  283        * <li>{@code cos(-infinity) = NaN}</li>
  284        * <li>{@code cos(NaN) = NaN}</li>
  285        * </ul>
  286        * 
  287        * @param d
  288        *            the angle whose cosine has to be computed, in radians.
  289        * @return the cosine of the argument.
  290        */
  291       public static native double cos(double d);
  292   
  293       /**
  294        * Returns the closest double approximation of the hyperbolic cosine of the
  295        * argument. The returned result is within 2.5 ulps (units in the last
  296        * place) of the real result.
  297        * <p>
  298        * Special cases:
  299        * <ul>
  300        * <li>{@code cosh(+infinity) = +infinity}</li>
  301        * <li>{@code cosh(-infinity) = +infinity}</li>
  302        * <li>{@code cosh(NaN) = NaN}</li>
  303        * </ul>
  304        * 
  305        * @param d
  306        *            the value whose hyperbolic cosine has to be computed.
  307        * @return the hyperbolic cosine of the argument.
  308        */
  309       public static native double cosh(double d);
  310   
  311       /**
  312        * Returns the closest double approximation of the raising "e" to the power
  313        * of the argument. The returned result is within 1 ulp (unit in the last
  314        * place) of the real result.
  315        * <p>
  316        * Special cases:
  317        * <ul>
  318        * <li>{@code exp(+infinity) = +infinity}</li>
  319        * <li>{@code exp(-infinity) = +0.0}</li>
  320        * <li>{@code exp(NaN) = NaN}</li>
  321        * </ul>
  322        * 
  323        * @param d
  324        *            the value whose exponential has to be computed.
  325        * @return the exponential of the argument.
  326        */
  327       public static native double exp(double d);
  328   
  329       /**
  330        * Returns the closest double approximation of <i>{@code e}</i><sup> {@code
  331        * d}</sup>{@code - 1}. If the argument is very close to 0, it is much more
  332        * accurate to use {@code expm1(d)+1} than {@code exp(d)} (due to
  333        * cancellation of significant digits). The returned result is within 1 ulp
  334        * (unit in the last place) of the real result.
  335        * <p>
  336        * For any finite input, the result is not less than -1.0. If the real
  337        * result is within 0.5 ulp of -1, -1.0 is returned.
  338        * <p>
  339        * Special cases:
  340        * <ul>
  341        * <li>{@code expm1(+0.0) = +0.0}</li>
  342        * <li>{@code expm1(-0.0) = -0.0}</li>
  343        * <li>{@code expm1(+infinity) = +infinity}</li>
  344        * <li>{@code expm1(-infinity) = -1.0}</li>
  345        * <li>{@code expm1(NaN) = NaN}</li>
  346        * </ul>
  347        * 
  348        * @param d
  349        *            the value to compute the <i>{@code e}</i><sup>{@code d} </sup>
  350        *            {@code - 1} of.
  351        * @return the <i>{@code e}</i><sup>{@code d}</sup>{@code - 1} value of the
  352        *         argument.
  353        */
  354       public static native double expm1(double d);
  355   
  356       /**
  357        * Returns the double conversion of the most positive (closest to positive
  358        * infinity) integer value which is less than the argument.
  359        * <p>
  360        * Special cases:
  361        * <ul>
  362        * <li>{@code floor(+0.0) = +0.0}</li>
  363        * <li>{@code floor(-0.0) = -0.0}</li>
  364        * <li>{@code floor(+infinity) = +infinity}</li>
  365        * <li>{@code floor(-infinity) = -infinity}</li>
  366        * <li>{@code floor(NaN) = NaN}</li>
  367        * </ul>
  368        * 
  369        * @param d
  370        *            the value whose closest integer value has to be computed.
  371        * @return the floor of the argument.
  372        */
  373       public static native double floor(double d);
  374   
  375       /**
  376        * Returns {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +} <i>
  377        * {@code y}</i><sup>{@code 2}</sup>{@code )}. The final result is without
  378        * medium underflow or overflow. The returned result is within 1 ulp (unit
  379        * in the last place) of the real result. If one parameter remains constant,
  380        * the result should be semi-monotonic.
  381        * <p>
  382        * Special cases:
  383        * <ul>
  384        * <li>{@code hypot(+infinity, (anything including NaN)) = +infinity}</li>
  385        * <li>{@code hypot(-infinity, (anything including NaN)) = +infinity}</li>
  386        * <li>{@code hypot((anything including NaN), +infinity) = +infinity}</li>
  387        * <li>{@code hypot((anything including NaN), -infinity) = +infinity}</li>
  388        * <li>{@code hypot(NaN, NaN) = NaN}</li>
  389        * </ul>
  390        * 
  391        * @param x
  392        *            a double number.
  393        * @param y
  394        *            a double number.
  395        * @return the {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +}
  396        *         <i> {@code y}</i><sup>{@code 2}</sup>{@code )} value of the
  397        *         arguments.
  398        */
  399       public static native double hypot(double x, double y);
  400   
  401       /**
  402        * Returns the remainder of dividing {@code x} by {@code y} using the IEEE
  403        * 754 rules. The result is {@code x-round(x/p)*p} where {@code round(x/p)}
  404        * is the nearest integer (rounded to even), but without numerical
  405        * cancellation problems.
  406        * <p>
  407        * Special cases:
  408        * <ul>
  409        * <li>{@code IEEEremainder((anything), 0) = NaN}</li>
  410        * <li>{@code IEEEremainder(+infinity, (anything)) = NaN}</li>
  411        * <li>{@code IEEEremainder(-infinity, (anything)) = NaN}</li>
  412        * <li>{@code IEEEremainder(NaN, (anything)) = NaN}</li>
  413        * <li>{@code IEEEremainder((anything), NaN) = NaN}</li>
  414        * <li>{@code IEEEremainder(x, +infinity) = x } where x is anything but
  415        * +/-infinity</li>
  416        * <li>{@code IEEEremainder(x, -infinity) = x } where x is anything but
  417        * +/-infinity</li>
  418        * </ul>
  419        * 
  420        * @param x
  421        *            the numerator of the operation.
  422        * @param y
  423        *            the denominator of the operation.
  424        * @return the IEEE754 floating point reminder of of {@code x/y}.
  425        */
  426       public static native double IEEEremainder(double x, double y);
  427   
  428       /**
  429        * Returns the closest double approximation of the natural logarithm of the
  430        * argument. The returned result is within 1 ulp (unit in the last place) of
  431        * the real result.
  432        * <p>
  433        * Special cases:
  434        * <ul>
  435        * <li>{@code log(+0.0) = -infinity}</li>
  436        * <li>{@code log(-0.0) = -infinity}</li>
  437        * <li>{@code log((anything < 0) = NaN}</li>
  438        * <li>{@code log(+infinity) = +infinity}</li>
  439        * <li>{@code log(-infinity) = NaN}</li>
  440        * <li>{@code log(NaN) = NaN}</li>
  441        * </ul>
  442        * 
  443        * @param d
  444        *            the value whose log has to be computed.
  445        * @return the natural logarithm of the argument.
  446        */
  447       public static native double log(double d);
  448   
  449       /**
  450        * Returns the closest double approximation of the base 10 logarithm of the
  451        * argument. The returned result is within 1 ulp (unit in the last place) of
  452        * the real result.
  453        * <p>
  454        * Special cases:
  455        * <ul>
  456        * <li>{@code log10(+0.0) = -infinity}</li>
  457        * <li>{@code log10(-0.0) = -infinity}</li>
  458        * <li>{@code log10((anything < 0) = NaN}</li>
  459        * <li>{@code log10(+infinity) = +infinity}</li>
  460        * <li>{@code log10(-infinity) = NaN}</li>
  461        * <li>{@code log10(NaN) = NaN}</li>
  462        * </ul>
  463        * 
  464        * @param d
  465        *            the value whose base 10 log has to be computed.
  466        * @return the natural logarithm of the argument.
  467        */
  468       public static native double log10(double d);
  469   
  470       /**
  471        * Returns the closest double approximation of the natural logarithm of the
  472        * sum of the argument and 1. If the argument is very close to 0, it is much
  473        * more accurate to use {@code log1p(d)} than {@code log(1.0+d)} (due to
  474        * numerical cancellation). The returned result is within 1 ulp (unit in the
  475        * last place) of the real result and is semi-monotonic.
  476        * <p>
  477        * Special cases:
  478        * <ul>
  479        * <li>{@code log1p(+0.0) = +0.0}</li>
  480        * <li>{@code log1p(-0.0) = -0.0}</li>
  481        * <li>{@code log1p((anything < 1)) = NaN}</li>
  482        * <li>{@code log1p(-1.0) = -infinity}</li>
  483        * <li>{@code log1p(+infinity) = +infinity}</li>
  484        * <li>{@code log1p(-infinity) = NaN}</li>
  485        * <li>{@code log1p(NaN) = NaN}</li>
  486        * </ul>
  487        * 
  488        * @param d
  489        *            the value to compute the {@code ln(1+d)} of.
  490        * @return the natural logarithm of the sum of the argument and 1.
  491        */
  492       public static native double log1p(double d);
  493   
  494       /**
  495        * Returns the most positive (closest to positive infinity) of the two
  496        * arguments.
  497        * <p>
  498        * Special cases:
  499        * <ul>
  500        * <li>{@code max(NaN, (anything)) = NaN}</li>
  501        * <li>{@code max((anything), NaN) = NaN}</li>
  502        * <li>{@code max(+0.0, -0.0) = +0.0}</li>
  503        * <li>{@code max(-0.0, +0.0) = +0.0}</li>
  504        * </ul>
  505        * 
  506        * @param d1
  507        *            the first argument.
  508        * @param d2
  509        *            the second argument.
  510        * @return the larger of {@code d1} and {@code d2}.
  511        */
  512       public static double max(double d1, double d2) {
  513           if (d1 > d2) {
  514               return d1;
  515           }
  516           if (d1 < d2) {
  517               return d2;
  518           }
  519           /* if either arg is NaN, return NaN */
  520           if (d1 != d2) {
  521               return Double.NaN;
  522           }
  523           /* max(+0.0,-0.0) == +0.0 */
  524           /* 0 == Double.doubleToRawLongBits(0.0d) */
  525           if (Double.doubleToRawLongBits(d1) != 0) {
  526               return d2;
  527           }
  528           return 0.0d;
  529       }
  530   
  531       /**
  532        * Returns the most positive (closest to positive infinity) of the two
  533        * arguments.
  534        * <p>
  535        * Special cases:
  536        * <ul>
  537        * <li>{@code max(NaN, (anything)) = NaN}</li>
  538        * <li>{@code max((anything), NaN) = NaN}</li>
  539        * <li>{@code max(+0.0, -0.0) = +0.0}</li>
  540        * <li>{@code max(-0.0, +0.0) = +0.0}</li>
  541        * </ul>
  542        * 
  543        * @param f1
  544        *            the first argument.
  545        * @param f2
  546        *            the second argument.
  547        * @return the larger of {@code f1} and {@code f2}.
  548        */
  549       public static float max(float f1, float f2) {
  550           if (f1 > f2) {
  551               return f1;
  552           }
  553           if (f1 < f2) {
  554               return f2;
  555           }
  556           /* if either arg is NaN, return NaN */
  557           if (f1 != f2) {
  558               return Float.NaN;
  559           }
  560           /* max(+0.0,-0.0) == +0.0 */
  561           /* 0 == Float.floatToRawIntBits(0.0f) */
  562           if (Float.floatToRawIntBits(f1) != 0) {
  563               return f2;
  564           }
  565           return 0.0f;
  566       }
  567   
  568       /**
  569        * Returns the most positive (closest to positive infinity) of the two
  570        * arguments.
  571        * 
  572        * @param i1
  573        *            the first argument.
  574        * @param i2
  575        *            the second argument.
  576        * @return the larger of {@code i1} and {@code i2}.
  577        */
  578       public static int max(int i1, int i2) {
  579           return i1 > i2 ? i1 : i2;
  580       }
  581   
  582       /**
  583        * Returns the most positive (closest to positive infinity) of the two
  584        * arguments.
  585        * 
  586        * @param l1
  587        *            the first argument.
  588        * @param l2
  589        *            the second argument.
  590        * @return the larger of {@code l1} and {@code l2}.
  591        */
  592       public static long max(long l1, long l2) {
  593           return l1 > l2 ? l1 : l2;
  594       }
  595   
  596       /**
  597        * Returns the most negative (closest to negative infinity) of the two
  598        * arguments.
  599        * <p>
  600        * Special cases:
  601        * <ul>
  602        * <li>{@code min(NaN, (anything)) = NaN}</li>
  603        * <li>{@code min((anything), NaN) = NaN}</li>
  604        * <li>{@code min(+0.0, -0.0) = -0.0}</li>
  605        * <li>{@code min(-0.0, +0.0) = -0.0}</li>
  606        * </ul>
  607        * 
  608        * @param d1
  609        *            the first argument.
  610        * @param d2
  611        *            the second argument.
  612        * @return the smaller of {@code d1} and {@code d2}.
  613        */
  614       public static double min(double d1, double d2) {
  615           if (d1 > d2) {
  616               return d2;
  617           }
  618           if (d1 < d2) {
  619               return d1;
  620           }
  621           /* if either arg is NaN, return NaN */
  622           if (d1 != d2) {
  623               return Double.NaN;
  624           }
  625           /* min(+0.0,-0.0) == -0.0 */
  626           /* 0x8000000000000000L == Double.doubleToRawLongBits(-0.0d) */
  627           if (Double.doubleToRawLongBits(d1) == 0x8000000000000000L) {
  628               return -0.0d;
  629           }
  630           return d2;
  631       }
  632   
  633       /**
  634        * Returns the most negative (closest to negative infinity) of the two
  635        * arguments.
  636        * <p>
  637        * Special cases:
  638        * <ul>
  639        * <li>{@code min(NaN, (anything)) = NaN}</li>
  640        * <li>{@code min((anything), NaN) = NaN}</li>
  641        * <li>{@code min(+0.0, -0.0) = -0.0}</li>
  642        * <li>{@code min(-0.0, +0.0) = -0.0}</li>
  643        * </ul>
  644        * 
  645        * @param f1
  646        *            the first argument.
  647        * @param f2
  648        *            the second argument.
  649        * @return the smaller of {@code f1} and {@code f2}.
  650        */
  651       public static float min(float f1, float f2) {
  652           if (f1 > f2) {
  653               return f2;
  654           }
  655           if (f1 < f2) {
  656               return f1;
  657           }
  658           /* if either arg is NaN, return NaN */
  659           if (f1 != f2) {
  660               return Float.NaN;
  661           }
  662           /* min(+0.0,-0.0) == -0.0 */
  663           /* 0x80000000 == Float.floatToRawIntBits(-0.0f) */
  664           if (Float.floatToRawIntBits(f1) == 0x80000000) {
  665               return -0.0f;
  666           }
  667           return f2;
  668       }
  669   
  670       /**
  671        * Returns the most negative (closest to negative infinity) of the two
  672        * arguments.
  673        * 
  674        * @param i1
  675        *            the first argument.
  676        * @param i2
  677        *            the second argument.
  678        * @return the smaller of {@code i1} and {@code i2}.
  679        */
  680       public static int min(int i1, int i2) {
  681           return i1 < i2 ? i1 : i2;
  682       }
  683   
  684       /**
  685        * Returns the most negative (closest to negative infinity) of the two
  686        * arguments.
  687        * 
  688        * @param l1
  689        *            the first argument.
  690        * @param l2
  691        *            the second argument.
  692        * @return the smaller of {@code l1} and {@code l2}.
  693        */
  694       public static long min(long l1, long l2) {
  695           return l1 < l2 ? l1 : l2;
  696       }
  697   
  698       /**
  699        * Returns the closest double approximation of the result of raising {@code
  700        * x} to the power of {@code y}.
  701        * <p>
  702        * Special cases:
  703        * <ul>
  704        * <li>{@code pow((anything), +0.0) = 1.0}</li>
  705        * <li>{@code pow((anything), -0.0) = 1.0}</li>
  706        * <li>{@code pow(x, 1.0) = x}</li>
  707        * <li>{@code pow((anything), NaN) = NaN}</li>
  708        * <li>{@code pow(NaN, (anything except 0)) = NaN}</li>
  709        * <li>{@code pow(+/-(|x| > 1), +infinity) = +infinity}</li>
  710        * <li>{@code pow(+/-(|x| > 1), -infinity) = +0.0}</li>
  711        * <li>{@code pow(+/-(|x| < 1), +infinity) = +0.0}</li>
  712        * <li>{@code pow(+/-(|x| < 1), -infinity) = +infinity}</li>
  713        * <li>{@code pow(+/-1.0 , +infinity) = NaN}</li>
  714        * <li>{@code pow(+/-1.0 , -infinity) = NaN}</li>
  715        * <li>{@code pow(+0.0, (+anything except 0, NaN)) = +0.0}</li>
  716        * <li>{@code pow(-0.0, (+anything except 0, NaN, odd integer)) = +0.0}</li>
  717        * <li>{@code pow(+0.0, (-anything except 0, NaN)) = +infinity}</li>
  718        * <li>{@code pow(-0.0, (-anything except 0, NAN, odd integer))} {@code =}
  719        * {@code +infinity}</li>
  720        * <li>{@code pow(-0.0, (odd integer)) = -pow( +0 , (odd integer) )}</li>
  721        * <li>{@code pow(+infinity, (+anything except 0, NaN)) = +infinity}</li>
  722        * <li>{@code pow(+infinity, (-anything except 0, NaN)) = +0.0}</li>
  723        * <li>{@code pow(-infinity, (anything)) = -pow(0, (-anything))}</li>
  724        * <li>{@code pow((-anything), (integer))} {@code =} {@code
  725        * pow(-1,(integer))*pow(+anything,integer) }</li>
  726        * <li>{@code pow((-anything except 0 and inf), (non-integer)) = NAN}</li>
  727        * </ul>
  728        * 
  729        * @param x
  730        *            the base of the operation.
  731        * @param y
  732        *            the exponent of the operation.
  733        * @return {@code x} to the power of {@code y}.
  734        */
  735       public static double pow(double x, double y) {
  736           if (x < 0.0 && x != Double.NEGATIVE_INFINITY) {
  737               if (y != Double.NEGATIVE_INFINITY && y != Double.POSITIVE_INFINITY) {  // is finit
  738                   if (y == Math.ceil(y)) {  // is integer
  739                       double absx = Math.abs(x);
  740                       if (y % 2 == 0.0) {
  741                           return doPow(absx, y);
  742                       }
  743                       return -1.0 * doPow(absx, y);
  744                   }
  745                   return Double.NaN;
  746               }
  747           }
  748           return doPow(x, y);
  749       }
  750   
  751       private static native double doPow(double x, double y);
  752   
  753       /**
  754        * Returns the double conversion of the result of rounding the argument to
  755        * an integer. Tie breaks are rounded towards even.
  756        * <p>
  757        * Special cases:
  758        * <ul>
  759        * <li>{@code rint(+0.0) = +0.0}</li>
  760        * <li>{@code rint(-0.0) = -0.0}</li>
  761        * <li>{@code rint(+infinity) = +infinity}</li>
  762        * <li>{@code rint(-infinity) = -infinity}</li>
  763        * <li>{@code rint(NaN) = NaN}</li>
  764        * </ul>
  765        * 
  766        * @param d
  767        *            the value to be rounded.
  768        * @return the closest integer to the argument (as a double).
  769        */
  770       public static native double rint(double d);
  771   
  772       /**
  773        * Returns the result of rounding the argument to an integer. The result is
  774        * equivalent to {@code (long) Math.floor(d+0.5)}.
  775        * <p>
  776        * Special cases:
  777        * <ul>
  778        * <li>{@code round(+0.0) = +0.0}</li>
  779        * <li>{@code round(-0.0) = +0.0}</li>
  780        * <li>{@code round((anything > Long.MAX_VALUE) = Long.MAX_VALUE}</li>
  781        * <li>{@code round((anything < Long.MIN_VALUE) = Long.MIN_VALUE}</li>
  782        * <li>{@code round(+infintiy) = Long.MAX_VALUE}</li>
  783        * <li>{@code round(-infintiy) = Long.MIN_VALUE}</li>
  784        * <li>{@code round(NaN) = +0.0}</li>
  785        * </ul>
  786        * 
  787        * @param d
  788        *            the value to be rounded.
  789        * @return the closest integer to the argument.
  790        */
  791       public static long round(double d) {
  792           // check for NaN
  793           if (d != d) {
  794               return 0L;
  795           }
  796           return (long) floor(d + 0.5d);
  797       }
  798   
  799       /**
  800        * Returns the result of rounding the argument to an integer. The result is
  801        * equivalent to {@code (int) Math.floor(f+0.5)}.
  802        * <p>
  803        * Special cases:
  804        * <ul>
  805        * <li>{@code round(+0.0) = +0.0}</li>
  806        * <li>{@code round(-0.0) = +0.0}</li>
  807        * <li>{@code round((anything > Integer.MAX_VALUE) = Integer.MAX_VALUE}</li>
  808        * <li>{@code round((anything < Integer.MIN_VALUE) = Integer.MIN_VALUE}</li>
  809        * <li>{@code round(+infintiy) = Integer.MAX_VALUE}</li>
  810        * <li>{@code round(-infintiy) = Integer.MIN_VALUE}</li>
  811        * <li>{@code round(NaN) = +0.0}</li>
  812        * </ul>
  813        * 
  814        * @param f
  815        *            the value to be rounded.
  816        * @return the closest integer to the argument.
  817        */
  818       public static int round(float f) {
  819           // check for NaN
  820           if (f != f) {
  821               return 0;
  822           }
  823           return (int) floor(f + 0.5f);
  824       }
  825   
  826       /**
  827        * Returns the signum function of the argument. If the argument is less than
  828        * zero, it returns -1.0. If the argument is greater than zero, 1.0 is
  829        * returned. If the argument is either positive or negative zero, the
  830        * argument is returned as result.
  831        * <p>
  832        * Special cases:
  833        * <ul>
  834        * <li>{@code signum(+0.0) = +0.0}</li>
  835        * <li>{@code signum(-0.0) = -0.0}</li>
  836        * <li>{@code signum(+infinity) = +1.0}</li>
  837        * <li>{@code signum(-infinity) = -1.0}</li>
  838        * <li>{@code signum(NaN) = NaN}</li>
  839        * </ul>
  840        * 
  841        * @param d
  842        *            the value whose signum has to be computed.
  843        * @return the value of the signum function.
  844        */
  845       public static double signum(double d) {
  846           return StrictMath.signum(d);
  847       }
  848   
  849       /**
  850        * Returns the signum function of the argument. If the argument is less than
  851        * zero, it returns -1.0. If the argument is greater than zero, 1.0 is
  852        * returned. If the argument is either positive or negative zero, the
  853        * argument is returned as result.
  854        * <p>
  855        * Special cases:
  856        * <ul>
  857        * <li>{@code signum(+0.0) = +0.0}</li>
  858        * <li>{@code signum(-0.0) = -0.0}</li>
  859        * <li>{@code signum(+infinity) = +1.0}</li>
  860        * <li>{@code signum(-infinity) = -1.0}</li>
  861        * <li>{@code signum(NaN) = NaN}</li>
  862        * </ul>
  863        * 
  864        * @param f
  865        *            the value whose signum has to be computed.
  866        * @return the value of the signum function.
  867        */
  868       public static float signum(float f) {
  869           return StrictMath.signum(f);
  870       }
  871   
  872       /**
  873        * Returns the closest double approximation of the sine of the argument. The
  874        * returned result is within 1 ulp (unit in the last place) of the real
  875        * result.
  876        * <p>
  877        * Special cases:
  878        * <ul>
  879        * <li>{@code sin(+0.0) = +0.0}</li>
  880        * <li>{@code sin(-0.0) = -0.0}</li>
  881        * <li>{@code sin(+infinity) = NaN}</li>
  882        * <li>{@code sin(-infinity) = NaN}</li>
  883        * <li>{@code sin(NaN) = NaN}</li>
  884        * </ul>
  885        * 
  886        * @param d
  887        *            the angle whose sin has to be computed, in radians.
  888        * @return the sine of the argument.
  889        */
  890       public static native double sin(double d);
  891   
  892       /**
  893        * Returns the closest double approximation of the hyperbolic sine of the
  894        * argument. The returned result is within 2.5 ulps (units in the last
  895        * place) of the real result.
  896        * <p>
  897        * Special cases:
  898        * <ul>
  899        * <li>{@code sinh(+0.0) = +0.0}</li>
  900        * <li>{@code sinh(-0.0) = -0.0}</li>
  901        * <li>{@code sinh(+infinity) = +infinity}</li>
  902        * <li>{@code sinh(-infinity) = -infinity}</li>
  903        * <li>{@code sinh(NaN) = NaN}</li>
  904        * </ul>
  905        * 
  906        * @param d
  907        *            the value whose hyperbolic sine has to be computed.
  908        * @return the hyperbolic sine of the argument.
  909        */
  910       public static native double sinh(double d);
  911   
  912       /**
  913        * Returns the closest double approximation of the square root of the
  914        * argument.
  915        * <p>
  916        * Special cases:
  917        * <ul>
  918        * <li>{@code sqrt(+0.0) = +0.0}</li>
  919        * <li>{@code sqrt(-0.0) = -0.0}</li>
  920        * <li>{@code sqrt( (anything < 0) ) = NaN}</li>
  921        * <li>{@code sqrt(+infinity) = +infinity}</li>
  922        * <li>{@code sqrt(NaN) = NaN}</li>
  923        * </ul>
  924        * 
  925        * @param d
  926        *            the value whose square root has to be computed.
  927        * @return the square root of the argument.
  928        */
  929       public static native double sqrt(double d);
  930   
  931       /**
  932        * Returns the closest double approximation of the tangent of the argument.
  933        * The returned result is within 1 ulp (unit in the last place) of the real
  934        * result.
  935        * <p>
  936        * Special cases:
  937        * <ul>
  938        * <li>{@code tan(+0.0) = +0.0}</li>
  939        * <li>{@code tan(-0.0) = -0.0}</li>
  940        * <li>{@code tan(+infinity) = NaN}</li>
  941        * <li>{@code tan(-infinity) = NaN}</li>
  942        * <li>{@code tan(NaN) = NaN}</li>
  943        * </ul>
  944        * 
  945        * @param d
  946        *            the angle whose tangens has to be computed, in radians.
  947        * @return the tangent of the argument.
  948        */
  949       public static native double tan(double d);
  950   
  951       /**
  952        * Returns the closest double approximation of the hyperbolic tangent of the
  953        * argument. The absolute value is always less than 1. The returned result
  954        * is within 2.5 ulps (units in the last place) of the real result. If the
  955        * real result is within 0.5ulp of 1 or -1, it should return exactly +1 or
  956        * -1.
  957        * <p>
  958        * Special cases:
  959        * <ul>
  960        * <li>{@code tanh(+0.0) = +0.0}</li>
  961        * <li>{@code tanh(-0.0) = -0.0}</li>
  962        * <li>{@code tanh(+infinity) = +1.0}</li>
  963        * <li>{@code tanh(-infinity) = -1.0}</li>
  964        * <li>{@code tanh(NaN) = NaN}</li>
  965        * </ul>
  966        * 
  967        * @param d
  968        *            the value whose hyperbolic tangent has to be computed.
  969        * @return the hyperbolic tangent of the argument.
  970        */
  971       public static native double tanh(double d);
  972   
  973       /**
  974        * Returns a pseudo-random number between 0.0 (inclusive) and 1.0
  975        * (exclusive).
  976        * 
  977        * @return a pseudo-random number.
  978        */
  979       public static double random() {
  980           if (random == null) {
  981               random = new java.util.Random();
  982           }
  983           return random.nextDouble();
  984       }
  985   
  986       /**
  987        * Returns the measure in radians of the supplied degree angle. The result
  988        * is {@code angdeg / 180 * pi}.
  989        * <p>
  990        * Special cases:
  991        * <ul>
  992        * <li>{@code toRadians(+0.0) = +0.0}</li>
  993        * <li>{@code toRadians(-0.0) = -0.0}</li>
  994        * <li>{@code toRadians(+infinity) = +infinity}</li>
  995        * <li>{@code toRadians(-infinity) = -infinity}</li>
  996        * <li>{@code toRadians(NaN) = NaN}</li>
  997        * </ul>
  998        * 
  999        * @param angdeg
 1000        *            an angle in degrees.
 1001        * @return the radian measure of the angle.
 1002        */
 1003       public static double toRadians(double angdeg) {
 1004           return angdeg / 180d * PI;
 1005       }
 1006   
 1007       /**
 1008        * Returns the measure in degrees of the supplied radian angle. The result
 1009        * is {@code angrad * 180 / pi}.
 1010        * <p>
 1011        * Special cases:
 1012        * <ul>
 1013        * <li>{@code toDegrees(+0.0) = +0.0}</li>
 1014        * <li>{@code toDegrees(-0.0) = -0.0}</li>
 1015        * <li>{@code toDegrees(+infinity) = +infinity}</li>
 1016        * <li>{@code toDegrees(-infinity) = -infinity}</li>
 1017        * <li>{@code toDegrees(NaN) = NaN}</li>
 1018        * </ul>
 1019        * 
 1020        * @param angrad
 1021        *            an angle in radians.
 1022        * @return the degree measure of the angle.
 1023        */
 1024       public static double toDegrees(double angrad) {
 1025           return angrad * 180d / PI;
 1026       }
 1027   
 1028       /**
 1029        * Returns the argument's ulp (unit in the last place). The size of a ulp of
 1030        * a double value is the positive distance between this value and the double
 1031        * value next larger in magnitude. For non-NaN {@code x}, {@code ulp(-x) ==
 1032        * ulp(x)}.
 1033        * <p>
 1034        * Special cases:
 1035        * <ul>
 1036        * <li>{@code ulp(+0.0) = Double.MIN_VALUE}</li>
 1037        * <li>{@code ulp(-0.0) = Double.MIN_VALUE}</li>
 1038        * <li>{@code ulp(+infintiy) = infinity}</li>
 1039        * <li>{@code ulp(-infintiy) = infinity}</li>
 1040        * <li>{@code ulp(NaN) = NaN}</li>
 1041        * </ul>
 1042        * 
 1043        * @param d
 1044        *            the floating-point value to compute ulp of.
 1045        * @return the size of a ulp of the argument.
 1046        */
 1047       public static double ulp(double d) {
 1048           // special cases
 1049           if (Double.isInfinite(d)) {
 1050               return Double.POSITIVE_INFINITY;
 1051           } else if (d == Double.MAX_VALUE || d == -Double.MAX_VALUE) {
 1052               return pow(2, 971);
 1053           }
 1054           d = abs(d);
 1055           return nextafter(d, Double.MAX_VALUE) - d;
 1056       }
 1057   
 1058       /**
 1059        * Returns the argument's ulp (unit in the last place). The size of a ulp of
 1060        * a float value is the positive distance between this value and the float
 1061        * value next larger in magnitude. For non-NaN {@code x}, {@code ulp(-x) ==
 1062        * ulp(x)}.
 1063        * <p>
 1064        * Special cases:
 1065        * <ul>
 1066        * <li>{@code ulp(+0.0) = Float.MIN_VALUE}</li>
 1067        * <li>{@code ulp(-0.0) = Float.MIN_VALUE}</li>
 1068        * <li>{@code ulp(+infintiy) = infinity}</li>
 1069        * <li>{@code ulp(-infintiy) = infinity}</li>
 1070        * <li>{@code ulp(NaN) = NaN}</li>
 1071        * </ul>
 1072        * 
 1073        * @param f
 1074        *            the floating-point value to compute ulp of.
 1075        * @return the size of a ulp of the argument.
 1076        */
 1077       public static float ulp(float f) {
 1078           // special cases
 1079           if (Float.isNaN(f)) {
 1080               return Float.NaN;
 1081           } else if (Float.isInfinite(f)) {
 1082               return Float.POSITIVE_INFINITY;
 1083           } else if (f == Float.MAX_VALUE || f == -Float.MAX_VALUE) {
 1084               return (float) pow(2, 104);
 1085           }
 1086           f = abs(f);
 1087           return nextafterf(f, Float.MAX_VALUE) - f;
 1088       }
 1089   
 1090       private native static double nextafter(double x, double y);
 1091   
 1092       private native static float nextafterf(float x, float y);
 1093       
 1094       /**
 1095        * Answers a result of the magnitude of the first given double value and the
 1096        * sign of the second given double value.
 1097        * 
 1098        * @param magnitude
 1099        *            the double value whose magnitude should be used
 1100        * @param sign
 1101        *            the double value whose sign should be used
 1102        * @return a result of the magnitude of the first given double value and the
 1103        *         sign of the second given double value .
 1104        * 
 1105        * @since 1.6
 1106        */
 1107       public static double copySign(double magnitude, double sign) {
 1108           long mbits = Double.doubleToRawLongBits(magnitude);
 1109           long sbits = Double.doubleToRawLongBits(sign);
 1110           return Double.longBitsToDouble((mbits & ~DOUBLE_SIGN_MASK)
 1111                   | (sbits & DOUBLE_SIGN_MASK));
 1112       }
 1113   
 1114       /**
 1115        * Answers a result of the magnitude of the first given float value and the
 1116        * sign of the second given float value .
 1117        * 
 1118        * @param magnitude
 1119        *            the float value whose magnitude should be used
 1120        * @param sign
 1121        *            the float value whose sign should be used
 1122        * @return a result with the magnitude of the first given float value and
 1123        *         the sign of the second given float value .
 1124        * 
 1125        * @since 1.6
 1126        */
 1127       public static float copySign(float magnitude, float sign) {
 1128           int mbits = Float.floatToRawIntBits(magnitude);
 1129           int sbits = Float.floatToRawIntBits(sign);
 1130           return Float.intBitsToFloat((mbits & ~FLOAT_SIGN_MASK)
 1131                   | (sbits & FLOAT_SIGN_MASK));
 1132       }
 1133       
 1134       /**
 1135        * Answers the exponent of a float.
 1136        * 
 1137        * @param f
 1138        *            the given float
 1139        * @return the exponent of the float.
 1140        * 
 1141        * @since 1.6
 1142        */
 1143       public static int getExponent(float f) {
 1144           int bits = Float.floatToRawIntBits(f);
 1145           bits = (bits & FLOAT_EXPONENT_MASK) >> FLOAT_MANTISSA_BITS;
 1146           return bits - FLOAT_EXPONENT_BIAS;
 1147       }
 1148   
 1149       /**
 1150        * Answers the exponent of a double.
 1151        * 
 1152        * @param d
 1153        *            the given double
 1154        * @return the exponent of the double.
 1155        * 
 1156        * @since 1.6
 1157        */
 1158       public static int getExponent(double d) {
 1159           long bits = Double.doubleToRawLongBits(d);
 1160           bits = (bits & DOUBLE_EXPONENT_MASK) >> DOUBLE_MANTISSA_BITS;
 1161           return (int) bits - DOUBLE_EXPONENT_BIAS;
 1162       }    
 1163       
 1164       /**
 1165        * Answers a double next to the first given double value in the direction of
 1166        * the second given double.
 1167        * 
 1168        * @param start
 1169        *            the double value to start
 1170        * @param direction
 1171        *            the double indicating the direction
 1172        * @return a double next to the first given double value in the direction of
 1173        *         the second given double.
 1174        * 
 1175        * @since 1.6
 1176        */
 1177       public static double nextAfter(double start, double direction) {
 1178           if (0 == start && 0 == direction) {
 1179               return direction;
 1180           }
 1181           return nextafter(start, direction);
 1182       }
 1183   
 1184       /**
 1185        * Answers a float next to the first given float value in the direction of
 1186        * the second given double value.
 1187        * 
 1188        * @param start
 1189        *            the float value to start
 1190        * @param direction
 1191        *            the double indicating the direction
 1192        * @return a float next to the first given float value in the direction of
 1193        *         the second given double.
 1194        * 
 1195        * @since 1.6
 1196        */
 1197       @SuppressWarnings("boxing")
 1198       public static float nextAfter(float start, double direction) {
 1199           if (Float.isNaN(start) || Double.isNaN(direction)) {
 1200               return Float.NaN;
 1201           }
 1202           if (0 == start && 0 == direction) {       	
 1203               return new Float(direction);
 1204           }
 1205           if ((start == Float.MIN_VALUE && direction < start)
 1206                   || (start == -Float.MIN_VALUE && direction > start)) {
 1207               return (start > 0 ? 0f : -0f);
 1208           }
 1209           if (Float.isInfinite(start) && (direction != start)) {
 1210               return (start > 0 ? Float.MAX_VALUE : -Float.MAX_VALUE);
 1211           }
 1212           if ((start == Float.MAX_VALUE && direction > start)
 1213                   || (start == -Float.MAX_VALUE && direction < start)) {
 1214               return (start > 0 ? Float.POSITIVE_INFINITY
 1215                       : Float.NEGATIVE_INFINITY);
 1216           }
 1217           if (direction > start) {
 1218               if (start > 0) {
 1219                   return Float.intBitsToFloat(Float.floatToIntBits(start) + 1);
 1220               }
 1221               if (start < 0) {
 1222                   return Float.intBitsToFloat(Float.floatToIntBits(start) - 1);
 1223               }
 1224               return +Float.MIN_VALUE;
 1225           }
 1226           if (direction < start) {
 1227               if (start > 0) {
 1228                   return Float.intBitsToFloat(Float.floatToIntBits(start) - 1);
 1229               }
 1230               if (start < 0) {
 1231                   return Float.intBitsToFloat(Float.floatToIntBits(start) + 1);
 1232               }
 1233               return -Float.MIN_VALUE;
 1234           }
 1235           return new Float(direction);
 1236       }
 1237       
 1238       /**
 1239        * Answers the next larger double value to d.
 1240        * 
 1241        * @param d
 1242        *            the double value to start
 1243        * @return the next larger double value of d.
 1244        * 
 1245        * @since 1.6
 1246        */
 1247       public static double nextUp(double d) {
 1248           if (Double.isNaN(d)) {
 1249               return Double.NaN;
 1250           }
 1251           if ((d == Double.POSITIVE_INFINITY)) {
 1252               return Double.POSITIVE_INFINITY;
 1253           }
 1254           if (0 == d) {
 1255               return Double.MIN_VALUE;
 1256           } else if (0 < d) {
 1257               return Double.longBitsToDouble(Double.doubleToLongBits(d) + 1);
 1258           } else {
 1259               return Double.longBitsToDouble(Double.doubleToLongBits(d) - 1);
 1260           }
 1261       }
 1262   
 1263       /**
 1264        * Answers the next larger float value to d.
 1265        * 
 1266        * @param f
 1267        *            the float value to start
 1268        * @return the next larger float value of d.
 1269        * 
 1270        * @since 1.6
 1271        */
 1272       public static float nextUp(float f) {
 1273           if (Float.isNaN(f)) {
 1274               return Float.NaN;
 1275           }
 1276           if ((f == Float.POSITIVE_INFINITY)) {
 1277               return Float.POSITIVE_INFINITY;
 1278           }
 1279           if (0 == f) {
 1280               return Float.MIN_VALUE;
 1281           } else if (0 < f) {
 1282               return Float.intBitsToFloat(Float.floatToIntBits(f) + 1);
 1283           } else {
 1284               return Float.intBitsToFloat(Float.floatToIntBits(f) - 1);
 1285           }
 1286       }
 1287       
 1288       /**
 1289        * Answers a double value of d * 2^scaleFactor, the result may be rounded.
 1290        * 
 1291        * @param d
 1292        *            the base number
 1293        * @param scaleFactor
 1294        *            the power number
 1295        * @return d * 2^scaleFactor
 1296        * 
 1297        * @since 1.6
 1298        */
 1299       @SuppressWarnings("boxing")
 1300       public static double scalb(double d, int scaleFactor) {
 1301           if (Double.isNaN(d) || Double.isInfinite(d) || 0 == d) {
 1302               return d;
 1303           }
 1304           // change double to long for calculation
 1305           long bits = Double.doubleToLongBits(d);
 1306           // the sign of the results must be the same of given d
 1307           long sign = bits & DOUBLE_SIGN_MASK;
 1308           // calculates the factor of the result
 1309           long factor = ((bits & DOUBLE_EXPONENT_MASK) >> DOUBLE_MANTISSA_BITS)
 1310                   - DOUBLE_EXPONENT_BIAS + scaleFactor;
 1311   
 1312           // calcutes the factor of sub-normal values
 1313           int subNormalFactor = Long.numberOfLeadingZeros(bits
 1314                   & ~DOUBLE_SIGN_MASK)
 1315                   - DOUBLE_NON_MANTISSA_BITS;
 1316           if (subNormalFactor < 0) {
 1317               // not sub-normal values
 1318               subNormalFactor = 0;
 1319           } else {
 1320               factor = factor - subNormalFactor;
 1321           }
 1322           if (factor > Double.MAX_EXPONENT) {
 1323               return (d > 0 ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY);
 1324           }
 1325   
 1326           long result;
 1327           // if result is a sub-normal
 1328           if (factor <= -DOUBLE_EXPONENT_BIAS) {
 1329               // the number of digits that shifts
 1330               long digits = factor + DOUBLE_EXPONENT_BIAS + subNormalFactor;
 1331               if (Math.abs(d) < Double.MIN_NORMAL) {
 1332                   // origin d is already sub-normal
 1333                   result = shiftLongBits(bits & DOUBLE_MANTISSA_MASK, digits);
 1334               } else {
 1335                   // origin d is not sub-normal, change mantissa to sub-normal
 1336                   result = shiftLongBits(bits & DOUBLE_MANTISSA_MASK
 1337                           | 0x0010000000000000L, digits - 1);
 1338               }
 1339           } else {
 1340               if (Math.abs(d) >= Double.MIN_NORMAL) {
 1341                   // common situation
 1342                   result = ((factor + DOUBLE_EXPONENT_BIAS) << DOUBLE_MANTISSA_BITS)
 1343                           | (bits & DOUBLE_MANTISSA_MASK);
 1344               } else {
 1345                   // origin d is sub-normal, change mantissa to normal style
 1346                   result = ((factor + DOUBLE_EXPONENT_BIAS) << DOUBLE_MANTISSA_BITS)
 1347                           | ((bits << (subNormalFactor + 1)) & DOUBLE_MANTISSA_MASK);
 1348               }
 1349           }
 1350           return Double.longBitsToDouble(result | sign);
 1351       }
 1352   
 1353       /**
 1354        * Answers a float value of d * 2^scaleFactor, the result may be rounded.
 1355        * 
 1356        * @param d
 1357        *            the base number
 1358        * @param scaleFactor
 1359        *            the power number
 1360        * @return d * 2^scaleFactor
 1361        * 
 1362        * @since 1.6
 1363        */
 1364       public static float scalb(float d, int scaleFactor) {
 1365           if (Float.isNaN(d) || Float.isInfinite(d) || 0 == d) {
 1366               return d;
 1367           }
 1368           int bits = Float.floatToIntBits(d);
 1369           int sign = bits & FLOAT_SIGN_MASK;
 1370           int factor = ((bits & FLOAT_EXPONENT_MASK) >> FLOAT_MANTISSA_BITS)
 1371                   - FLOAT_EXPONENT_BIAS + scaleFactor;
 1372           // calcutes the factor of sub-normal values
 1373           int subNormalFactor = Integer.numberOfLeadingZeros(bits
 1374                   & ~FLOAT_SIGN_MASK)
 1375                   - FLOAT_NON_MANTISSA_BITS;
 1376           if (subNormalFactor < 0) {
 1377               // not sub-normal values
 1378               subNormalFactor = 0;
 1379           } else {
 1380               factor = factor - subNormalFactor;
 1381           }
 1382           if (factor > Float.MAX_EXPONENT) {
 1383               return (d > 0 ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY);
 1384           }
 1385   
 1386           int result;
 1387           // if result is a sub-normal
 1388           if (factor <= -FLOAT_EXPONENT_BIAS) {
 1389               // the number of digits that shifts
 1390               int digits = factor + FLOAT_EXPONENT_BIAS + subNormalFactor;
 1391               if (Math.abs(d) < Float.MIN_NORMAL) {
 1392                   // origin d is already sub-normal
 1393                   result = shiftIntBits(bits & FLOAT_MANTISSA_MASK, digits);
 1394               } else {
 1395                   // origin d is not sub-normal, change mantissa to sub-normal
 1396                   result = shiftIntBits(bits & FLOAT_MANTISSA_MASK | 0x00800000,
 1397                           digits - 1);
 1398               }
 1399           } else {
 1400               if (Math.abs(d) >= Float.MIN_NORMAL) {
 1401                   // common situation
 1402                   result = ((factor + FLOAT_EXPONENT_BIAS) << FLOAT_MANTISSA_BITS)
 1403                           | (bits & FLOAT_MANTISSA_MASK);
 1404               } else {
 1405                   // origin d is sub-normal, change mantissa to normal style
 1406                   result = ((factor + FLOAT_EXPONENT_BIAS) << FLOAT_MANTISSA_BITS)
 1407                           | ((bits << (subNormalFactor + 1)) & FLOAT_MANTISSA_MASK);
 1408               }
 1409           }
 1410           return Float.intBitsToFloat(result | sign);
 1411       }
 1412   
 1413       // Shifts integer bits as float, if the digits is positive, left-shift; if
 1414       // not, shift to right and calculate its carry.
 1415       private static int shiftIntBits(int bits, int digits) {
 1416           if (digits > 0) {
 1417               return bits << digits;
 1418           }
 1419           // change it to positive
 1420           int absdigits = -digits;
 1421           if (!(Integer.numberOfLeadingZeros(bits & ~FLOAT_SIGN_MASK) <= (32 - absdigits))) {
 1422               return 0;
 1423           }
 1424           int ret = bits >> absdigits;
 1425           boolean halfbit = ((bits >> (absdigits - 1)) & 0x1) == 1;
 1426           if (halfbit) {
 1427               if (Integer.numberOfTrailingZeros(bits) < (absdigits - 1)) {
 1428                   ret = ret + 1;
 1429               }
 1430               if (Integer.numberOfTrailingZeros(bits) == (absdigits - 1)) {
 1431                   if ((ret & 0x1) == 1) {
 1432                       ret = ret + 1;
 1433                   }
 1434               }
 1435           }
 1436           return ret;
 1437       }
 1438   
 1439       // Shifts long bits as double, if the digits is positive, left-shift; if
 1440       // not, shift to right and calculate its carry.
 1441       private static long shiftLongBits(long bits, long digits) {
 1442           if (digits > 0) {
 1443               return bits << digits;
 1444           }
 1445           // change it to positive
 1446           long absdigits = -digits;
 1447           if (!(Long.numberOfLeadingZeros(bits & ~DOUBLE_SIGN_MASK) <= (64 - absdigits))) {
 1448               return 0;
 1449           }
 1450           long ret = bits >> absdigits;
 1451           boolean halfbit = ((bits >> (absdigits - 1)) & 0x1) == 1;
 1452           if (halfbit) {
 1453               // some bits will remain after shifting, calculates its carry
 1454               // subnormal
 1455               if (Long.numberOfTrailingZeros(bits) < (absdigits - 1)) {
 1456                   ret = ret + 1;
 1457               }
 1458               if (Long.numberOfTrailingZeros(bits) == (absdigits - 1)) {
 1459                   if ((ret & 0x1) == 1) {
 1460                       ret = ret + 1;
 1461                   }
 1462               }
 1463           }
 1464           return ret;
 1465       }
 1466   }

Save This Page
Home » apache-harmony-6.0-src-r917296-snapshot » java » lang » [javadoc | source]