emul/src/main/java/java/lang/StrictMath.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 29 Sep 2012 11:37:46 +0200
branchjdk7-b147
changeset 69 e4d7540b796a
child 84 d65b3a2fbfaf
permissions -rw-r--r--
Adding also strict math
jaroslav@69
     1
/*
jaroslav@69
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
jaroslav@69
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@69
     4
 *
jaroslav@69
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@69
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@69
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@69
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@69
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@69
    10
 *
jaroslav@69
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@69
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@69
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@69
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@69
    15
 * accompanied this code).
jaroslav@69
    16
 *
jaroslav@69
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@69
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@69
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@69
    20
 *
jaroslav@69
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@69
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@69
    23
 * questions.
jaroslav@69
    24
 */
jaroslav@69
    25
jaroslav@69
    26
package java.lang;
jaroslav@69
    27
import java.util.Random;
jaroslav@69
    28
import sun.misc.FpUtils;
jaroslav@69
    29
import sun.misc.DoubleConsts;
jaroslav@69
    30
jaroslav@69
    31
/**
jaroslav@69
    32
 * The class {@code StrictMath} contains methods for performing basic
jaroslav@69
    33
 * numeric operations such as the elementary exponential, logarithm,
jaroslav@69
    34
 * square root, and trigonometric functions.
jaroslav@69
    35
 *
jaroslav@69
    36
 * <p>To help ensure portability of Java programs, the definitions of
jaroslav@69
    37
 * some of the numeric functions in this package require that they
jaroslav@69
    38
 * produce the same results as certain published algorithms. These
jaroslav@69
    39
 * algorithms are available from the well-known network library
jaroslav@69
    40
 * {@code netlib} as the package "Freely Distributable Math
jaroslav@69
    41
 * Library," <a
jaroslav@69
    42
 * href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These
jaroslav@69
    43
 * algorithms, which are written in the C programming language, are
jaroslav@69
    44
 * then to be understood as executed with all floating-point
jaroslav@69
    45
 * operations following the rules of Java floating-point arithmetic.
jaroslav@69
    46
 *
jaroslav@69
    47
 * <p>The Java math library is defined with respect to
jaroslav@69
    48
 * {@code fdlibm} version 5.3. Where {@code fdlibm} provides
jaroslav@69
    49
 * more than one definition for a function (such as
jaroslav@69
    50
 * {@code acos}), use the "IEEE 754 core function" version
jaroslav@69
    51
 * (residing in a file whose name begins with the letter
jaroslav@69
    52
 * {@code e}).  The methods which require {@code fdlibm}
jaroslav@69
    53
 * semantics are {@code sin}, {@code cos}, {@code tan},
jaroslav@69
    54
 * {@code asin}, {@code acos}, {@code atan},
jaroslav@69
    55
 * {@code exp}, {@code log}, {@code log10},
jaroslav@69
    56
 * {@code cbrt}, {@code atan2}, {@code pow},
jaroslav@69
    57
 * {@code sinh}, {@code cosh}, {@code tanh},
jaroslav@69
    58
 * {@code hypot}, {@code expm1}, and {@code log1p}.
jaroslav@69
    59
 *
jaroslav@69
    60
 * @author  unascribed
jaroslav@69
    61
 * @author  Joseph D. Darcy
jaroslav@69
    62
 * @since   1.3
jaroslav@69
    63
 */
jaroslav@69
    64
jaroslav@69
    65
public final class StrictMath {
jaroslav@69
    66
jaroslav@69
    67
    /**
jaroslav@69
    68
     * Don't let anyone instantiate this class.
jaroslav@69
    69
     */
jaroslav@69
    70
    private StrictMath() {}
jaroslav@69
    71
jaroslav@69
    72
    /**
jaroslav@69
    73
     * The {@code double} value that is closer than any other to
jaroslav@69
    74
     * <i>e</i>, the base of the natural logarithms.
jaroslav@69
    75
     */
jaroslav@69
    76
    public static final double E = 2.7182818284590452354;
jaroslav@69
    77
jaroslav@69
    78
    /**
jaroslav@69
    79
     * The {@code double} value that is closer than any other to
jaroslav@69
    80
     * <i>pi</i>, the ratio of the circumference of a circle to its
jaroslav@69
    81
     * diameter.
jaroslav@69
    82
     */
jaroslav@69
    83
    public static final double PI = 3.14159265358979323846;
jaroslav@69
    84
jaroslav@69
    85
    /**
jaroslav@69
    86
     * Returns the trigonometric sine of an angle. Special cases:
jaroslav@69
    87
     * <ul><li>If the argument is NaN or an infinity, then the
jaroslav@69
    88
     * result is NaN.
jaroslav@69
    89
     * <li>If the argument is zero, then the result is a zero with the
jaroslav@69
    90
     * same sign as the argument.</ul>
jaroslav@69
    91
     *
jaroslav@69
    92
     * @param   a   an angle, in radians.
jaroslav@69
    93
     * @return  the sine of the argument.
jaroslav@69
    94
     */
jaroslav@69
    95
    public static native double sin(double a);
jaroslav@69
    96
jaroslav@69
    97
    /**
jaroslav@69
    98
     * Returns the trigonometric cosine of an angle. Special cases:
jaroslav@69
    99
     * <ul><li>If the argument is NaN or an infinity, then the
jaroslav@69
   100
     * result is NaN.</ul>
jaroslav@69
   101
     *
jaroslav@69
   102
     * @param   a   an angle, in radians.
jaroslav@69
   103
     * @return  the cosine of the argument.
jaroslav@69
   104
     */
jaroslav@69
   105
    public static native double cos(double a);
jaroslav@69
   106
jaroslav@69
   107
    /**
jaroslav@69
   108
     * Returns the trigonometric tangent of an angle. Special cases:
jaroslav@69
   109
     * <ul><li>If the argument is NaN or an infinity, then the result
jaroslav@69
   110
     * is NaN.
jaroslav@69
   111
     * <li>If the argument is zero, then the result is a zero with the
jaroslav@69
   112
     * same sign as the argument.</ul>
jaroslav@69
   113
     *
jaroslav@69
   114
     * @param   a   an angle, in radians.
jaroslav@69
   115
     * @return  the tangent of the argument.
jaroslav@69
   116
     */
jaroslav@69
   117
    public static native double tan(double a);
jaroslav@69
   118
jaroslav@69
   119
    /**
jaroslav@69
   120
     * Returns the arc sine of a value; the returned angle is in the
jaroslav@69
   121
     * range -<i>pi</i>/2 through <i>pi</i>/2.  Special cases:
jaroslav@69
   122
     * <ul><li>If the argument is NaN or its absolute value is greater
jaroslav@69
   123
     * than 1, then the result is NaN.
jaroslav@69
   124
     * <li>If the argument is zero, then the result is a zero with the
jaroslav@69
   125
     * same sign as the argument.</ul>
jaroslav@69
   126
     *
jaroslav@69
   127
     * @param   a   the value whose arc sine is to be returned.
jaroslav@69
   128
     * @return  the arc sine of the argument.
jaroslav@69
   129
     */
jaroslav@69
   130
    public static native double asin(double a);
jaroslav@69
   131
jaroslav@69
   132
    /**
jaroslav@69
   133
     * Returns the arc cosine of a value; the returned angle is in the
jaroslav@69
   134
     * range 0.0 through <i>pi</i>.  Special case:
jaroslav@69
   135
     * <ul><li>If the argument is NaN or its absolute value is greater
jaroslav@69
   136
     * than 1, then the result is NaN.</ul>
jaroslav@69
   137
     *
jaroslav@69
   138
     * @param   a   the value whose arc cosine is to be returned.
jaroslav@69
   139
     * @return  the arc cosine of the argument.
jaroslav@69
   140
     */
jaroslav@69
   141
    public static native double acos(double a);
jaroslav@69
   142
jaroslav@69
   143
    /**
jaroslav@69
   144
     * Returns the arc tangent of a value; the returned angle is in the
jaroslav@69
   145
     * range -<i>pi</i>/2 through <i>pi</i>/2.  Special cases:
jaroslav@69
   146
     * <ul><li>If the argument is NaN, then the result is NaN.
jaroslav@69
   147
     * <li>If the argument is zero, then the result is a zero with the
jaroslav@69
   148
     * same sign as the argument.</ul>
jaroslav@69
   149
     *
jaroslav@69
   150
     * @param   a   the value whose arc tangent is to be returned.
jaroslav@69
   151
     * @return  the arc tangent of the argument.
jaroslav@69
   152
     */
jaroslav@69
   153
    public static native double atan(double a);
jaroslav@69
   154
jaroslav@69
   155
    /**
jaroslav@69
   156
     * Converts an angle measured in degrees to an approximately
jaroslav@69
   157
     * equivalent angle measured in radians.  The conversion from
jaroslav@69
   158
     * degrees to radians is generally inexact.
jaroslav@69
   159
     *
jaroslav@69
   160
     * @param   angdeg   an angle, in degrees
jaroslav@69
   161
     * @return  the measurement of the angle {@code angdeg}
jaroslav@69
   162
     *          in radians.
jaroslav@69
   163
     */
jaroslav@69
   164
    public static strictfp double toRadians(double angdeg) {
jaroslav@69
   165
        return angdeg / 180.0 * PI;
jaroslav@69
   166
    }
jaroslav@69
   167
jaroslav@69
   168
    /**
jaroslav@69
   169
     * Converts an angle measured in radians to an approximately
jaroslav@69
   170
     * equivalent angle measured in degrees.  The conversion from
jaroslav@69
   171
     * radians to degrees is generally inexact; users should
jaroslav@69
   172
     * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
jaroslav@69
   173
     * equal {@code 0.0}.
jaroslav@69
   174
     *
jaroslav@69
   175
     * @param   angrad   an angle, in radians
jaroslav@69
   176
     * @return  the measurement of the angle {@code angrad}
jaroslav@69
   177
     *          in degrees.
jaroslav@69
   178
     */
jaroslav@69
   179
    public static strictfp double toDegrees(double angrad) {
jaroslav@69
   180
        return angrad * 180.0 / PI;
jaroslav@69
   181
    }
jaroslav@69
   182
jaroslav@69
   183
    /**
jaroslav@69
   184
     * Returns Euler's number <i>e</i> raised to the power of a
jaroslav@69
   185
     * {@code double} value. Special cases:
jaroslav@69
   186
     * <ul><li>If the argument is NaN, the result is NaN.
jaroslav@69
   187
     * <li>If the argument is positive infinity, then the result is
jaroslav@69
   188
     * positive infinity.
jaroslav@69
   189
     * <li>If the argument is negative infinity, then the result is
jaroslav@69
   190
     * positive zero.</ul>
jaroslav@69
   191
     *
jaroslav@69
   192
     * @param   a   the exponent to raise <i>e</i> to.
jaroslav@69
   193
     * @return  the value <i>e</i><sup>{@code a}</sup>,
jaroslav@69
   194
     *          where <i>e</i> is the base of the natural logarithms.
jaroslav@69
   195
     */
jaroslav@69
   196
    public static native double exp(double a);
jaroslav@69
   197
jaroslav@69
   198
    /**
jaroslav@69
   199
     * Returns the natural logarithm (base <i>e</i>) of a {@code double}
jaroslav@69
   200
     * value. Special cases:
jaroslav@69
   201
     * <ul><li>If the argument is NaN or less than zero, then the result
jaroslav@69
   202
     * is NaN.
jaroslav@69
   203
     * <li>If the argument is positive infinity, then the result is
jaroslav@69
   204
     * positive infinity.
jaroslav@69
   205
     * <li>If the argument is positive zero or negative zero, then the
jaroslav@69
   206
     * result is negative infinity.</ul>
jaroslav@69
   207
     *
jaroslav@69
   208
     * @param   a   a value
jaroslav@69
   209
     * @return  the value ln&nbsp;{@code a}, the natural logarithm of
jaroslav@69
   210
     *          {@code a}.
jaroslav@69
   211
     */
jaroslav@69
   212
    public static native double log(double a);
jaroslav@69
   213
jaroslav@69
   214
jaroslav@69
   215
    /**
jaroslav@69
   216
     * Returns the base 10 logarithm of a {@code double} value.
jaroslav@69
   217
     * Special cases:
jaroslav@69
   218
     *
jaroslav@69
   219
     * <ul><li>If the argument is NaN or less than zero, then the result
jaroslav@69
   220
     * is NaN.
jaroslav@69
   221
     * <li>If the argument is positive infinity, then the result is
jaroslav@69
   222
     * positive infinity.
jaroslav@69
   223
     * <li>If the argument is positive zero or negative zero, then the
jaroslav@69
   224
     * result is negative infinity.
jaroslav@69
   225
     * <li> If the argument is equal to 10<sup><i>n</i></sup> for
jaroslav@69
   226
     * integer <i>n</i>, then the result is <i>n</i>.
jaroslav@69
   227
     * </ul>
jaroslav@69
   228
     *
jaroslav@69
   229
     * @param   a   a value
jaroslav@69
   230
     * @return  the base 10 logarithm of  {@code a}.
jaroslav@69
   231
     * @since 1.5
jaroslav@69
   232
     */
jaroslav@69
   233
    public static native double log10(double a);
jaroslav@69
   234
jaroslav@69
   235
    /**
jaroslav@69
   236
     * Returns the correctly rounded positive square root of a
jaroslav@69
   237
     * {@code double} value.
jaroslav@69
   238
     * Special cases:
jaroslav@69
   239
     * <ul><li>If the argument is NaN or less than zero, then the result
jaroslav@69
   240
     * is NaN.
jaroslav@69
   241
     * <li>If the argument is positive infinity, then the result is positive
jaroslav@69
   242
     * infinity.
jaroslav@69
   243
     * <li>If the argument is positive zero or negative zero, then the
jaroslav@69
   244
     * result is the same as the argument.</ul>
jaroslav@69
   245
     * Otherwise, the result is the {@code double} value closest to
jaroslav@69
   246
     * the true mathematical square root of the argument value.
jaroslav@69
   247
     *
jaroslav@69
   248
     * @param   a   a value.
jaroslav@69
   249
     * @return  the positive square root of {@code a}.
jaroslav@69
   250
     */
jaroslav@69
   251
    public static native double sqrt(double a);
jaroslav@69
   252
jaroslav@69
   253
    /**
jaroslav@69
   254
     * Returns the cube root of a {@code double} value.  For
jaroslav@69
   255
     * positive finite {@code x}, {@code cbrt(-x) ==
jaroslav@69
   256
     * -cbrt(x)}; that is, the cube root of a negative value is
jaroslav@69
   257
     * the negative of the cube root of that value's magnitude.
jaroslav@69
   258
     * Special cases:
jaroslav@69
   259
     *
jaroslav@69
   260
     * <ul>
jaroslav@69
   261
     *
jaroslav@69
   262
     * <li>If the argument is NaN, then the result is NaN.
jaroslav@69
   263
     *
jaroslav@69
   264
     * <li>If the argument is infinite, then the result is an infinity
jaroslav@69
   265
     * with the same sign as the argument.
jaroslav@69
   266
     *
jaroslav@69
   267
     * <li>If the argument is zero, then the result is a zero with the
jaroslav@69
   268
     * same sign as the argument.
jaroslav@69
   269
     *
jaroslav@69
   270
     * </ul>
jaroslav@69
   271
     *
jaroslav@69
   272
     * @param   a   a value.
jaroslav@69
   273
     * @return  the cube root of {@code a}.
jaroslav@69
   274
     * @since 1.5
jaroslav@69
   275
     */
jaroslav@69
   276
    public static native double cbrt(double a);
jaroslav@69
   277
jaroslav@69
   278
    /**
jaroslav@69
   279
     * Computes the remainder operation on two arguments as prescribed
jaroslav@69
   280
     * by the IEEE 754 standard.
jaroslav@69
   281
     * The remainder value is mathematically equal to
jaroslav@69
   282
     * <code>f1&nbsp;-&nbsp;f2</code>&nbsp;&times;&nbsp;<i>n</i>,
jaroslav@69
   283
     * where <i>n</i> is the mathematical integer closest to the exact
jaroslav@69
   284
     * mathematical value of the quotient {@code f1/f2}, and if two
jaroslav@69
   285
     * mathematical integers are equally close to {@code f1/f2},
jaroslav@69
   286
     * then <i>n</i> is the integer that is even. If the remainder is
jaroslav@69
   287
     * zero, its sign is the same as the sign of the first argument.
jaroslav@69
   288
     * Special cases:
jaroslav@69
   289
     * <ul><li>If either argument is NaN, or the first argument is infinite,
jaroslav@69
   290
     * or the second argument is positive zero or negative zero, then the
jaroslav@69
   291
     * result is NaN.
jaroslav@69
   292
     * <li>If the first argument is finite and the second argument is
jaroslav@69
   293
     * infinite, then the result is the same as the first argument.</ul>
jaroslav@69
   294
     *
jaroslav@69
   295
     * @param   f1   the dividend.
jaroslav@69
   296
     * @param   f2   the divisor.
jaroslav@69
   297
     * @return  the remainder when {@code f1} is divided by
jaroslav@69
   298
     *          {@code f2}.
jaroslav@69
   299
     */
jaroslav@69
   300
    public static native double IEEEremainder(double f1, double f2);
jaroslav@69
   301
jaroslav@69
   302
    /**
jaroslav@69
   303
     * Returns the smallest (closest to negative infinity)
jaroslav@69
   304
     * {@code double} value that is greater than or equal to the
jaroslav@69
   305
     * argument and is equal to a mathematical integer. Special cases:
jaroslav@69
   306
     * <ul><li>If the argument value is already equal to a
jaroslav@69
   307
     * mathematical integer, then the result is the same as the
jaroslav@69
   308
     * argument.  <li>If the argument is NaN or an infinity or
jaroslav@69
   309
     * positive zero or negative zero, then the result is the same as
jaroslav@69
   310
     * the argument.  <li>If the argument value is less than zero but
jaroslav@69
   311
     * greater than -1.0, then the result is negative zero.</ul> Note
jaroslav@69
   312
     * that the value of {@code StrictMath.ceil(x)} is exactly the
jaroslav@69
   313
     * value of {@code -StrictMath.floor(-x)}.
jaroslav@69
   314
     *
jaroslav@69
   315
     * @param   a   a value.
jaroslav@69
   316
     * @return  the smallest (closest to negative infinity)
jaroslav@69
   317
     *          floating-point value that is greater than or equal to
jaroslav@69
   318
     *          the argument and is equal to a mathematical integer.
jaroslav@69
   319
     */
jaroslav@69
   320
    public static double ceil(double a) {
jaroslav@69
   321
        return floorOrCeil(a, -0.0, 1.0, 1.0);
jaroslav@69
   322
    }
jaroslav@69
   323
jaroslav@69
   324
    /**
jaroslav@69
   325
     * Returns the largest (closest to positive infinity)
jaroslav@69
   326
     * {@code double} value that is less than or equal to the
jaroslav@69
   327
     * argument and is equal to a mathematical integer. Special cases:
jaroslav@69
   328
     * <ul><li>If the argument value is already equal to a
jaroslav@69
   329
     * mathematical integer, then the result is the same as the
jaroslav@69
   330
     * argument.  <li>If the argument is NaN or an infinity or
jaroslav@69
   331
     * positive zero or negative zero, then the result is the same as
jaroslav@69
   332
     * the argument.</ul>
jaroslav@69
   333
     *
jaroslav@69
   334
     * @param   a   a value.
jaroslav@69
   335
     * @return  the largest (closest to positive infinity)
jaroslav@69
   336
     *          floating-point value that less than or equal to the argument
jaroslav@69
   337
     *          and is equal to a mathematical integer.
jaroslav@69
   338
     */
jaroslav@69
   339
    public static double floor(double a) {
jaroslav@69
   340
        return floorOrCeil(a, -1.0, 0.0, -1.0);
jaroslav@69
   341
    }
jaroslav@69
   342
jaroslav@69
   343
    /**
jaroslav@69
   344
     * Internal method to share logic between floor and ceil.
jaroslav@69
   345
     *
jaroslav@69
   346
     * @param a the value to be floored or ceiled
jaroslav@69
   347
     * @param negativeBoundary result for values in (-1, 0)
jaroslav@69
   348
     * @param positiveBoundary result for values in (0, 1)
jaroslav@69
   349
     * @param increment value to add when the argument is non-integral
jaroslav@69
   350
     */
jaroslav@69
   351
    private static double floorOrCeil(double a,
jaroslav@69
   352
                                      double negativeBoundary,
jaroslav@69
   353
                                      double positiveBoundary,
jaroslav@69
   354
                                      double sign) {
jaroslav@69
   355
        int exponent = Math.getExponent(a);
jaroslav@69
   356
jaroslav@69
   357
        if (exponent < 0) {
jaroslav@69
   358
            /*
jaroslav@69
   359
             * Absolute value of argument is less than 1.
jaroslav@69
   360
             * floorOrceil(-0.0) => -0.0
jaroslav@69
   361
             * floorOrceil(+0.0) => +0.0
jaroslav@69
   362
             */
jaroslav@69
   363
            return ((a == 0.0) ? a :
jaroslav@69
   364
                    ( (a < 0.0) ?  negativeBoundary : positiveBoundary) );
jaroslav@69
   365
        } else if (exponent >= 52) {
jaroslav@69
   366
            /*
jaroslav@69
   367
             * Infinity, NaN, or a value so large it must be integral.
jaroslav@69
   368
             */
jaroslav@69
   369
            return a;
jaroslav@69
   370
        }
jaroslav@69
   371
        // Else the argument is either an integral value already XOR it
jaroslav@69
   372
        // has to be rounded to one.
jaroslav@69
   373
        assert exponent >= 0 && exponent <= 51;
jaroslav@69
   374
jaroslav@69
   375
        long doppel = Double.doubleToRawLongBits(a);
jaroslav@69
   376
        long mask   = DoubleConsts.SIGNIF_BIT_MASK >> exponent;
jaroslav@69
   377
jaroslav@69
   378
        if ( (mask & doppel) == 0L )
jaroslav@69
   379
            return a; // integral value
jaroslav@69
   380
        else {
jaroslav@69
   381
            double result = Double.longBitsToDouble(doppel & (~mask));
jaroslav@69
   382
            if (sign*a > 0.0)
jaroslav@69
   383
                result = result + sign;
jaroslav@69
   384
            return result;
jaroslav@69
   385
        }
jaroslav@69
   386
    }
jaroslav@69
   387
jaroslav@69
   388
    /**
jaroslav@69
   389
     * Returns the {@code double} value that is closest in value
jaroslav@69
   390
     * to the argument and is equal to a mathematical integer. If two
jaroslav@69
   391
     * {@code double} values that are mathematical integers are
jaroslav@69
   392
     * equally close to the value of the argument, the result is the
jaroslav@69
   393
     * integer value that is even. Special cases:
jaroslav@69
   394
     * <ul><li>If the argument value is already equal to a mathematical
jaroslav@69
   395
     * integer, then the result is the same as the argument.
jaroslav@69
   396
     * <li>If the argument is NaN or an infinity or positive zero or negative
jaroslav@69
   397
     * zero, then the result is the same as the argument.</ul>
jaroslav@69
   398
     *
jaroslav@69
   399
     * @param   a   a value.
jaroslav@69
   400
     * @return  the closest floating-point value to {@code a} that is
jaroslav@69
   401
     *          equal to a mathematical integer.
jaroslav@69
   402
     * @author Joseph D. Darcy
jaroslav@69
   403
     */
jaroslav@69
   404
    public static double rint(double a) {
jaroslav@69
   405
        /*
jaroslav@69
   406
         * If the absolute value of a is not less than 2^52, it
jaroslav@69
   407
         * is either a finite integer (the double format does not have
jaroslav@69
   408
         * enough significand bits for a number that large to have any
jaroslav@69
   409
         * fractional portion), an infinity, or a NaN.  In any of
jaroslav@69
   410
         * these cases, rint of the argument is the argument.
jaroslav@69
   411
         *
jaroslav@69
   412
         * Otherwise, the sum (twoToThe52 + a ) will properly round
jaroslav@69
   413
         * away any fractional portion of a since ulp(twoToThe52) ==
jaroslav@69
   414
         * 1.0; subtracting out twoToThe52 from this sum will then be
jaroslav@69
   415
         * exact and leave the rounded integer portion of a.
jaroslav@69
   416
         *
jaroslav@69
   417
         * This method does *not* need to be declared strictfp to get
jaroslav@69
   418
         * fully reproducible results.  Whether or not a method is
jaroslav@69
   419
         * declared strictfp can only make a difference in the
jaroslav@69
   420
         * returned result if some operation would overflow or
jaroslav@69
   421
         * underflow with strictfp semantics.  The operation
jaroslav@69
   422
         * (twoToThe52 + a ) cannot overflow since large values of a
jaroslav@69
   423
         * are screened out; the add cannot underflow since twoToThe52
jaroslav@69
   424
         * is too large.  The subtraction ((twoToThe52 + a ) -
jaroslav@69
   425
         * twoToThe52) will be exact as discussed above and thus
jaroslav@69
   426
         * cannot overflow or meaningfully underflow.  Finally, the
jaroslav@69
   427
         * last multiply in the return statement is by plus or minus
jaroslav@69
   428
         * 1.0, which is exact too.
jaroslav@69
   429
         */
jaroslav@69
   430
        double twoToThe52 = (double)(1L << 52); // 2^52
jaroslav@69
   431
        double sign = FpUtils.rawCopySign(1.0, a); // preserve sign info
jaroslav@69
   432
        a = Math.abs(a);
jaroslav@69
   433
jaroslav@69
   434
        if (a < twoToThe52) { // E_min <= ilogb(a) <= 51
jaroslav@69
   435
            a = ((twoToThe52 + a ) - twoToThe52);
jaroslav@69
   436
        }
jaroslav@69
   437
jaroslav@69
   438
        return sign * a; // restore original sign
jaroslav@69
   439
    }
jaroslav@69
   440
jaroslav@69
   441
    /**
jaroslav@69
   442
     * Returns the angle <i>theta</i> from the conversion of rectangular
jaroslav@69
   443
     * coordinates ({@code x},&nbsp;{@code y}) to polar
jaroslav@69
   444
     * coordinates (r,&nbsp;<i>theta</i>).
jaroslav@69
   445
     * This method computes the phase <i>theta</i> by computing an arc tangent
jaroslav@69
   446
     * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
jaroslav@69
   447
     * cases:
jaroslav@69
   448
     * <ul><li>If either argument is NaN, then the result is NaN.
jaroslav@69
   449
     * <li>If the first argument is positive zero and the second argument
jaroslav@69
   450
     * is positive, or the first argument is positive and finite and the
jaroslav@69
   451
     * second argument is positive infinity, then the result is positive
jaroslav@69
   452
     * zero.
jaroslav@69
   453
     * <li>If the first argument is negative zero and the second argument
jaroslav@69
   454
     * is positive, or the first argument is negative and finite and the
jaroslav@69
   455
     * second argument is positive infinity, then the result is negative zero.
jaroslav@69
   456
     * <li>If the first argument is positive zero and the second argument
jaroslav@69
   457
     * is negative, or the first argument is positive and finite and the
jaroslav@69
   458
     * second argument is negative infinity, then the result is the
jaroslav@69
   459
     * {@code double} value closest to <i>pi</i>.
jaroslav@69
   460
     * <li>If the first argument is negative zero and the second argument
jaroslav@69
   461
     * is negative, or the first argument is negative and finite and the
jaroslav@69
   462
     * second argument is negative infinity, then the result is the
jaroslav@69
   463
     * {@code double} value closest to -<i>pi</i>.
jaroslav@69
   464
     * <li>If the first argument is positive and the second argument is
jaroslav@69
   465
     * positive zero or negative zero, or the first argument is positive
jaroslav@69
   466
     * infinity and the second argument is finite, then the result is the
jaroslav@69
   467
     * {@code double} value closest to <i>pi</i>/2.
jaroslav@69
   468
     * <li>If the first argument is negative and the second argument is
jaroslav@69
   469
     * positive zero or negative zero, or the first argument is negative
jaroslav@69
   470
     * infinity and the second argument is finite, then the result is the
jaroslav@69
   471
     * {@code double} value closest to -<i>pi</i>/2.
jaroslav@69
   472
     * <li>If both arguments are positive infinity, then the result is the
jaroslav@69
   473
     * {@code double} value closest to <i>pi</i>/4.
jaroslav@69
   474
     * <li>If the first argument is positive infinity and the second argument
jaroslav@69
   475
     * is negative infinity, then the result is the {@code double}
jaroslav@69
   476
     * value closest to 3*<i>pi</i>/4.
jaroslav@69
   477
     * <li>If the first argument is negative infinity and the second argument
jaroslav@69
   478
     * is positive infinity, then the result is the {@code double} value
jaroslav@69
   479
     * closest to -<i>pi</i>/4.
jaroslav@69
   480
     * <li>If both arguments are negative infinity, then the result is the
jaroslav@69
   481
     * {@code double} value closest to -3*<i>pi</i>/4.</ul>
jaroslav@69
   482
     *
jaroslav@69
   483
     * @param   y   the ordinate coordinate
jaroslav@69
   484
     * @param   x   the abscissa coordinate
jaroslav@69
   485
     * @return  the <i>theta</i> component of the point
jaroslav@69
   486
     *          (<i>r</i>,&nbsp;<i>theta</i>)
jaroslav@69
   487
     *          in polar coordinates that corresponds to the point
jaroslav@69
   488
     *          (<i>x</i>,&nbsp;<i>y</i>) in Cartesian coordinates.
jaroslav@69
   489
     */
jaroslav@69
   490
    public static native double atan2(double y, double x);
jaroslav@69
   491
jaroslav@69
   492
jaroslav@69
   493
    /**
jaroslav@69
   494
     * Returns the value of the first argument raised to the power of the
jaroslav@69
   495
     * second argument. Special cases:
jaroslav@69
   496
     *
jaroslav@69
   497
     * <ul><li>If the second argument is positive or negative zero, then the
jaroslav@69
   498
     * result is 1.0.
jaroslav@69
   499
     * <li>If the second argument is 1.0, then the result is the same as the
jaroslav@69
   500
     * first argument.
jaroslav@69
   501
     * <li>If the second argument is NaN, then the result is NaN.
jaroslav@69
   502
     * <li>If the first argument is NaN and the second argument is nonzero,
jaroslav@69
   503
     * then the result is NaN.
jaroslav@69
   504
     *
jaroslav@69
   505
     * <li>If
jaroslav@69
   506
     * <ul>
jaroslav@69
   507
     * <li>the absolute value of the first argument is greater than 1
jaroslav@69
   508
     * and the second argument is positive infinity, or
jaroslav@69
   509
     * <li>the absolute value of the first argument is less than 1 and
jaroslav@69
   510
     * the second argument is negative infinity,
jaroslav@69
   511
     * </ul>
jaroslav@69
   512
     * then the result is positive infinity.
jaroslav@69
   513
     *
jaroslav@69
   514
     * <li>If
jaroslav@69
   515
     * <ul>
jaroslav@69
   516
     * <li>the absolute value of the first argument is greater than 1 and
jaroslav@69
   517
     * the second argument is negative infinity, or
jaroslav@69
   518
     * <li>the absolute value of the
jaroslav@69
   519
     * first argument is less than 1 and the second argument is positive
jaroslav@69
   520
     * infinity,
jaroslav@69
   521
     * </ul>
jaroslav@69
   522
     * then the result is positive zero.
jaroslav@69
   523
     *
jaroslav@69
   524
     * <li>If the absolute value of the first argument equals 1 and the
jaroslav@69
   525
     * second argument is infinite, then the result is NaN.
jaroslav@69
   526
     *
jaroslav@69
   527
     * <li>If
jaroslav@69
   528
     * <ul>
jaroslav@69
   529
     * <li>the first argument is positive zero and the second argument
jaroslav@69
   530
     * is greater than zero, or
jaroslav@69
   531
     * <li>the first argument is positive infinity and the second
jaroslav@69
   532
     * argument is less than zero,
jaroslav@69
   533
     * </ul>
jaroslav@69
   534
     * then the result is positive zero.
jaroslav@69
   535
     *
jaroslav@69
   536
     * <li>If
jaroslav@69
   537
     * <ul>
jaroslav@69
   538
     * <li>the first argument is positive zero and the second argument
jaroslav@69
   539
     * is less than zero, or
jaroslav@69
   540
     * <li>the first argument is positive infinity and the second
jaroslav@69
   541
     * argument is greater than zero,
jaroslav@69
   542
     * </ul>
jaroslav@69
   543
     * then the result is positive infinity.
jaroslav@69
   544
     *
jaroslav@69
   545
     * <li>If
jaroslav@69
   546
     * <ul>
jaroslav@69
   547
     * <li>the first argument is negative zero and the second argument
jaroslav@69
   548
     * is greater than zero but not a finite odd integer, or
jaroslav@69
   549
     * <li>the first argument is negative infinity and the second
jaroslav@69
   550
     * argument is less than zero but not a finite odd integer,
jaroslav@69
   551
     * </ul>
jaroslav@69
   552
     * then the result is positive zero.
jaroslav@69
   553
     *
jaroslav@69
   554
     * <li>If
jaroslav@69
   555
     * <ul>
jaroslav@69
   556
     * <li>the first argument is negative zero and the second argument
jaroslav@69
   557
     * is a positive finite odd integer, or
jaroslav@69
   558
     * <li>the first argument is negative infinity and the second
jaroslav@69
   559
     * argument is a negative finite odd integer,
jaroslav@69
   560
     * </ul>
jaroslav@69
   561
     * then the result is negative zero.
jaroslav@69
   562
     *
jaroslav@69
   563
     * <li>If
jaroslav@69
   564
     * <ul>
jaroslav@69
   565
     * <li>the first argument is negative zero and the second argument
jaroslav@69
   566
     * is less than zero but not a finite odd integer, or
jaroslav@69
   567
     * <li>the first argument is negative infinity and the second
jaroslav@69
   568
     * argument is greater than zero but not a finite odd integer,
jaroslav@69
   569
     * </ul>
jaroslav@69
   570
     * then the result is positive infinity.
jaroslav@69
   571
     *
jaroslav@69
   572
     * <li>If
jaroslav@69
   573
     * <ul>
jaroslav@69
   574
     * <li>the first argument is negative zero and the second argument
jaroslav@69
   575
     * is a negative finite odd integer, or
jaroslav@69
   576
     * <li>the first argument is negative infinity and the second
jaroslav@69
   577
     * argument is a positive finite odd integer,
jaroslav@69
   578
     * </ul>
jaroslav@69
   579
     * then the result is negative infinity.
jaroslav@69
   580
     *
jaroslav@69
   581
     * <li>If the first argument is finite and less than zero
jaroslav@69
   582
     * <ul>
jaroslav@69
   583
     * <li> if the second argument is a finite even integer, the
jaroslav@69
   584
     * result is equal to the result of raising the absolute value of
jaroslav@69
   585
     * the first argument to the power of the second argument
jaroslav@69
   586
     *
jaroslav@69
   587
     * <li>if the second argument is a finite odd integer, the result
jaroslav@69
   588
     * is equal to the negative of the result of raising the absolute
jaroslav@69
   589
     * value of the first argument to the power of the second
jaroslav@69
   590
     * argument
jaroslav@69
   591
     *
jaroslav@69
   592
     * <li>if the second argument is finite and not an integer, then
jaroslav@69
   593
     * the result is NaN.
jaroslav@69
   594
     * </ul>
jaroslav@69
   595
     *
jaroslav@69
   596
     * <li>If both arguments are integers, then the result is exactly equal
jaroslav@69
   597
     * to the mathematical result of raising the first argument to the power
jaroslav@69
   598
     * of the second argument if that result can in fact be represented
jaroslav@69
   599
     * exactly as a {@code double} value.</ul>
jaroslav@69
   600
     *
jaroslav@69
   601
     * <p>(In the foregoing descriptions, a floating-point value is
jaroslav@69
   602
     * considered to be an integer if and only if it is finite and a
jaroslav@69
   603
     * fixed point of the method {@link #ceil ceil} or,
jaroslav@69
   604
     * equivalently, a fixed point of the method {@link #floor
jaroslav@69
   605
     * floor}. A value is a fixed point of a one-argument
jaroslav@69
   606
     * method if and only if the result of applying the method to the
jaroslav@69
   607
     * value is equal to the value.)
jaroslav@69
   608
     *
jaroslav@69
   609
     * @param   a   base.
jaroslav@69
   610
     * @param   b   the exponent.
jaroslav@69
   611
     * @return  the value {@code a}<sup>{@code b}</sup>.
jaroslav@69
   612
     */
jaroslav@69
   613
    public static native double pow(double a, double b);
jaroslav@69
   614
jaroslav@69
   615
    /**
jaroslav@69
   616
     * Returns the closest {@code int} to the argument, with ties
jaroslav@69
   617
     * rounding up.
jaroslav@69
   618
     *
jaroslav@69
   619
     * <p>Special cases:
jaroslav@69
   620
     * <ul><li>If the argument is NaN, the result is 0.
jaroslav@69
   621
     * <li>If the argument is negative infinity or any value less than or
jaroslav@69
   622
     * equal to the value of {@code Integer.MIN_VALUE}, the result is
jaroslav@69
   623
     * equal to the value of {@code Integer.MIN_VALUE}.
jaroslav@69
   624
     * <li>If the argument is positive infinity or any value greater than or
jaroslav@69
   625
     * equal to the value of {@code Integer.MAX_VALUE}, the result is
jaroslav@69
   626
     * equal to the value of {@code Integer.MAX_VALUE}.</ul>
jaroslav@69
   627
     *
jaroslav@69
   628
     * @param   a   a floating-point value to be rounded to an integer.
jaroslav@69
   629
     * @return  the value of the argument rounded to the nearest
jaroslav@69
   630
     *          {@code int} value.
jaroslav@69
   631
     * @see     java.lang.Integer#MAX_VALUE
jaroslav@69
   632
     * @see     java.lang.Integer#MIN_VALUE
jaroslav@69
   633
     */
jaroslav@69
   634
    public static int round(float a) {
jaroslav@69
   635
        return Math.round(a);
jaroslav@69
   636
    }
jaroslav@69
   637
jaroslav@69
   638
    /**
jaroslav@69
   639
     * Returns the closest {@code long} to the argument, with ties
jaroslav@69
   640
     * rounding up.
jaroslav@69
   641
     *
jaroslav@69
   642
     * <p>Special cases:
jaroslav@69
   643
     * <ul><li>If the argument is NaN, the result is 0.
jaroslav@69
   644
     * <li>If the argument is negative infinity or any value less than or
jaroslav@69
   645
     * equal to the value of {@code Long.MIN_VALUE}, the result is
jaroslav@69
   646
     * equal to the value of {@code Long.MIN_VALUE}.
jaroslav@69
   647
     * <li>If the argument is positive infinity or any value greater than or
jaroslav@69
   648
     * equal to the value of {@code Long.MAX_VALUE}, the result is
jaroslav@69
   649
     * equal to the value of {@code Long.MAX_VALUE}.</ul>
jaroslav@69
   650
     *
jaroslav@69
   651
     * @param   a  a floating-point value to be rounded to a
jaroslav@69
   652
     *          {@code long}.
jaroslav@69
   653
     * @return  the value of the argument rounded to the nearest
jaroslav@69
   654
     *          {@code long} value.
jaroslav@69
   655
     * @see     java.lang.Long#MAX_VALUE
jaroslav@69
   656
     * @see     java.lang.Long#MIN_VALUE
jaroslav@69
   657
     */
jaroslav@69
   658
    public static long round(double a) {
jaroslav@69
   659
        return Math.round(a);
jaroslav@69
   660
    }
jaroslav@69
   661
jaroslav@69
   662
    private static Random randomNumberGenerator;
jaroslav@69
   663
jaroslav@69
   664
    private static synchronized Random initRNG() {
jaroslav@69
   665
        Random rnd = randomNumberGenerator;
jaroslav@69
   666
        return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
jaroslav@69
   667
    }
jaroslav@69
   668
jaroslav@69
   669
    /**
jaroslav@69
   670
     * Returns a {@code double} value with a positive sign, greater
jaroslav@69
   671
     * than or equal to {@code 0.0} and less than {@code 1.0}.
jaroslav@69
   672
     * Returned values are chosen pseudorandomly with (approximately)
jaroslav@69
   673
     * uniform distribution from that range.
jaroslav@69
   674
     *
jaroslav@69
   675
     * <p>When this method is first called, it creates a single new
jaroslav@69
   676
     * pseudorandom-number generator, exactly as if by the expression
jaroslav@69
   677
     *
jaroslav@69
   678
     * <blockquote>{@code new java.util.Random()}</blockquote>
jaroslav@69
   679
     *
jaroslav@69
   680
     * This new pseudorandom-number generator is used thereafter for
jaroslav@69
   681
     * all calls to this method and is used nowhere else.
jaroslav@69
   682
     *
jaroslav@69
   683
     * <p>This method is properly synchronized to allow correct use by
jaroslav@69
   684
     * more than one thread. However, if many threads need to generate
jaroslav@69
   685
     * pseudorandom numbers at a great rate, it may reduce contention
jaroslav@69
   686
     * for each thread to have its own pseudorandom number generator.
jaroslav@69
   687
     *
jaroslav@69
   688
     * @return  a pseudorandom {@code double} greater than or equal
jaroslav@69
   689
     * to {@code 0.0} and less than {@code 1.0}.
jaroslav@69
   690
     * @see Random#nextDouble()
jaroslav@69
   691
     */
jaroslav@69
   692
    public static double random() {
jaroslav@69
   693
        Random rnd = randomNumberGenerator;
jaroslav@69
   694
        if (rnd == null) rnd = initRNG();
jaroslav@69
   695
        return rnd.nextDouble();
jaroslav@69
   696
    }
jaroslav@69
   697
jaroslav@69
   698
    /**
jaroslav@69
   699
     * Returns the absolute value of an {@code int} value..
jaroslav@69
   700
     * If the argument is not negative, the argument is returned.
jaroslav@69
   701
     * If the argument is negative, the negation of the argument is returned.
jaroslav@69
   702
     *
jaroslav@69
   703
     * <p>Note that if the argument is equal to the value of
jaroslav@69
   704
     * {@link Integer#MIN_VALUE}, the most negative representable
jaroslav@69
   705
     * {@code int} value, the result is that same value, which is
jaroslav@69
   706
     * negative.
jaroslav@69
   707
     *
jaroslav@69
   708
     * @param   a   the  argument whose absolute value is to be determined.
jaroslav@69
   709
     * @return  the absolute value of the argument.
jaroslav@69
   710
     */
jaroslav@69
   711
    public static int abs(int a) {
jaroslav@69
   712
        return (a < 0) ? -a : a;
jaroslav@69
   713
    }
jaroslav@69
   714
jaroslav@69
   715
    /**
jaroslav@69
   716
     * Returns the absolute value of a {@code long} value.
jaroslav@69
   717
     * If the argument is not negative, the argument is returned.
jaroslav@69
   718
     * If the argument is negative, the negation of the argument is returned.
jaroslav@69
   719
     *
jaroslav@69
   720
     * <p>Note that if the argument is equal to the value of
jaroslav@69
   721
     * {@link Long#MIN_VALUE}, the most negative representable
jaroslav@69
   722
     * {@code long} value, the result is that same value, which
jaroslav@69
   723
     * is negative.
jaroslav@69
   724
     *
jaroslav@69
   725
     * @param   a   the  argument whose absolute value is to be determined.
jaroslav@69
   726
     * @return  the absolute value of the argument.
jaroslav@69
   727
     */
jaroslav@69
   728
    public static long abs(long a) {
jaroslav@69
   729
        return (a < 0) ? -a : a;
jaroslav@69
   730
    }
jaroslav@69
   731
jaroslav@69
   732
    /**
jaroslav@69
   733
     * Returns the absolute value of a {@code float} value.
jaroslav@69
   734
     * If the argument is not negative, the argument is returned.
jaroslav@69
   735
     * If the argument is negative, the negation of the argument is returned.
jaroslav@69
   736
     * Special cases:
jaroslav@69
   737
     * <ul><li>If the argument is positive zero or negative zero, the
jaroslav@69
   738
     * result is positive zero.
jaroslav@69
   739
     * <li>If the argument is infinite, the result is positive infinity.
jaroslav@69
   740
     * <li>If the argument is NaN, the result is NaN.</ul>
jaroslav@69
   741
     * In other words, the result is the same as the value of the expression:
jaroslav@69
   742
     * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
jaroslav@69
   743
     *
jaroslav@69
   744
     * @param   a   the argument whose absolute value is to be determined
jaroslav@69
   745
     * @return  the absolute value of the argument.
jaroslav@69
   746
     */
jaroslav@69
   747
    public static float abs(float a) {
jaroslav@69
   748
        return (a <= 0.0F) ? 0.0F - a : a;
jaroslav@69
   749
    }
jaroslav@69
   750
jaroslav@69
   751
    /**
jaroslav@69
   752
     * Returns the absolute value of a {@code double} value.
jaroslav@69
   753
     * If the argument is not negative, the argument is returned.
jaroslav@69
   754
     * If the argument is negative, the negation of the argument is returned.
jaroslav@69
   755
     * Special cases:
jaroslav@69
   756
     * <ul><li>If the argument is positive zero or negative zero, the result
jaroslav@69
   757
     * is positive zero.
jaroslav@69
   758
     * <li>If the argument is infinite, the result is positive infinity.
jaroslav@69
   759
     * <li>If the argument is NaN, the result is NaN.</ul>
jaroslav@69
   760
     * In other words, the result is the same as the value of the expression:
jaroslav@69
   761
     * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
jaroslav@69
   762
     *
jaroslav@69
   763
     * @param   a   the argument whose absolute value is to be determined
jaroslav@69
   764
     * @return  the absolute value of the argument.
jaroslav@69
   765
     */
jaroslav@69
   766
    public static double abs(double a) {
jaroslav@69
   767
        return (a <= 0.0D) ? 0.0D - a : a;
jaroslav@69
   768
    }
jaroslav@69
   769
jaroslav@69
   770
    /**
jaroslav@69
   771
     * Returns the greater of two {@code int} values. That is, the
jaroslav@69
   772
     * result is the argument closer to the value of
jaroslav@69
   773
     * {@link Integer#MAX_VALUE}. If the arguments have the same value,
jaroslav@69
   774
     * the result is that same value.
jaroslav@69
   775
     *
jaroslav@69
   776
     * @param   a   an argument.
jaroslav@69
   777
     * @param   b   another argument.
jaroslav@69
   778
     * @return  the larger of {@code a} and {@code b}.
jaroslav@69
   779
     */
jaroslav@69
   780
    public static int max(int a, int b) {
jaroslav@69
   781
        return (a >= b) ? a : b;
jaroslav@69
   782
    }
jaroslav@69
   783
jaroslav@69
   784
    /**
jaroslav@69
   785
     * Returns the greater of two {@code long} values. That is, the
jaroslav@69
   786
     * result is the argument closer to the value of
jaroslav@69
   787
     * {@link Long#MAX_VALUE}. If the arguments have the same value,
jaroslav@69
   788
     * the result is that same value.
jaroslav@69
   789
     *
jaroslav@69
   790
     * @param   a   an argument.
jaroslav@69
   791
     * @param   b   another argument.
jaroslav@69
   792
     * @return  the larger of {@code a} and {@code b}.
jaroslav@69
   793
        */
jaroslav@69
   794
    public static long max(long a, long b) {
jaroslav@69
   795
        return (a >= b) ? a : b;
jaroslav@69
   796
    }
jaroslav@69
   797
jaroslav@69
   798
    // Use raw bit-wise conversions on guaranteed non-NaN arguments.
jaroslav@69
   799
    private static long negativeZeroFloatBits  = Float.floatToRawIntBits(-0.0f);
jaroslav@69
   800
    private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
jaroslav@69
   801
jaroslav@69
   802
    /**
jaroslav@69
   803
     * Returns the greater of two {@code float} values.  That is,
jaroslav@69
   804
     * the result is the argument closer to positive infinity. If the
jaroslav@69
   805
     * arguments have the same value, the result is that same
jaroslav@69
   806
     * value. If either value is NaN, then the result is NaN.  Unlike
jaroslav@69
   807
     * the numerical comparison operators, this method considers
jaroslav@69
   808
     * negative zero to be strictly smaller than positive zero. If one
jaroslav@69
   809
     * argument is positive zero and the other negative zero, the
jaroslav@69
   810
     * result is positive zero.
jaroslav@69
   811
     *
jaroslav@69
   812
     * @param   a   an argument.
jaroslav@69
   813
     * @param   b   another argument.
jaroslav@69
   814
     * @return  the larger of {@code a} and {@code b}.
jaroslav@69
   815
     */
jaroslav@69
   816
    public static float max(float a, float b) {
jaroslav@69
   817
        if (a != a)
jaroslav@69
   818
            return a;   // a is NaN
jaroslav@69
   819
        if ((a == 0.0f) &&
jaroslav@69
   820
            (b == 0.0f) &&
jaroslav@69
   821
            (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
jaroslav@69
   822
            // Raw conversion ok since NaN can't map to -0.0.
jaroslav@69
   823
            return b;
jaroslav@69
   824
        }
jaroslav@69
   825
        return (a >= b) ? a : b;
jaroslav@69
   826
    }
jaroslav@69
   827
jaroslav@69
   828
    /**
jaroslav@69
   829
     * Returns the greater of two {@code double} values.  That
jaroslav@69
   830
     * is, the result is the argument closer to positive infinity. If
jaroslav@69
   831
     * the arguments have the same value, the result is that same
jaroslav@69
   832
     * value. If either value is NaN, then the result is NaN.  Unlike
jaroslav@69
   833
     * the numerical comparison operators, this method considers
jaroslav@69
   834
     * negative zero to be strictly smaller than positive zero. If one
jaroslav@69
   835
     * argument is positive zero and the other negative zero, the
jaroslav@69
   836
     * result is positive zero.
jaroslav@69
   837
     *
jaroslav@69
   838
     * @param   a   an argument.
jaroslav@69
   839
     * @param   b   another argument.
jaroslav@69
   840
     * @return  the larger of {@code a} and {@code b}.
jaroslav@69
   841
     */
jaroslav@69
   842
    public static double max(double a, double b) {
jaroslav@69
   843
        if (a != a)
jaroslav@69
   844
            return a;   // a is NaN
jaroslav@69
   845
        if ((a == 0.0d) &&
jaroslav@69
   846
            (b == 0.0d) &&
jaroslav@69
   847
            (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
jaroslav@69
   848
            // Raw conversion ok since NaN can't map to -0.0.
jaroslav@69
   849
            return b;
jaroslav@69
   850
        }
jaroslav@69
   851
        return (a >= b) ? a : b;
jaroslav@69
   852
    }
jaroslav@69
   853
jaroslav@69
   854
    /**
jaroslav@69
   855
     * Returns the smaller of two {@code int} values. That is,
jaroslav@69
   856
     * the result the argument closer to the value of
jaroslav@69
   857
     * {@link Integer#MIN_VALUE}.  If the arguments have the same
jaroslav@69
   858
     * value, the result is that same value.
jaroslav@69
   859
     *
jaroslav@69
   860
     * @param   a   an argument.
jaroslav@69
   861
     * @param   b   another argument.
jaroslav@69
   862
     * @return  the smaller of {@code a} and {@code b}.
jaroslav@69
   863
     */
jaroslav@69
   864
    public static int min(int a, int b) {
jaroslav@69
   865
        return (a <= b) ? a : b;
jaroslav@69
   866
    }
jaroslav@69
   867
jaroslav@69
   868
    /**
jaroslav@69
   869
     * Returns the smaller of two {@code long} values. That is,
jaroslav@69
   870
     * the result is the argument closer to the value of
jaroslav@69
   871
     * {@link Long#MIN_VALUE}. If the arguments have the same
jaroslav@69
   872
     * value, the result is that same value.
jaroslav@69
   873
     *
jaroslav@69
   874
     * @param   a   an argument.
jaroslav@69
   875
     * @param   b   another argument.
jaroslav@69
   876
     * @return  the smaller of {@code a} and {@code b}.
jaroslav@69
   877
     */
jaroslav@69
   878
    public static long min(long a, long b) {
jaroslav@69
   879
        return (a <= b) ? a : b;
jaroslav@69
   880
    }
jaroslav@69
   881
jaroslav@69
   882
    /**
jaroslav@69
   883
     * Returns the smaller of two {@code float} values.  That is,
jaroslav@69
   884
     * the result is the value closer to negative infinity. If the
jaroslav@69
   885
     * arguments have the same value, the result is that same
jaroslav@69
   886
     * value. If either value is NaN, then the result is NaN.  Unlike
jaroslav@69
   887
     * the numerical comparison operators, this method considers
jaroslav@69
   888
     * negative zero to be strictly smaller than positive zero.  If
jaroslav@69
   889
     * one argument is positive zero and the other is negative zero,
jaroslav@69
   890
     * the result is negative zero.
jaroslav@69
   891
     *
jaroslav@69
   892
     * @param   a   an argument.
jaroslav@69
   893
     * @param   b   another argument.
jaroslav@69
   894
     * @return  the smaller of {@code a} and {@code b.}
jaroslav@69
   895
     */
jaroslav@69
   896
    public static float min(float a, float b) {
jaroslav@69
   897
        if (a != a)
jaroslav@69
   898
            return a;   // a is NaN
jaroslav@69
   899
        if ((a == 0.0f) &&
jaroslav@69
   900
            (b == 0.0f) &&
jaroslav@69
   901
            (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
jaroslav@69
   902
            // Raw conversion ok since NaN can't map to -0.0.
jaroslav@69
   903
            return b;
jaroslav@69
   904
        }
jaroslav@69
   905
        return (a <= b) ? a : b;
jaroslav@69
   906
    }
jaroslav@69
   907
jaroslav@69
   908
    /**
jaroslav@69
   909
     * Returns the smaller of two {@code double} values.  That
jaroslav@69
   910
     * is, the result is the value closer to negative infinity. If the
jaroslav@69
   911
     * arguments have the same value, the result is that same
jaroslav@69
   912
     * value. If either value is NaN, then the result is NaN.  Unlike
jaroslav@69
   913
     * the numerical comparison operators, this method considers
jaroslav@69
   914
     * negative zero to be strictly smaller than positive zero. If one
jaroslav@69
   915
     * argument is positive zero and the other is negative zero, the
jaroslav@69
   916
     * result is negative zero.
jaroslav@69
   917
     *
jaroslav@69
   918
     * @param   a   an argument.
jaroslav@69
   919
     * @param   b   another argument.
jaroslav@69
   920
     * @return  the smaller of {@code a} and {@code b}.
jaroslav@69
   921
     */
jaroslav@69
   922
    public static double min(double a, double b) {
jaroslav@69
   923
        if (a != a)
jaroslav@69
   924
            return a;   // a is NaN
jaroslav@69
   925
        if ((a == 0.0d) &&
jaroslav@69
   926
            (b == 0.0d) &&
jaroslav@69
   927
            (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
jaroslav@69
   928
            // Raw conversion ok since NaN can't map to -0.0.
jaroslav@69
   929
            return b;
jaroslav@69
   930
        }
jaroslav@69
   931
        return (a <= b) ? a : b;
jaroslav@69
   932
    }
jaroslav@69
   933
jaroslav@69
   934
    /**
jaroslav@69
   935
     * Returns the size of an ulp of the argument.  An ulp of a
jaroslav@69
   936
     * {@code double} value is the positive distance between this
jaroslav@69
   937
     * floating-point value and the {@code double} value next
jaroslav@69
   938
     * larger in magnitude.  Note that for non-NaN <i>x</i>,
jaroslav@69
   939
     * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
jaroslav@69
   940
     *
jaroslav@69
   941
     * <p>Special Cases:
jaroslav@69
   942
     * <ul>
jaroslav@69
   943
     * <li> If the argument is NaN, then the result is NaN.
jaroslav@69
   944
     * <li> If the argument is positive or negative infinity, then the
jaroslav@69
   945
     * result is positive infinity.
jaroslav@69
   946
     * <li> If the argument is positive or negative zero, then the result is
jaroslav@69
   947
     * {@code Double.MIN_VALUE}.
jaroslav@69
   948
     * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
jaroslav@69
   949
     * the result is equal to 2<sup>971</sup>.
jaroslav@69
   950
     * </ul>
jaroslav@69
   951
     *
jaroslav@69
   952
     * @param d the floating-point value whose ulp is to be returned
jaroslav@69
   953
     * @return the size of an ulp of the argument
jaroslav@69
   954
     * @author Joseph D. Darcy
jaroslav@69
   955
     * @since 1.5
jaroslav@69
   956
     */
jaroslav@69
   957
    public static double ulp(double d) {
jaroslav@69
   958
        return sun.misc.FpUtils.ulp(d);
jaroslav@69
   959
    }
jaroslav@69
   960
jaroslav@69
   961
    /**
jaroslav@69
   962
     * Returns the size of an ulp of the argument.  An ulp of a
jaroslav@69
   963
     * {@code float} value is the positive distance between this
jaroslav@69
   964
     * floating-point value and the {@code float} value next
jaroslav@69
   965
     * larger in magnitude.  Note that for non-NaN <i>x</i>,
jaroslav@69
   966
     * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
jaroslav@69
   967
     *
jaroslav@69
   968
     * <p>Special Cases:
jaroslav@69
   969
     * <ul>
jaroslav@69
   970
     * <li> If the argument is NaN, then the result is NaN.
jaroslav@69
   971
     * <li> If the argument is positive or negative infinity, then the
jaroslav@69
   972
     * result is positive infinity.
jaroslav@69
   973
     * <li> If the argument is positive or negative zero, then the result is
jaroslav@69
   974
     * {@code Float.MIN_VALUE}.
jaroslav@69
   975
     * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
jaroslav@69
   976
     * the result is equal to 2<sup>104</sup>.
jaroslav@69
   977
     * </ul>
jaroslav@69
   978
     *
jaroslav@69
   979
     * @param f the floating-point value whose ulp is to be returned
jaroslav@69
   980
     * @return the size of an ulp of the argument
jaroslav@69
   981
     * @author Joseph D. Darcy
jaroslav@69
   982
     * @since 1.5
jaroslav@69
   983
     */
jaroslav@69
   984
    public static float ulp(float f) {
jaroslav@69
   985
        return sun.misc.FpUtils.ulp(f);
jaroslav@69
   986
    }
jaroslav@69
   987
jaroslav@69
   988
    /**
jaroslav@69
   989
     * Returns the signum function of the argument; zero if the argument
jaroslav@69
   990
     * is zero, 1.0 if the argument is greater than zero, -1.0 if the
jaroslav@69
   991
     * argument is less than zero.
jaroslav@69
   992
     *
jaroslav@69
   993
     * <p>Special Cases:
jaroslav@69
   994
     * <ul>
jaroslav@69
   995
     * <li> If the argument is NaN, then the result is NaN.
jaroslav@69
   996
     * <li> If the argument is positive zero or negative zero, then the
jaroslav@69
   997
     *      result is the same as the argument.
jaroslav@69
   998
     * </ul>
jaroslav@69
   999
     *
jaroslav@69
  1000
     * @param d the floating-point value whose signum is to be returned
jaroslav@69
  1001
     * @return the signum function of the argument
jaroslav@69
  1002
     * @author Joseph D. Darcy
jaroslav@69
  1003
     * @since 1.5
jaroslav@69
  1004
     */
jaroslav@69
  1005
    public static double signum(double d) {
jaroslav@69
  1006
        return sun.misc.FpUtils.signum(d);
jaroslav@69
  1007
    }
jaroslav@69
  1008
jaroslav@69
  1009
    /**
jaroslav@69
  1010
     * Returns the signum function of the argument; zero if the argument
jaroslav@69
  1011
     * is zero, 1.0f if the argument is greater than zero, -1.0f if the
jaroslav@69
  1012
     * argument is less than zero.
jaroslav@69
  1013
     *
jaroslav@69
  1014
     * <p>Special Cases:
jaroslav@69
  1015
     * <ul>
jaroslav@69
  1016
     * <li> If the argument is NaN, then the result is NaN.
jaroslav@69
  1017
     * <li> If the argument is positive zero or negative zero, then the
jaroslav@69
  1018
     *      result is the same as the argument.
jaroslav@69
  1019
     * </ul>
jaroslav@69
  1020
     *
jaroslav@69
  1021
     * @param f the floating-point value whose signum is to be returned
jaroslav@69
  1022
     * @return the signum function of the argument
jaroslav@69
  1023
     * @author Joseph D. Darcy
jaroslav@69
  1024
     * @since 1.5
jaroslav@69
  1025
     */
jaroslav@69
  1026
    public static float signum(float f) {
jaroslav@69
  1027
        return sun.misc.FpUtils.signum(f);
jaroslav@69
  1028
    }
jaroslav@69
  1029
jaroslav@69
  1030
    /**
jaroslav@69
  1031
     * Returns the hyperbolic sine of a {@code double} value.
jaroslav@69
  1032
     * The hyperbolic sine of <i>x</i> is defined to be
jaroslav@69
  1033
     * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/2
jaroslav@69
  1034
     * where <i>e</i> is {@linkplain Math#E Euler's number}.
jaroslav@69
  1035
     *
jaroslav@69
  1036
     * <p>Special cases:
jaroslav@69
  1037
     * <ul>
jaroslav@69
  1038
     *
jaroslav@69
  1039
     * <li>If the argument is NaN, then the result is NaN.
jaroslav@69
  1040
     *
jaroslav@69
  1041
     * <li>If the argument is infinite, then the result is an infinity
jaroslav@69
  1042
     * with the same sign as the argument.
jaroslav@69
  1043
     *
jaroslav@69
  1044
     * <li>If the argument is zero, then the result is a zero with the
jaroslav@69
  1045
     * same sign as the argument.
jaroslav@69
  1046
     *
jaroslav@69
  1047
     * </ul>
jaroslav@69
  1048
     *
jaroslav@69
  1049
     * @param   x The number whose hyperbolic sine is to be returned.
jaroslav@69
  1050
     * @return  The hyperbolic sine of {@code x}.
jaroslav@69
  1051
     * @since 1.5
jaroslav@69
  1052
     */
jaroslav@69
  1053
    public static native double sinh(double x);
jaroslav@69
  1054
jaroslav@69
  1055
    /**
jaroslav@69
  1056
     * Returns the hyperbolic cosine of a {@code double} value.
jaroslav@69
  1057
     * The hyperbolic cosine of <i>x</i> is defined to be
jaroslav@69
  1058
     * (<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>)/2
jaroslav@69
  1059
     * where <i>e</i> is {@linkplain Math#E Euler's number}.
jaroslav@69
  1060
     *
jaroslav@69
  1061
     * <p>Special cases:
jaroslav@69
  1062
     * <ul>
jaroslav@69
  1063
     *
jaroslav@69
  1064
     * <li>If the argument is NaN, then the result is NaN.
jaroslav@69
  1065
     *
jaroslav@69
  1066
     * <li>If the argument is infinite, then the result is positive
jaroslav@69
  1067
     * infinity.
jaroslav@69
  1068
     *
jaroslav@69
  1069
     * <li>If the argument is zero, then the result is {@code 1.0}.
jaroslav@69
  1070
     *
jaroslav@69
  1071
     * </ul>
jaroslav@69
  1072
     *
jaroslav@69
  1073
     * @param   x The number whose hyperbolic cosine is to be returned.
jaroslav@69
  1074
     * @return  The hyperbolic cosine of {@code x}.
jaroslav@69
  1075
     * @since 1.5
jaroslav@69
  1076
     */
jaroslav@69
  1077
    public static native double cosh(double x);
jaroslav@69
  1078
jaroslav@69
  1079
    /**
jaroslav@69
  1080
     * Returns the hyperbolic tangent of a {@code double} value.
jaroslav@69
  1081
     * The hyperbolic tangent of <i>x</i> is defined to be
jaroslav@69
  1082
     * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/(<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>),
jaroslav@69
  1083
     * in other words, {@linkplain Math#sinh
jaroslav@69
  1084
     * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}.  Note
jaroslav@69
  1085
     * that the absolute value of the exact tanh is always less than
jaroslav@69
  1086
     * 1.
jaroslav@69
  1087
     *
jaroslav@69
  1088
     * <p>Special cases:
jaroslav@69
  1089
     * <ul>
jaroslav@69
  1090
     *
jaroslav@69
  1091
     * <li>If the argument is NaN, then the result is NaN.
jaroslav@69
  1092
     *
jaroslav@69
  1093
     * <li>If the argument is zero, then the result is a zero with the
jaroslav@69
  1094
     * same sign as the argument.
jaroslav@69
  1095
     *
jaroslav@69
  1096
     * <li>If the argument is positive infinity, then the result is
jaroslav@69
  1097
     * {@code +1.0}.
jaroslav@69
  1098
     *
jaroslav@69
  1099
     * <li>If the argument is negative infinity, then the result is
jaroslav@69
  1100
     * {@code -1.0}.
jaroslav@69
  1101
     *
jaroslav@69
  1102
     * </ul>
jaroslav@69
  1103
     *
jaroslav@69
  1104
     * @param   x The number whose hyperbolic tangent is to be returned.
jaroslav@69
  1105
     * @return  The hyperbolic tangent of {@code x}.
jaroslav@69
  1106
     * @since 1.5
jaroslav@69
  1107
     */
jaroslav@69
  1108
    public static native double tanh(double x);
jaroslav@69
  1109
jaroslav@69
  1110
    /**
jaroslav@69
  1111
     * Returns sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
jaroslav@69
  1112
     * without intermediate overflow or underflow.
jaroslav@69
  1113
     *
jaroslav@69
  1114
     * <p>Special cases:
jaroslav@69
  1115
     * <ul>
jaroslav@69
  1116
     *
jaroslav@69
  1117
     * <li> If either argument is infinite, then the result
jaroslav@69
  1118
     * is positive infinity.
jaroslav@69
  1119
     *
jaroslav@69
  1120
     * <li> If either argument is NaN and neither argument is infinite,
jaroslav@69
  1121
     * then the result is NaN.
jaroslav@69
  1122
     *
jaroslav@69
  1123
     * </ul>
jaroslav@69
  1124
     *
jaroslav@69
  1125
     * @param x a value
jaroslav@69
  1126
     * @param y a value
jaroslav@69
  1127
     * @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
jaroslav@69
  1128
     * without intermediate overflow or underflow
jaroslav@69
  1129
     * @since 1.5
jaroslav@69
  1130
     */
jaroslav@69
  1131
    public static native double hypot(double x, double y);
jaroslav@69
  1132
jaroslav@69
  1133
    /**
jaroslav@69
  1134
     * Returns <i>e</i><sup>x</sup>&nbsp;-1.  Note that for values of
jaroslav@69
  1135
     * <i>x</i> near 0, the exact sum of
jaroslav@69
  1136
     * {@code expm1(x)}&nbsp;+&nbsp;1 is much closer to the true
jaroslav@69
  1137
     * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
jaroslav@69
  1138
     *
jaroslav@69
  1139
     * <p>Special cases:
jaroslav@69
  1140
     * <ul>
jaroslav@69
  1141
     * <li>If the argument is NaN, the result is NaN.
jaroslav@69
  1142
     *
jaroslav@69
  1143
     * <li>If the argument is positive infinity, then the result is
jaroslav@69
  1144
     * positive infinity.
jaroslav@69
  1145
     *
jaroslav@69
  1146
     * <li>If the argument is negative infinity, then the result is
jaroslav@69
  1147
     * -1.0.
jaroslav@69
  1148
     *
jaroslav@69
  1149
     * <li>If the argument is zero, then the result is a zero with the
jaroslav@69
  1150
     * same sign as the argument.
jaroslav@69
  1151
     *
jaroslav@69
  1152
     * </ul>
jaroslav@69
  1153
     *
jaroslav@69
  1154
     * @param   x   the exponent to raise <i>e</i> to in the computation of
jaroslav@69
  1155
     *              <i>e</i><sup>{@code x}</sup>&nbsp;-1.
jaroslav@69
  1156
     * @return  the value <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1.
jaroslav@69
  1157
     * @since 1.5
jaroslav@69
  1158
     */
jaroslav@69
  1159
    public static native double expm1(double x);
jaroslav@69
  1160
jaroslav@69
  1161
    /**
jaroslav@69
  1162
     * Returns the natural logarithm of the sum of the argument and 1.
jaroslav@69
  1163
     * Note that for small values {@code x}, the result of
jaroslav@69
  1164
     * {@code log1p(x)} is much closer to the true result of ln(1
jaroslav@69
  1165
     * + {@code x}) than the floating-point evaluation of
jaroslav@69
  1166
     * {@code log(1.0+x)}.
jaroslav@69
  1167
     *
jaroslav@69
  1168
     * <p>Special cases:
jaroslav@69
  1169
     * <ul>
jaroslav@69
  1170
     *
jaroslav@69
  1171
     * <li>If the argument is NaN or less than -1, then the result is
jaroslav@69
  1172
     * NaN.
jaroslav@69
  1173
     *
jaroslav@69
  1174
     * <li>If the argument is positive infinity, then the result is
jaroslav@69
  1175
     * positive infinity.
jaroslav@69
  1176
     *
jaroslav@69
  1177
     * <li>If the argument is negative one, then the result is
jaroslav@69
  1178
     * negative infinity.
jaroslav@69
  1179
     *
jaroslav@69
  1180
     * <li>If the argument is zero, then the result is a zero with the
jaroslav@69
  1181
     * same sign as the argument.
jaroslav@69
  1182
     *
jaroslav@69
  1183
     * </ul>
jaroslav@69
  1184
     *
jaroslav@69
  1185
     * @param   x   a value
jaroslav@69
  1186
     * @return the value ln({@code x}&nbsp;+&nbsp;1), the natural
jaroslav@69
  1187
     * log of {@code x}&nbsp;+&nbsp;1
jaroslav@69
  1188
     * @since 1.5
jaroslav@69
  1189
     */
jaroslav@69
  1190
    public static native double log1p(double x);
jaroslav@69
  1191
jaroslav@69
  1192
    /**
jaroslav@69
  1193
     * Returns the first floating-point argument with the sign of the
jaroslav@69
  1194
     * second floating-point argument.  For this method, a NaN
jaroslav@69
  1195
     * {@code sign} argument is always treated as if it were
jaroslav@69
  1196
     * positive.
jaroslav@69
  1197
     *
jaroslav@69
  1198
     * @param magnitude  the parameter providing the magnitude of the result
jaroslav@69
  1199
     * @param sign   the parameter providing the sign of the result
jaroslav@69
  1200
     * @return a value with the magnitude of {@code magnitude}
jaroslav@69
  1201
     * and the sign of {@code sign}.
jaroslav@69
  1202
     * @since 1.6
jaroslav@69
  1203
     */
jaroslav@69
  1204
    public static double copySign(double magnitude, double sign) {
jaroslav@69
  1205
        return sun.misc.FpUtils.copySign(magnitude, sign);
jaroslav@69
  1206
    }
jaroslav@69
  1207
jaroslav@69
  1208
    /**
jaroslav@69
  1209
     * Returns the first floating-point argument with the sign of the
jaroslav@69
  1210
     * second floating-point argument.  For this method, a NaN
jaroslav@69
  1211
     * {@code sign} argument is always treated as if it were
jaroslav@69
  1212
     * positive.
jaroslav@69
  1213
     *
jaroslav@69
  1214
     * @param magnitude  the parameter providing the magnitude of the result
jaroslav@69
  1215
     * @param sign   the parameter providing the sign of the result
jaroslav@69
  1216
     * @return a value with the magnitude of {@code magnitude}
jaroslav@69
  1217
     * and the sign of {@code sign}.
jaroslav@69
  1218
     * @since 1.6
jaroslav@69
  1219
     */
jaroslav@69
  1220
    public static float copySign(float magnitude, float sign) {
jaroslav@69
  1221
        return sun.misc.FpUtils.copySign(magnitude, sign);
jaroslav@69
  1222
    }
jaroslav@69
  1223
    /**
jaroslav@69
  1224
     * Returns the unbiased exponent used in the representation of a
jaroslav@69
  1225
     * {@code float}.  Special cases:
jaroslav@69
  1226
     *
jaroslav@69
  1227
     * <ul>
jaroslav@69
  1228
     * <li>If the argument is NaN or infinite, then the result is
jaroslav@69
  1229
     * {@link Float#MAX_EXPONENT} + 1.
jaroslav@69
  1230
     * <li>If the argument is zero or subnormal, then the result is
jaroslav@69
  1231
     * {@link Float#MIN_EXPONENT} -1.
jaroslav@69
  1232
     * </ul>
jaroslav@69
  1233
     * @param f a {@code float} value
jaroslav@69
  1234
     * @since 1.6
jaroslav@69
  1235
     */
jaroslav@69
  1236
    public static int getExponent(float f) {
jaroslav@69
  1237
        return sun.misc.FpUtils.getExponent(f);
jaroslav@69
  1238
    }
jaroslav@69
  1239
jaroslav@69
  1240
    /**
jaroslav@69
  1241
     * Returns the unbiased exponent used in the representation of a
jaroslav@69
  1242
     * {@code double}.  Special cases:
jaroslav@69
  1243
     *
jaroslav@69
  1244
     * <ul>
jaroslav@69
  1245
     * <li>If the argument is NaN or infinite, then the result is
jaroslav@69
  1246
     * {@link Double#MAX_EXPONENT} + 1.
jaroslav@69
  1247
     * <li>If the argument is zero or subnormal, then the result is
jaroslav@69
  1248
     * {@link Double#MIN_EXPONENT} -1.
jaroslav@69
  1249
     * </ul>
jaroslav@69
  1250
     * @param d a {@code double} value
jaroslav@69
  1251
     * @since 1.6
jaroslav@69
  1252
     */
jaroslav@69
  1253
    public static int getExponent(double d) {
jaroslav@69
  1254
        return sun.misc.FpUtils.getExponent(d);
jaroslav@69
  1255
    }
jaroslav@69
  1256
jaroslav@69
  1257
    /**
jaroslav@69
  1258
     * Returns the floating-point number adjacent to the first
jaroslav@69
  1259
     * argument in the direction of the second argument.  If both
jaroslav@69
  1260
     * arguments compare as equal the second argument is returned.
jaroslav@69
  1261
     *
jaroslav@69
  1262
     * <p>Special cases:
jaroslav@69
  1263
     * <ul>
jaroslav@69
  1264
     * <li> If either argument is a NaN, then NaN is returned.
jaroslav@69
  1265
     *
jaroslav@69
  1266
     * <li> If both arguments are signed zeros, {@code direction}
jaroslav@69
  1267
     * is returned unchanged (as implied by the requirement of
jaroslav@69
  1268
     * returning the second argument if the arguments compare as
jaroslav@69
  1269
     * equal).
jaroslav@69
  1270
     *
jaroslav@69
  1271
     * <li> If {@code start} is
jaroslav@69
  1272
     * &plusmn;{@link Double#MIN_VALUE} and {@code direction}
jaroslav@69
  1273
     * has a value such that the result should have a smaller
jaroslav@69
  1274
     * magnitude, then a zero with the same sign as {@code start}
jaroslav@69
  1275
     * is returned.
jaroslav@69
  1276
     *
jaroslav@69
  1277
     * <li> If {@code start} is infinite and
jaroslav@69
  1278
     * {@code direction} has a value such that the result should
jaroslav@69
  1279
     * have a smaller magnitude, {@link Double#MAX_VALUE} with the
jaroslav@69
  1280
     * same sign as {@code start} is returned.
jaroslav@69
  1281
     *
jaroslav@69
  1282
     * <li> If {@code start} is equal to &plusmn;
jaroslav@69
  1283
     * {@link Double#MAX_VALUE} and {@code direction} has a
jaroslav@69
  1284
     * value such that the result should have a larger magnitude, an
jaroslav@69
  1285
     * infinity with same sign as {@code start} is returned.
jaroslav@69
  1286
     * </ul>
jaroslav@69
  1287
     *
jaroslav@69
  1288
     * @param start  starting floating-point value
jaroslav@69
  1289
     * @param direction value indicating which of
jaroslav@69
  1290
     * {@code start}'s neighbors or {@code start} should
jaroslav@69
  1291
     * be returned
jaroslav@69
  1292
     * @return The floating-point number adjacent to {@code start} in the
jaroslav@69
  1293
     * direction of {@code direction}.
jaroslav@69
  1294
     * @since 1.6
jaroslav@69
  1295
     */
jaroslav@69
  1296
    public static double nextAfter(double start, double direction) {
jaroslav@69
  1297
        return sun.misc.FpUtils.nextAfter(start, direction);
jaroslav@69
  1298
    }
jaroslav@69
  1299
jaroslav@69
  1300
    /**
jaroslav@69
  1301
     * Returns the floating-point number adjacent to the first
jaroslav@69
  1302
     * argument in the direction of the second argument.  If both
jaroslav@69
  1303
     * arguments compare as equal a value equivalent to the second argument
jaroslav@69
  1304
     * is returned.
jaroslav@69
  1305
     *
jaroslav@69
  1306
     * <p>Special cases:
jaroslav@69
  1307
     * <ul>
jaroslav@69
  1308
     * <li> If either argument is a NaN, then NaN is returned.
jaroslav@69
  1309
     *
jaroslav@69
  1310
     * <li> If both arguments are signed zeros, a value equivalent
jaroslav@69
  1311
     * to {@code direction} is returned.
jaroslav@69
  1312
     *
jaroslav@69
  1313
     * <li> If {@code start} is
jaroslav@69
  1314
     * &plusmn;{@link Float#MIN_VALUE} and {@code direction}
jaroslav@69
  1315
     * has a value such that the result should have a smaller
jaroslav@69
  1316
     * magnitude, then a zero with the same sign as {@code start}
jaroslav@69
  1317
     * is returned.
jaroslav@69
  1318
     *
jaroslav@69
  1319
     * <li> If {@code start} is infinite and
jaroslav@69
  1320
     * {@code direction} has a value such that the result should
jaroslav@69
  1321
     * have a smaller magnitude, {@link Float#MAX_VALUE} with the
jaroslav@69
  1322
     * same sign as {@code start} is returned.
jaroslav@69
  1323
     *
jaroslav@69
  1324
     * <li> If {@code start} is equal to &plusmn;
jaroslav@69
  1325
     * {@link Float#MAX_VALUE} and {@code direction} has a
jaroslav@69
  1326
     * value such that the result should have a larger magnitude, an
jaroslav@69
  1327
     * infinity with same sign as {@code start} is returned.
jaroslav@69
  1328
     * </ul>
jaroslav@69
  1329
     *
jaroslav@69
  1330
     * @param start  starting floating-point value
jaroslav@69
  1331
     * @param direction value indicating which of
jaroslav@69
  1332
     * {@code start}'s neighbors or {@code start} should
jaroslav@69
  1333
     * be returned
jaroslav@69
  1334
     * @return The floating-point number adjacent to {@code start} in the
jaroslav@69
  1335
     * direction of {@code direction}.
jaroslav@69
  1336
     * @since 1.6
jaroslav@69
  1337
     */
jaroslav@69
  1338
    public static float nextAfter(float start, double direction) {
jaroslav@69
  1339
        return sun.misc.FpUtils.nextAfter(start, direction);
jaroslav@69
  1340
    }
jaroslav@69
  1341
jaroslav@69
  1342
    /**
jaroslav@69
  1343
     * Returns the floating-point value adjacent to {@code d} in
jaroslav@69
  1344
     * the direction of positive infinity.  This method is
jaroslav@69
  1345
     * semantically equivalent to {@code nextAfter(d,
jaroslav@69
  1346
     * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
jaroslav@69
  1347
     * implementation may run faster than its equivalent
jaroslav@69
  1348
     * {@code nextAfter} call.
jaroslav@69
  1349
     *
jaroslav@69
  1350
     * <p>Special Cases:
jaroslav@69
  1351
     * <ul>
jaroslav@69
  1352
     * <li> If the argument is NaN, the result is NaN.
jaroslav@69
  1353
     *
jaroslav@69
  1354
     * <li> If the argument is positive infinity, the result is
jaroslav@69
  1355
     * positive infinity.
jaroslav@69
  1356
     *
jaroslav@69
  1357
     * <li> If the argument is zero, the result is
jaroslav@69
  1358
     * {@link Double#MIN_VALUE}
jaroslav@69
  1359
     *
jaroslav@69
  1360
     * </ul>
jaroslav@69
  1361
     *
jaroslav@69
  1362
     * @param d starting floating-point value
jaroslav@69
  1363
     * @return The adjacent floating-point value closer to positive
jaroslav@69
  1364
     * infinity.
jaroslav@69
  1365
     * @since 1.6
jaroslav@69
  1366
     */
jaroslav@69
  1367
    public static double nextUp(double d) {
jaroslav@69
  1368
        return sun.misc.FpUtils.nextUp(d);
jaroslav@69
  1369
    }
jaroslav@69
  1370
jaroslav@69
  1371
    /**
jaroslav@69
  1372
     * Returns the floating-point value adjacent to {@code f} in
jaroslav@69
  1373
     * the direction of positive infinity.  This method is
jaroslav@69
  1374
     * semantically equivalent to {@code nextAfter(f,
jaroslav@69
  1375
     * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
jaroslav@69
  1376
     * implementation may run faster than its equivalent
jaroslav@69
  1377
     * {@code nextAfter} call.
jaroslav@69
  1378
     *
jaroslav@69
  1379
     * <p>Special Cases:
jaroslav@69
  1380
     * <ul>
jaroslav@69
  1381
     * <li> If the argument is NaN, the result is NaN.
jaroslav@69
  1382
     *
jaroslav@69
  1383
     * <li> If the argument is positive infinity, the result is
jaroslav@69
  1384
     * positive infinity.
jaroslav@69
  1385
     *
jaroslav@69
  1386
     * <li> If the argument is zero, the result is
jaroslav@69
  1387
     * {@link Float#MIN_VALUE}
jaroslav@69
  1388
     *
jaroslav@69
  1389
     * </ul>
jaroslav@69
  1390
     *
jaroslav@69
  1391
     * @param f starting floating-point value
jaroslav@69
  1392
     * @return The adjacent floating-point value closer to positive
jaroslav@69
  1393
     * infinity.
jaroslav@69
  1394
     * @since 1.6
jaroslav@69
  1395
     */
jaroslav@69
  1396
    public static float nextUp(float f) {
jaroslav@69
  1397
        return sun.misc.FpUtils.nextUp(f);
jaroslav@69
  1398
    }
jaroslav@69
  1399
jaroslav@69
  1400
jaroslav@69
  1401
    /**
jaroslav@69
  1402
     * Return {@code d} &times;
jaroslav@69
  1403
     * 2<sup>{@code scaleFactor}</sup> rounded as if performed
jaroslav@69
  1404
     * by a single correctly rounded floating-point multiply to a
jaroslav@69
  1405
     * member of the double value set.  See the Java
jaroslav@69
  1406
     * Language Specification for a discussion of floating-point
jaroslav@69
  1407
     * value sets.  If the exponent of the result is between {@link
jaroslav@69
  1408
     * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
jaroslav@69
  1409
     * answer is calculated exactly.  If the exponent of the result
jaroslav@69
  1410
     * would be larger than {@code Double.MAX_EXPONENT}, an
jaroslav@69
  1411
     * infinity is returned.  Note that if the result is subnormal,
jaroslav@69
  1412
     * precision may be lost; that is, when {@code scalb(x, n)}
jaroslav@69
  1413
     * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
jaroslav@69
  1414
     * <i>x</i>.  When the result is non-NaN, the result has the same
jaroslav@69
  1415
     * sign as {@code d}.
jaroslav@69
  1416
     *
jaroslav@69
  1417
     * <p>Special cases:
jaroslav@69
  1418
     * <ul>
jaroslav@69
  1419
     * <li> If the first argument is NaN, NaN is returned.
jaroslav@69
  1420
     * <li> If the first argument is infinite, then an infinity of the
jaroslav@69
  1421
     * same sign is returned.
jaroslav@69
  1422
     * <li> If the first argument is zero, then a zero of the same
jaroslav@69
  1423
     * sign is returned.
jaroslav@69
  1424
     * </ul>
jaroslav@69
  1425
     *
jaroslav@69
  1426
     * @param d number to be scaled by a power of two.
jaroslav@69
  1427
     * @param scaleFactor power of 2 used to scale {@code d}
jaroslav@69
  1428
     * @return {@code d} &times; 2<sup>{@code scaleFactor}</sup>
jaroslav@69
  1429
     * @since 1.6
jaroslav@69
  1430
     */
jaroslav@69
  1431
    public static double scalb(double d, int scaleFactor) {
jaroslav@69
  1432
        return sun.misc.FpUtils.scalb(d, scaleFactor);
jaroslav@69
  1433
    }
jaroslav@69
  1434
jaroslav@69
  1435
    /**
jaroslav@69
  1436
     * Return {@code f} &times;
jaroslav@69
  1437
     * 2<sup>{@code scaleFactor}</sup> rounded as if performed
jaroslav@69
  1438
     * by a single correctly rounded floating-point multiply to a
jaroslav@69
  1439
     * member of the float value set.  See the Java
jaroslav@69
  1440
     * Language Specification for a discussion of floating-point
jaroslav@69
  1441
     * value sets.  If the exponent of the result is between {@link
jaroslav@69
  1442
     * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
jaroslav@69
  1443
     * answer is calculated exactly.  If the exponent of the result
jaroslav@69
  1444
     * would be larger than {@code Float.MAX_EXPONENT}, an
jaroslav@69
  1445
     * infinity is returned.  Note that if the result is subnormal,
jaroslav@69
  1446
     * precision may be lost; that is, when {@code scalb(x, n)}
jaroslav@69
  1447
     * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
jaroslav@69
  1448
     * <i>x</i>.  When the result is non-NaN, the result has the same
jaroslav@69
  1449
     * sign as {@code f}.
jaroslav@69
  1450
     *
jaroslav@69
  1451
     * <p>Special cases:
jaroslav@69
  1452
     * <ul>
jaroslav@69
  1453
     * <li> If the first argument is NaN, NaN is returned.
jaroslav@69
  1454
     * <li> If the first argument is infinite, then an infinity of the
jaroslav@69
  1455
     * same sign is returned.
jaroslav@69
  1456
     * <li> If the first argument is zero, then a zero of the same
jaroslav@69
  1457
     * sign is returned.
jaroslav@69
  1458
     * </ul>
jaroslav@69
  1459
     *
jaroslav@69
  1460
     * @param f number to be scaled by a power of two.
jaroslav@69
  1461
     * @param scaleFactor power of 2 used to scale {@code f}
jaroslav@69
  1462
     * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
jaroslav@69
  1463
     * @since 1.6
jaroslav@69
  1464
     */
jaroslav@69
  1465
    public static float scalb(float f, int scaleFactor) {
jaroslav@69
  1466
        return sun.misc.FpUtils.scalb(f, scaleFactor);
jaroslav@69
  1467
    }
jaroslav@69
  1468
}