jdk/src/share/classes/java/lang/Math.java
author sherman
Thu, 16 Feb 2012 11:43:20 -0800
changeset 11905 646e7e50c2d7
parent 11512 ac5944feab25
child 14342 8435a30053c1
permissions -rw-r--r--
6708398: Support integer overflow Summary: Added add/sub/multiply/toIntExact methods to j.l.Math and StrictMath classes Reviewed-by: emcmanus Contributed-by: roger.riggs@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
    29
import sun.misc.FloatConsts;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
    30
import sun.misc.DoubleConsts;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The class {@code Math} contains methods for performing basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * numeric operations such as the elementary exponential, logarithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * square root, and trigonometric functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>Unlike some of the numeric methods of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * {@code StrictMath}, all implementations of the equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * functions of class {@code Math} are not defined to return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * bit-for-bit same results.  This relaxation permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * better-performing implementations where strict reproducibility is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * not required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>By default many of the {@code Math} methods simply call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the equivalent method in {@code StrictMath} for their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * implementation.  Code generators are encouraged to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * platform-specific native libraries or microprocessor instructions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * where available, to provide higher-performance implementations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * {@code Math} methods.  Such higher-performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * implementations still must conform to the specification for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@code Math}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>The quality of implementation specifications concern two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * properties, accuracy of the returned result and monotonicity of the
10122
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    55
 * method.  Accuracy of the floating-point {@code Math} methods is
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    56
 * measured in terms of <i>ulps</i>, units in the last place.  For a
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    57
 * given floating-point format, an {@linkplain #ulp(double) ulp} of a
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    58
 * specific real number value is the distance between the two
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    59
 * floating-point values bracketing that numerical value.  When
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    60
 * discussing the accuracy of a method as a whole rather than at a
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    61
 * specific argument, the number of ulps cited is for the worst-case
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    62
 * error at any argument.  If a method always has an error less than
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    63
 * 0.5 ulps, the method always returns the floating-point number
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    64
 * nearest the exact result; such a method is <i>correctly
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    65
 * rounded</i>.  A correctly rounded method is generally the best a
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    66
 * floating-point approximation can be; however, it is impractical for
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    67
 * many floating-point methods to be correctly rounded.  Instead, for
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    68
 * the {@code Math} class, a larger error bound of 1 or 2 ulps is
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    69
 * allowed for certain methods.  Informally, with a 1 ulp error bound,
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    70
 * when the exact result is a representable number, the exact result
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    71
 * should be returned as the computed result; otherwise, either of the
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    72
 * two floating-point values which bracket the exact result may be
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    73
 * returned.  For exact results large in magnitude, one of the
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    74
 * endpoints of the bracket may be infinite.  Besides accuracy at
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    75
 * individual arguments, maintaining proper relations between the
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    76
 * method at different arguments is also important.  Therefore, most
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    77
 * methods with more than 0.5 ulp errors are required to be
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    78
 * <i>semi-monotonic</i>: whenever the mathematical function is
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    79
 * non-decreasing, so is the floating-point approximation, likewise,
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    80
 * whenever the mathematical function is non-increasing, so is the
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    81
 * floating-point approximation.  Not all approximations that have 1
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    82
 * ulp accuracy will automatically meet the monotonicity requirements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    84
 * <p>
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    85
 * The platform uses signed two's complement integer arithmetic with
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    86
 * int and long primitive types.  The developer should choose
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    87
 * the primitive type to ensure that arithmetic operations consistently
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    88
 * produce correct results, which in some cases means the operations
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    89
 * will not overflow the range of values of the computation.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    90
 * The best practice is to choose the primitive type and algorithm to avoid
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    91
 * overflow. In cases where the size is {@code int} or {@code long} and
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    92
 * overflow errors need to be detected, the methods {@code addExact},
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    93
 * {@code subtractExact}, {@code multiplyExact}, and {@code toIntExact}
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    94
 * throw an {@code ArithmeticException} when the results overflow.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    95
 * For other arithmetic operations such as divide, absolute value,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    96
 * increment, decrement, and negation overflow occurs only with
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    97
 * a specific minimum or maximum value and should be checked against
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    98
 * the minimum or maximum as appropriate.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    99
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @author  Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
public final class Math {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Don't let anyone instantiate this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private Math() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * The {@code double} value that is closer than any other to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <i>e</i>, the base of the natural logarithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static final double E = 2.7182818284590452354;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The {@code double} value that is closer than any other to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <i>pi</i>, the ratio of the circumference of a circle to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * diameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static final double PI = 3.14159265358979323846;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Returns the trigonometric sine of an angle.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <ul><li>If the argument is NaN or an infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * same sign as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param   a   an angle, in radians.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return  the sine of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static double sin(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return StrictMath.sin(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Returns the trigonometric cosine of an angle. Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <ul><li>If the argument is NaN or an infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * result is NaN.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param   a   an angle, in radians.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return  the cosine of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static double cos(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return StrictMath.cos(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the trigonometric tangent of an angle.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <ul><li>If the argument is NaN or an infinity, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * same sign as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param   a   an angle, in radians.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return  the tangent of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public static double tan(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return StrictMath.tan(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns the arc sine of a value; the returned angle is in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * range -<i>pi</i>/2 through <i>pi</i>/2.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <ul><li>If the argument is NaN or its absolute value is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * than 1, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * same sign as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param   a   the value whose arc sine is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return  the arc sine of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public static double asin(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return StrictMath.asin(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns the arc cosine of a value; the returned angle is in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * range 0.0 through <i>pi</i>.  Special case:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <ul><li>If the argument is NaN or its absolute value is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * than 1, then the result is NaN.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param   a   the value whose arc cosine is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return  the arc cosine of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public static double acos(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return StrictMath.acos(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Returns the arc tangent of a value; the returned angle is in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * range -<i>pi</i>/2 through <i>pi</i>/2.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <ul><li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * same sign as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param   a   the value whose arc tangent is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return  the arc tangent of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public static double atan(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return StrictMath.atan(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Converts an angle measured in degrees to an approximately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * equivalent angle measured in radians.  The conversion from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * degrees to radians is generally inexact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param   angdeg   an angle, in degrees
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @return  the measurement of the angle {@code angdeg}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *          in radians.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static double toRadians(double angdeg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return angdeg / 180.0 * PI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Converts an angle measured in radians to an approximately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * equivalent angle measured in degrees.  The conversion from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * radians to degrees is generally inexact; users should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * equal {@code 0.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param   angrad   an angle, in radians
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return  the measurement of the angle {@code angrad}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *          in degrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public static double toDegrees(double angrad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return angrad * 180.0 / PI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Returns Euler's number <i>e</i> raised to the power of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * {@code double} value.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <ul><li>If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <li>If the argument is negative infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * positive zero.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param   a   the exponent to raise <i>e</i> to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @return  the value <i>e</i><sup>{@code a}</sup>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *          where <i>e</i> is the base of the natural logarithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public static double exp(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return StrictMath.exp(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Returns the natural logarithm (base <i>e</i>) of a {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * value.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <ul><li>If the argument is NaN or less than zero, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <li>If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * result is negative infinity.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param   a   a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @return  the value ln&nbsp;{@code a}, the natural logarithm of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *          {@code a}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public static double log(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return StrictMath.log(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Returns the base 10 logarithm of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <ul><li>If the argument is NaN or less than zero, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <li>If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * result is negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <li> If the argument is equal to 10<sup><i>n</i></sup> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * integer <i>n</i>, then the result is <i>n</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param   a   a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @return  the base 10 logarithm of  {@code a}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public static double log10(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return StrictMath.log10(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Returns the correctly rounded positive square root of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <ul><li>If the argument is NaN or less than zero, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <li>If the argument is positive infinity, then the result is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <li>If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * result is the same as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Otherwise, the result is the {@code double} value closest to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * the true mathematical square root of the argument value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param   a   a value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return  the positive square root of {@code a}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *          If the argument is NaN or less than zero, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public static double sqrt(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return StrictMath.sqrt(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                   // Note that hardware sqrt instructions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                   // frequently can be directly used by JITs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                   // and should be much faster than doing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                   // Math.sqrt in software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns the cube root of a {@code double} value.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * positive finite {@code x}, {@code cbrt(-x) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * -cbrt(x)}; that is, the cube root of a negative value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * the negative of the cube root of that value's magnitude.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <li>If the argument is infinite, then the result is an infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * with the same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param   a   a value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @return  the cube root of {@code a}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public static double cbrt(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return StrictMath.cbrt(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Computes the remainder operation on two arguments as prescribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * by the IEEE 754 standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * The remainder value is mathematically equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <code>f1&nbsp;-&nbsp;f2</code>&nbsp;&times;&nbsp;<i>n</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * where <i>n</i> is the mathematical integer closest to the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * mathematical value of the quotient {@code f1/f2}, and if two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * mathematical integers are equally close to {@code f1/f2},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * then <i>n</i> is the integer that is even. If the remainder is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * zero, its sign is the same as the sign of the first argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * <ul><li>If either argument is NaN, or the first argument is infinite,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * or the second argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <li>If the first argument is finite and the second argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * infinite, then the result is the same as the first argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param   f1   the dividend.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param   f2   the divisor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @return  the remainder when {@code f1} is divided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *          {@code f2}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public static double IEEEremainder(double f1, double f2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Returns the smallest (closest to negative infinity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * {@code double} value that is greater than or equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * argument and is equal to a mathematical integer. Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * <ul><li>If the argument value is already equal to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * mathematical integer, then the result is the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * argument.  <li>If the argument is NaN or an infinity or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * positive zero or negative zero, then the result is the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * the argument.  <li>If the argument value is less than zero but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * greater than -1.0, then the result is negative zero.</ul> Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * that the value of {@code Math.ceil(x)} is exactly the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * value of {@code -Math.floor(-x)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param   a   a value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @return  the smallest (closest to negative infinity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *          floating-point value that is greater than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *          the argument and is equal to a mathematical integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public static double ceil(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return StrictMath.ceil(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Returns the largest (closest to positive infinity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * {@code double} value that is less than or equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * argument and is equal to a mathematical integer. Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * <ul><li>If the argument value is already equal to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * mathematical integer, then the result is the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * argument.  <li>If the argument is NaN or an infinity or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * positive zero or negative zero, then the result is the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @param   a   a value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @return  the largest (closest to positive infinity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *          floating-point value that less than or equal to the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *          and is equal to a mathematical integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public static double floor(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return StrictMath.floor(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Returns the {@code double} value that is closest in value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * to the argument and is equal to a mathematical integer. If two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * {@code double} values that are mathematical integers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * equally close, the result is the integer value that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * even. Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <ul><li>If the argument value is already equal to a mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * integer, then the result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * <li>If the argument is NaN or an infinity or positive zero or negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * zero, then the result is the same as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @param   a   a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @return  the closest floating-point value to {@code a} that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *          equal to a mathematical integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public static double rint(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return StrictMath.rint(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Returns the angle <i>theta</i> from the conversion of rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * coordinates ({@code x},&nbsp;{@code y}) to polar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * coordinates (r,&nbsp;<i>theta</i>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * This method computes the phase <i>theta</i> by computing an arc tangent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * <ul><li>If either argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * <li>If the first argument is positive zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * is positive, or the first argument is positive and finite and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * second argument is positive infinity, then the result is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * <li>If the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * is positive, or the first argument is negative and finite and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * second argument is positive infinity, then the result is negative zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <li>If the first argument is positive zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * is negative, or the first argument is positive and finite and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * second argument is negative infinity, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * {@code double} value closest to <i>pi</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <li>If the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * is negative, or the first argument is negative and finite and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * second argument is negative infinity, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * {@code double} value closest to -<i>pi</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <li>If the first argument is positive and the second argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * positive zero or negative zero, or the first argument is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * infinity and the second argument is finite, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * {@code double} value closest to <i>pi</i>/2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <li>If the first argument is negative and the second argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * positive zero or negative zero, or the first argument is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * infinity and the second argument is finite, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * {@code double} value closest to -<i>pi</i>/2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * <li>If both arguments are positive infinity, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * {@code double} value closest to <i>pi</i>/4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <li>If the first argument is positive infinity and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * is negative infinity, then the result is the {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * value closest to 3*<i>pi</i>/4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * <li>If the first argument is negative infinity and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * is positive infinity, then the result is the {@code double} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * closest to -<i>pi</i>/4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * <li>If both arguments are negative infinity, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * {@code double} value closest to -3*<i>pi</i>/4.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * <p>The computed result must be within 2 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param   y   the ordinate coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param   x   the abscissa coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @return  the <i>theta</i> component of the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *          (<i>r</i>,&nbsp;<i>theta</i>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *          in polar coordinates that corresponds to the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *          (<i>x</i>,&nbsp;<i>y</i>) in Cartesian coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public static double atan2(double y, double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Returns the value of the first argument raised to the power of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * second argument. Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * <ul><li>If the second argument is positive or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * result is 1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * <li>If the second argument is 1.0, then the result is the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * first argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <li>If the second argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * <li>If the first argument is NaN and the second argument is nonzero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <li>the absolute value of the first argument is greater than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * and the second argument is positive infinity, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <li>the absolute value of the first argument is less than 1 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * the second argument is negative infinity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * then the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * <li>the absolute value of the first argument is greater than 1 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * the second argument is negative infinity, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * <li>the absolute value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * first argument is less than 1 and the second argument is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * infinity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * then the result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * <li>If the absolute value of the first argument equals 1 and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * second argument is infinite, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * <li>the first argument is positive zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * is greater than zero, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * <li>the first argument is positive infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * argument is less than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * then the result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * <li>the first argument is positive zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * is less than zero, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * <li>the first argument is positive infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * argument is greater than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * then the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <li>the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * is greater than zero but not a finite odd integer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <li>the first argument is negative infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * argument is less than zero but not a finite odd integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * then the result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <li>the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * is a positive finite odd integer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <li>the first argument is negative infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * argument is a negative finite odd integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * then the result is negative zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * <li>the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * is less than zero but not a finite odd integer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * <li>the first argument is negative infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * argument is greater than zero but not a finite odd integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * then the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * <li>the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * is a negative finite odd integer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <li>the first argument is negative infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * argument is a positive finite odd integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * then the result is negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <li>If the first argument is finite and less than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * <li> if the second argument is a finite even integer, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * result is equal to the result of raising the absolute value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * the first argument to the power of the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * <li>if the second argument is a finite odd integer, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * is equal to the negative of the result of raising the absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * value of the first argument to the power of the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * <li>if the second argument is finite and not an integer, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * <li>If both arguments are integers, then the result is exactly equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * to the mathematical result of raising the first argument to the power
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * of the second argument if that result can in fact be represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * exactly as a {@code double} value.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <p>(In the foregoing descriptions, a floating-point value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * considered to be an integer if and only if it is finite and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * fixed point of the method {@link #ceil ceil} or,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * equivalently, a fixed point of the method {@link #floor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * floor}. A value is a fixed point of a one-argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * method if and only if the result of applying the method to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * value is equal to the value.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @param   a   the base.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @param   b   the exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @return  the value {@code a}<sup>{@code b}</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public static double pow(double a, double b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return StrictMath.pow(a, b); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   648
     * Returns the closest {@code int} to the argument, with ties
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   649
     * rounding up.
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   650
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * <ul><li>If the argument is NaN, the result is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <li>If the argument is negative infinity or any value less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * equal to the value of {@code Integer.MIN_VALUE}, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * equal to the value of {@code Integer.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * <li>If the argument is positive infinity or any value greater than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * equal to the value of {@code Integer.MAX_VALUE}, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * equal to the value of {@code Integer.MAX_VALUE}.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @param   a   a floating-point value to be rounded to an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @return  the value of the argument rounded to the nearest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *          {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @see     java.lang.Integer#MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @see     java.lang.Integer#MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    public static int round(float a) {
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   668
        if (a != 0x1.fffffep-2f) // greatest float value less than 0.5
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   669
            return (int)floor(a + 0.5f);
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   670
        else
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   671
            return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /**
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   675
     * Returns the closest {@code long} to the argument, with ties
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   676
     * rounding up.
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   677
     *
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   678
     * <p>Special cases:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <ul><li>If the argument is NaN, the result is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <li>If the argument is negative infinity or any value less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * equal to the value of {@code Long.MIN_VALUE}, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * equal to the value of {@code Long.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <li>If the argument is positive infinity or any value greater than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * equal to the value of {@code Long.MAX_VALUE}, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * equal to the value of {@code Long.MAX_VALUE}.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @param   a   a floating-point value to be rounded to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *          {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @return  the value of the argument rounded to the nearest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *          {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @see     java.lang.Long#MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @see     java.lang.Long#MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    public static long round(double a) {
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   695
        if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   696
            return (long)floor(a + 0.5d);
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   697
        else
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   698
            return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    private static Random randomNumberGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
5781
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   703
    private static synchronized Random initRNG() {
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   704
        Random rnd = randomNumberGenerator;
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   705
        return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * Returns a {@code double} value with a positive sign, greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * than or equal to {@code 0.0} and less than {@code 1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * Returned values are chosen pseudorandomly with (approximately)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * uniform distribution from that range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * <p>When this method is first called, it creates a single new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * pseudorandom-number generator, exactly as if by the expression
5781
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   716
     *
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   717
     * <blockquote>{@code new java.util.Random()}</blockquote>
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   718
     *
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   719
     * This new pseudorandom-number generator is used thereafter for
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   720
     * all calls to this method and is used nowhere else.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * <p>This method is properly synchronized to allow correct use by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * more than one thread. However, if many threads need to generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * pseudorandom numbers at a great rate, it may reduce contention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * for each thread to have its own pseudorandom-number generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @return  a pseudorandom {@code double} greater than or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * to {@code 0.0} and less than {@code 1.0}.
5781
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   729
     * @see Random#nextDouble()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    public static double random() {
5781
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   732
        Random rnd = randomNumberGenerator;
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   733
        if (rnd == null) rnd = initRNG();
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   734
        return rnd.nextDouble();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   738
     * Returns the sum of its arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   739
     * throwing an exception if the result overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   740
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   741
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   742
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   743
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   744
     * @throws ArithmeticException if the result overflows an int
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   745
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   746
    public static int addExact(int x, int y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   747
        int r = x + y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   748
        // HD 2-12 Overflow iff both arguments have the opposite sign of the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   749
        if (((x ^ r) & (y ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   750
            throw new ArithmeticException("integer overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   751
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   752
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   753
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   754
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   755
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   756
     * Returns the sum of its arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   757
     * throwing an exception if the result overflows a {@code long}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   758
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   759
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   760
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   761
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   762
     * @throws ArithmeticException if the result overflows a long
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   763
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   764
    public static long addExact(long x, long y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   765
        long r = x + y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   766
        // HD 2-12 Overflow iff both arguments have the opposite sign of the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   767
        if (((x ^ r) & (y ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   768
            throw new ArithmeticException("long overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   769
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   770
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   771
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   772
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   773
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   774
     * Returns the difference of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   775
     * throwing an exception if the result overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   776
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   777
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   778
     * @param y the second value to subtract from the first
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   779
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   780
     * @throws ArithmeticException if the result overflows an int
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   781
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   782
    public static int subtractExact(int x, int y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   783
        int r = x - y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   784
        // HD 2-12 Overflow iff the arguments have different signs and
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   785
        // the sign of the result is different than the sign of x
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   786
        if (((x ^ y) & (x ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   787
            throw new ArithmeticException("integer overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   788
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   789
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   790
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   791
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   792
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   793
     * Returns the difference of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   794
     * throwing an exception if the result overflows a {@code long}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   795
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   796
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   797
     * @param y the second value to subtract from the first
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   798
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   799
     * @throws ArithmeticException if the result overflows a long
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   800
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   801
    public static long subtractExact(long x, long y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   802
        long r = x - y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   803
        // HD 2-12 Overflow iff the arguments have different signs and
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   804
        // the sign of the result is different than the sign of x
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   805
        if (((x ^ y) & (x ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   806
            throw new ArithmeticException("long overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   807
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   808
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   809
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   810
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   811
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   812
     * Returns the product of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   813
     * throwing an exception if the result overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   814
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   815
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   816
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   817
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   818
     * @throws ArithmeticException if the result overflows an int
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   819
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   820
    public static int multiplyExact(int x, int y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   821
        long r = (long)x * (long)y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   822
        if ((int)r != r) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   823
            throw new ArithmeticException("long overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   824
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   825
        return (int)r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   826
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   827
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   828
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   829
     * Returns the product of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   830
     * throwing an exception if the result overflows a {@code long}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   831
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   832
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   833
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   834
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   835
     * @throws ArithmeticException if the result overflows a long
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   836
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   837
    public static long multiplyExact(long x, long y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   838
        long r = x * y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   839
        long ax = Math.abs(x);
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   840
        long ay = Math.abs(y);
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   841
        if (((ax | ay) >>> 31 != 0)) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   842
            // Some bits greater than 2^31 that might cause overflow
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   843
            // Check the result using the divide operator
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   844
            // and check for the special case of Long.MIN_VALUE * -1
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   845
           if (((y != 0) && (r / y != x)) ||
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   846
               (x == Long.MIN_VALUE && y == -1)) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   847
                throw new ArithmeticException("long overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   848
            }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   849
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   850
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   851
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   852
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   853
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   854
     * Returns the value of the {@code long} argument;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   855
     * throwing an exception if the value overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   856
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   857
     * @param value the long value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   858
     * @return the argument as an int
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   859
     * @throws ArithmeticException if the {@code argument} overflows an int
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   860
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   861
    public static int toIntExact(long value) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   862
        if ((int)value != value) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   863
            throw new ArithmeticException("integer overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   864
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   865
        return (int)value;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   866
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   867
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   868
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * Returns the absolute value of an {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * <p>Note that if the argument is equal to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * {@link Integer#MIN_VALUE}, the most negative representable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * {@code int} value, the result is that same value, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    public static int abs(int a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        return (a < 0) ? -a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * Returns the absolute value of a {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * <p>Note that if the argument is equal to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * {@link Long#MIN_VALUE}, the most negative representable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * {@code long} value, the result is that same value, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    public static long abs(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        return (a < 0) ? -a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * Returns the absolute value of a {@code float} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * <ul><li>If the argument is positive zero or negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * <li>If the argument is infinite, the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <li>If the argument is NaN, the result is NaN.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * In other words, the result is the same as the value of the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    public static float abs(float a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        return (a <= 0.0F) ? 0.0F - a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Returns the absolute value of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * <ul><li>If the argument is positive zero or negative zero, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * <li>If the argument is infinite, the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * <li>If the argument is NaN, the result is NaN.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * In other words, the result is the same as the value of the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    public static double abs(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        return (a <= 0.0D) ? 0.0D - a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * Returns the greater of two {@code int} values. That is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * result is the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * {@link Integer#MAX_VALUE}. If the arguments have the same value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    public static int max(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * Returns the greater of two {@code long} values. That is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * result is the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * {@link Long#MAX_VALUE}. If the arguments have the same value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    public static long max(long a, long b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
   968
    // Use raw bit-wise conversions on guaranteed non-NaN arguments.
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
   969
    private static long negativeZeroFloatBits  = Float.floatToRawIntBits(-0.0f);
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
   970
    private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * Returns the greater of two {@code float} values.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * the result is the argument closer to positive infinity. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * negative zero to be strictly smaller than positive zero. If one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * argument is positive zero and the other negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    public static float max(float a, float b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
   987
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
   988
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
   989
        if ((a == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
   990
            (b == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
   991
            (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
   992
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * Returns the greater of two {@code double} values.  That
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * is, the result is the argument closer to positive infinity. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * the arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * negative zero to be strictly smaller than positive zero. If one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * argument is positive zero and the other negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    public static double max(double a, double b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1013
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1014
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1015
        if ((a == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1016
            (b == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1017
            (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1018
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * Returns the smaller of two {@code int} values. That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * the result the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * {@link Integer#MIN_VALUE}.  If the arguments have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * value, the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    public static int min(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * Returns the smaller of two {@code long} values. That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * the result is the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * {@link Long#MIN_VALUE}. If the arguments have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * value, the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    public static long min(long a, long b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * Returns the smaller of two {@code float} values.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * the result is the value closer to negative infinity. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * negative zero to be strictly smaller than positive zero.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * one argument is positive zero and the other is negative zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * the result is negative zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    public static float min(float a, float b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1067
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1068
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1069
        if ((a == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1070
            (b == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1071
            (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1072
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * Returns the smaller of two {@code double} values.  That
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * is, the result is the value closer to negative infinity. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * negative zero to be strictly smaller than positive zero. If one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * argument is positive zero and the other is negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * result is negative zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    public static double min(double a, double b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1093
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1094
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1095
        if ((a == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1096
            (b == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1097
            (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1098
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /**
10122
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1105
     * Returns the size of an ulp of the argument.  An ulp, unit in
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1106
     * the last place, of a {@code double} value is the positive
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1107
     * distance between this floating-point value and the {@code
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1108
     * double} value next larger in magnitude.  Note that for non-NaN
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1109
     * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * <li> If the argument is positive or negative infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * <li> If the argument is positive or negative zero, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * {@code Double.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * the result is equal to 2<sup>971</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * @param d the floating-point value whose ulp is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @return the size of an ulp of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    public static double ulp(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1128
        int exp = getExponent(d);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1129
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1130
        switch(exp) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1131
        case DoubleConsts.MAX_EXPONENT+1:       // NaN or infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1132
            return Math.abs(d);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1133
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1134
        case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1135
            return Double.MIN_VALUE;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1136
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1137
        default:
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1138
            assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1139
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1140
            // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1141
            exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1142
            if (exp >= DoubleConsts.MIN_EXPONENT) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1143
                return powerOfTwoD(exp);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1144
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1145
            else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1146
                // return a subnormal result; left shift integer
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1147
                // representation of Double.MIN_VALUE appropriate
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1148
                // number of positions
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1149
                return Double.longBitsToDouble(1L <<
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1150
                (exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) ));
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1151
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1152
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    /**
10122
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1156
     * Returns the size of an ulp of the argument.  An ulp, unit in
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1157
     * the last place, of a {@code float} value is the positive
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1158
     * distance between this floating-point value and the {@code
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1159
     * float} value next larger in magnitude.  Note that for non-NaN
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1160
     * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * <li> If the argument is positive or negative infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * <li> If the argument is positive or negative zero, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * {@code Float.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * the result is equal to 2<sup>104</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * @param f the floating-point value whose ulp is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @return the size of an ulp of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    public static float ulp(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1179
        int exp = getExponent(f);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1180
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1181
        switch(exp) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1182
        case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1183
            return Math.abs(f);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1184
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1185
        case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1186
            return FloatConsts.MIN_VALUE;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1187
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1188
        default:
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1189
            assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1190
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1191
            // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1192
            exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1193
            if (exp >= FloatConsts.MIN_EXPONENT) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1194
                return powerOfTwoF(exp);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1195
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1196
            else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1197
                // return a subnormal result; left shift integer
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1198
                // representation of FloatConsts.MIN_VALUE appropriate
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1199
                // number of positions
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1200
                return Float.intBitsToFloat(1 <<
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1201
                (exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) ));
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1202
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1203
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * Returns the signum function of the argument; zero if the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * is zero, 1.0 if the argument is greater than zero, -1.0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * argument is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * <li> If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     *      result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @param d the floating-point value whose signum is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @return the signum function of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    public static double signum(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1224
        return (d == 0.0 || Double.isNaN(d))?d:copySign(1.0, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * Returns the signum function of the argument; zero if the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * is zero, 1.0f if the argument is greater than zero, -1.0f if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * argument is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * <li> If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *      result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @param f the floating-point value whose signum is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * @return the signum function of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    public static float signum(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1245
        return (f == 0.0f || Float.isNaN(f))?f:copySign(1.0f, f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * Returns the hyperbolic sine of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * The hyperbolic sine of <i>x</i> is defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * where <i>e</i> is {@linkplain Math#E Euler's number}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * <li>If the argument is infinite, then the result is an infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * with the same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * <p>The computed result must be within 2.5 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @param   x The number whose hyperbolic sine is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * @return  The hyperbolic sine of {@code x}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    public static double sinh(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        return StrictMath.sinh(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * Returns the hyperbolic cosine of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * The hyperbolic cosine of <i>x</i> is defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * (<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>)/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * where <i>e</i> is {@linkplain Math#E Euler's number}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * <li>If the argument is infinite, then the result is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * <li>If the argument is zero, then the result is {@code 1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * <p>The computed result must be within 2.5 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * @param   x The number whose hyperbolic cosine is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * @return  The hyperbolic cosine of {@code x}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    public static double cosh(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        return StrictMath.cosh(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * Returns the hyperbolic tangent of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * The hyperbolic tangent of <i>x</i> is defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/(<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * in other words, {@linkplain Math#sinh
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * that the absolute value of the exact tanh is always less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * {@code +1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * <li>If the argument is negative infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * {@code -1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * <p>The computed result must be within 2.5 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * The result of {@code tanh} for any finite input must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * an absolute value less than or equal to 1.  Note that once the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * exact result of tanh is within 1/2 of an ulp of the limit value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * of &plusmn;1, correctly signed &plusmn;{@code 1.0} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @param   x The number whose hyperbolic tangent is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @return  The hyperbolic tangent of {@code x}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    public static double tanh(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        return StrictMath.tanh(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * Returns sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * without intermediate overflow or underflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * <li> If either argument is infinite, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * <li> If either argument is NaN and neither argument is infinite,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * <p>The computed result must be within 1 ulp of the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * result.  If one parameter is held constant, the results must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * semi-monotonic in the other parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * @param x a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * @param y a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * without intermediate overflow or underflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    public static double hypot(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        return StrictMath.hypot(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * Returns <i>e</i><sup>x</sup>&nbsp;-1.  Note that for values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * <i>x</i> near 0, the exact sum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * {@code expm1(x)}&nbsp;+&nbsp;1 is much closer to the true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * <li>If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * <li>If the argument is negative infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * -1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * Results must be semi-monotonic.  The result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * {@code expm1} for any finite input must be greater than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * equal to {@code -1.0}.  Note that once the exact result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1 is within 1/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * ulp of the limit value -1, {@code -1.0} should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * @param   x   the exponent to raise <i>e</i> to in the computation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     *              <i>e</i><sup>{@code x}</sup>&nbsp;-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * @return  the value <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    public static double expm1(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        return StrictMath.expm1(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * Returns the natural logarithm of the sum of the argument and 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * Note that for small values {@code x}, the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * {@code log1p(x)} is much closer to the true result of ln(1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * + {@code x}) than the floating-point evaluation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * {@code log(1.0+x)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * <li>If the argument is NaN or less than -1, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * <li>If the argument is negative one, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * @param   x   a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * @return the value ln({@code x}&nbsp;+&nbsp;1), the natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * log of {@code x}&nbsp;+&nbsp;1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    public static double log1p(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        return StrictMath.log1p(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * second floating-point argument.  Note that unlike the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * StrictMath#copySign(double, double) StrictMath.copySign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * method, this method does not require NaN {@code sign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * arguments to be treated as positive values; implementations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * permitted to treat some NaN arguments as positive and other NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * arguments as negative to allow greater performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * @param sign   the parameter providing the sign of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * @return a value with the magnitude of {@code magnitude}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * and the sign of {@code sign}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    public static double copySign(double magnitude, double sign) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1465
        return Double.longBitsToDouble((Double.doubleToRawLongBits(sign) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1466
                                        (DoubleConsts.SIGN_BIT_MASK)) |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1467
                                       (Double.doubleToRawLongBits(magnitude) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1468
                                        (DoubleConsts.EXP_BIT_MASK |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1469
                                         DoubleConsts.SIGNIF_BIT_MASK)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     * second floating-point argument.  Note that unlike the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * StrictMath#copySign(float, float) StrictMath.copySign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * method, this method does not require NaN {@code sign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * arguments to be treated as positive values; implementations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * permitted to treat some NaN arguments as positive and other NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * arguments as negative to allow greater performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * @param sign   the parameter providing the sign of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * @return a value with the magnitude of {@code magnitude}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * and the sign of {@code sign}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    public static float copySign(float magnitude, float sign) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1488
        return Float.intBitsToFloat((Float.floatToRawIntBits(sign) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1489
                                     (FloatConsts.SIGN_BIT_MASK)) |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1490
                                    (Float.floatToRawIntBits(magnitude) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1491
                                     (FloatConsts.EXP_BIT_MASK |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1492
                                      FloatConsts.SIGNIF_BIT_MASK)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * Returns the unbiased exponent used in the representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * {@code float}.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * <li>If the argument is NaN or infinite, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * {@link Float#MAX_EXPONENT} + 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * <li>If the argument is zero or subnormal, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * {@link Float#MIN_EXPONENT} -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * @param f a {@code float} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * @return the unbiased exponent of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    public static int getExponent(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1510
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1511
         * Bitwise convert f to integer, mask out exponent bits, shift
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1512
         * to the right and then subtract out float's bias adjust to
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1513
         * get true exponent value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1514
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1515
        return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1516
                (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * Returns the unbiased exponent used in the representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * {@code double}.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * <li>If the argument is NaN or infinite, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * {@link Double#MAX_EXPONENT} + 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * <li>If the argument is zero or subnormal, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * {@link Double#MIN_EXPONENT} -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * @param d a {@code double} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * @return the unbiased exponent of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    public static int getExponent(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1534
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1535
         * Bitwise convert d to long, mask out exponent bits, shift
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1536
         * to the right and then subtract out double's bias adjust to
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1537
         * get true exponent value.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1538
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1539
        return (int)(((Double.doubleToRawLongBits(d) & DoubleConsts.EXP_BIT_MASK) >>
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1540
                      (DoubleConsts.SIGNIFICAND_WIDTH - 1)) - DoubleConsts.EXP_BIAS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * Returns the floating-point number adjacent to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * argument in the direction of the second argument.  If both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * arguments compare as equal the second argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * <li> If either argument is a NaN, then NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * <li> If both arguments are signed zeros, {@code direction}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * is returned unchanged (as implied by the requirement of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * returning the second argument if the arguments compare as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * equal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * <li> If {@code start} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * &plusmn;{@link Double#MIN_VALUE} and {@code direction}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * has a value such that the result should have a smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * magnitude, then a zero with the same sign as {@code start}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * <li> If {@code start} is infinite and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * {@code direction} has a value such that the result should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * have a smaller magnitude, {@link Double#MAX_VALUE} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * <li> If {@code start} is equal to &plusmn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * {@link Double#MAX_VALUE} and {@code direction} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * value such that the result should have a larger magnitude, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * infinity with same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * @param start  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * @param direction value indicating which of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * {@code start}'s neighbors or {@code start} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * @return The floating-point number adjacent to {@code start} in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * direction of {@code direction}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    public static double nextAfter(double start, double direction) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1584
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1585
         * The cases:
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1586
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1587
         * nextAfter(+infinity, 0)  == MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1588
         * nextAfter(+infinity, +infinity)  == +infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1589
         * nextAfter(-infinity, 0)  == -MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1590
         * nextAfter(-infinity, -infinity)  == -infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1591
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1592
         * are naturally handled without any additional testing
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1593
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1594
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1595
        // First check for NaN values
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1596
        if (Double.isNaN(start) || Double.isNaN(direction)) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1597
            // return a NaN derived from the input NaN(s)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1598
            return start + direction;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1599
        } else if (start == direction) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1600
            return direction;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1601
        } else {        // start > direction or start < direction
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1602
            // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1603
            // then bitwise convert start to integer.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1604
            long transducer = Double.doubleToRawLongBits(start + 0.0d);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1605
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1606
            /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1607
             * IEEE 754 floating-point numbers are lexicographically
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1608
             * ordered if treated as signed- magnitude integers .
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1609
             * Since Java's integers are two's complement,
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1610
             * incrementing" the two's complement representation of a
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1611
             * logically negative floating-point value *decrements*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1612
             * the signed-magnitude representation. Therefore, when
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1613
             * the integer representation of a floating-point values
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1614
             * is less than zero, the adjustment to the representation
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1615
             * is in the opposite direction than would be expected at
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1616
             * first .
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1617
             */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1618
            if (direction > start) { // Calculate next greater value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1619
                transducer = transducer + (transducer >= 0L ? 1L:-1L);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1620
            } else  { // Calculate next lesser value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1621
                assert direction < start;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1622
                if (transducer > 0L)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1623
                    --transducer;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1624
                else
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1625
                    if (transducer < 0L )
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1626
                        ++transducer;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1627
                    /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1628
                     * transducer==0, the result is -MIN_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1629
                     *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1630
                     * The transition from zero (implicitly
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1631
                     * positive) to the smallest negative
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1632
                     * signed magnitude value must be done
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1633
                     * explicitly.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1634
                     */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1635
                    else
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1636
                        transducer = DoubleConsts.SIGN_BIT_MASK | 1L;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1637
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1638
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1639
            return Double.longBitsToDouble(transducer);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1640
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * Returns the floating-point number adjacent to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * argument in the direction of the second argument.  If both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * arguments compare as equal a value equivalent to the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * <li> If either argument is a NaN, then NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * <li> If both arguments are signed zeros, a value equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * to {@code direction} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * <li> If {@code start} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * &plusmn;{@link Float#MIN_VALUE} and {@code direction}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * has a value such that the result should have a smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * magnitude, then a zero with the same sign as {@code start}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * <li> If {@code start} is infinite and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * {@code direction} has a value such that the result should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * have a smaller magnitude, {@link Float#MAX_VALUE} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * <li> If {@code start} is equal to &plusmn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * {@link Float#MAX_VALUE} and {@code direction} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * value such that the result should have a larger magnitude, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * infinity with same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * @param start  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * @param direction value indicating which of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * {@code start}'s neighbors or {@code start} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * @return The floating-point number adjacent to {@code start} in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * direction of {@code direction}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    public static float nextAfter(float start, double direction) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1683
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1684
         * The cases:
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1685
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1686
         * nextAfter(+infinity, 0)  == MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1687
         * nextAfter(+infinity, +infinity)  == +infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1688
         * nextAfter(-infinity, 0)  == -MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1689
         * nextAfter(-infinity, -infinity)  == -infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1690
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1691
         * are naturally handled without any additional testing
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1692
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1693
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1694
        // First check for NaN values
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1695
        if (Float.isNaN(start) || Double.isNaN(direction)) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1696
            // return a NaN derived from the input NaN(s)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1697
            return start + (float)direction;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1698
        } else if (start == direction) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1699
            return (float)direction;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1700
        } else {        // start > direction or start < direction
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1701
            // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1702
            // then bitwise convert start to integer.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1703
            int transducer = Float.floatToRawIntBits(start + 0.0f);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1704
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1705
            /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1706
             * IEEE 754 floating-point numbers are lexicographically
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1707
             * ordered if treated as signed- magnitude integers .
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1708
             * Since Java's integers are two's complement,
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1709
             * incrementing" the two's complement representation of a
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1710
             * logically negative floating-point value *decrements*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1711
             * the signed-magnitude representation. Therefore, when
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1712
             * the integer representation of a floating-point values
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1713
             * is less than zero, the adjustment to the representation
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1714
             * is in the opposite direction than would be expected at
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1715
             * first.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1716
             */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1717
            if (direction > start) {// Calculate next greater value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1718
                transducer = transducer + (transducer >= 0 ? 1:-1);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1719
            } else  { // Calculate next lesser value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1720
                assert direction < start;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1721
                if (transducer > 0)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1722
                    --transducer;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1723
                else
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1724
                    if (transducer < 0 )
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1725
                        ++transducer;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1726
                    /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1727
                     * transducer==0, the result is -MIN_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1728
                     *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1729
                     * The transition from zero (implicitly
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1730
                     * positive) to the smallest negative
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1731
                     * signed magnitude value must be done
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1732
                     * explicitly.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1733
                     */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1734
                    else
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1735
                        transducer = FloatConsts.SIGN_BIT_MASK | 1;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1736
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1737
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1738
            return Float.intBitsToFloat(transducer);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1739
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * Returns the floating-point value adjacent to {@code d} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * the direction of positive infinity.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * semantically equivalent to {@code nextAfter(d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * implementation may run faster than its equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * {@code nextAfter} call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * <li> If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * <li> If the argument is zero, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * {@link Double#MIN_VALUE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * @param d starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * @return The adjacent floating-point value closer to positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    public static double nextUp(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1768
        if( Double.isNaN(d) || d == Double.POSITIVE_INFINITY)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1769
            return d;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1770
        else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1771
            d += 0.0d;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1772
            return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1773
                                           ((d >= 0.0d)?+1L:-1L));
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1774
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * Returns the floating-point value adjacent to {@code f} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * the direction of positive infinity.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * semantically equivalent to {@code nextAfter(f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * implementation may run faster than its equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * {@code nextAfter} call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * <li> If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * <li> If the argument is zero, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * {@link Float#MIN_VALUE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * @param f starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * @return The adjacent floating-point value closer to positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    public static float nextUp(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1803
        if( Float.isNaN(f) || f == FloatConsts.POSITIVE_INFINITY)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1804
            return f;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1805
        else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1806
            f += 0.0f;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1807
            return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1808
                                        ((f >= 0.0f)?+1:-1));
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1809
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
10608
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1812
    /**
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1813
     * Returns the floating-point value adjacent to {@code d} in
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1814
     * the direction of negative infinity.  This method is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1815
     * semantically equivalent to {@code nextAfter(d,
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1816
     * Double.NEGATIVE_INFINITY)}; however, a
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1817
     * {@code nextDown} implementation may run faster than its
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1818
     * equivalent {@code nextAfter} call.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1819
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1820
     * <p>Special Cases:
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1821
     * <ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1822
     * <li> If the argument is NaN, the result is NaN.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1823
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1824
     * <li> If the argument is negative infinity, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1825
     * negative infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1826
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1827
     * <li> If the argument is zero, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1828
     * {@code -Double.MIN_VALUE}
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1829
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1830
     * </ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1831
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1832
     * @param d  starting floating-point value
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1833
     * @return The adjacent floating-point value closer to negative
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1834
     * infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1835
     * @since 1.8
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1836
     */
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1837
    public static double nextDown(double d) {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1838
        if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1839
            return d;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1840
        else {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1841
            if (d == 0.0)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1842
                return -Double.MIN_VALUE;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1843
            else
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1844
                return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1845
                                               ((d > 0.0d)?-1L:+1L));
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1846
        }
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1847
    }
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1848
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1849
    /**
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1850
     * Returns the floating-point value adjacent to {@code f} in
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1851
     * the direction of negative infinity.  This method is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1852
     * semantically equivalent to {@code nextAfter(f,
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1853
     * Float.NEGATIVE_INFINITY)}; however, a
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1854
     * {@code nextDown} implementation may run faster than its
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1855
     * equivalent {@code nextAfter} call.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1856
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1857
     * <p>Special Cases:
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1858
     * <ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1859
     * <li> If the argument is NaN, the result is NaN.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1860
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1861
     * <li> If the argument is negative infinity, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1862
     * negative infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1863
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1864
     * <li> If the argument is zero, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1865
     * {@code -Float.MIN_VALUE}
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1866
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1867
     * </ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1868
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1869
     * @param f  starting floating-point value
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1870
     * @return The adjacent floating-point value closer to negative
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1871
     * infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1872
     * @since 1.8
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1873
     */
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1874
    public static float nextDown(float f) {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1875
        if (Float.isNaN(f) || f == Float.NEGATIVE_INFINITY)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1876
            return f;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1877
        else {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1878
            if (f == 0.0f)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1879
                return -Float.MIN_VALUE;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1880
            else
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1881
                return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1882
                                            ((f > 0.0f)?-1:+1));
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1883
        }
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  1884
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1887
     * Returns {@code d} &times;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * 2<sup>{@code scaleFactor}</sup> rounded as if performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * by a single correctly rounded floating-point multiply to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * member of the double value set.  See the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * Language Specification for a discussion of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * value sets.  If the exponent of the result is between {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     * answer is calculated exactly.  If the exponent of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * would be larger than {@code Double.MAX_EXPONENT}, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * infinity is returned.  Note that if the result is subnormal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * precision may be lost; that is, when {@code scalb(x, n)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * <i>x</i>.  When the result is non-NaN, the result has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     * sign as {@code d}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * <li> If the first argument is NaN, NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * <li> If the first argument is infinite, then an infinity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * same sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * <li> If the first argument is zero, then a zero of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * @param d number to be scaled by a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * @param scaleFactor power of 2 used to scale {@code d}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * @return {@code d} &times; 2<sup>{@code scaleFactor}</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
    public static double scalb(double d, int scaleFactor) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1917
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1918
         * This method does not need to be declared strictfp to
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1919
         * compute the same correct result on all platforms.  When
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1920
         * scaling up, it does not matter what order the
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1921
         * multiply-store operations are done; the result will be
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1922
         * finite or overflow regardless of the operation ordering.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1923
         * However, to get the correct result when scaling down, a
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1924
         * particular ordering must be used.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1925
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1926
         * When scaling down, the multiply-store operations are
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1927
         * sequenced so that it is not possible for two consecutive
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1928
         * multiply-stores to return subnormal results.  If one
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1929
         * multiply-store result is subnormal, the next multiply will
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1930
         * round it away to zero.  This is done by first multiplying
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1931
         * by 2 ^ (scaleFactor % n) and then multiplying several
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1932
         * times by by 2^n as needed where n is the exponent of number
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1933
         * that is a covenient power of two.  In this way, at most one
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1934
         * real rounding error occurs.  If the double value set is
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1935
         * being used exclusively, the rounding will occur on a
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1936
         * multiply.  If the double-extended-exponent value set is
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1937
         * being used, the products will (perhaps) be exact but the
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1938
         * stores to d are guaranteed to round to the double value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1939
         * set.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1940
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1941
         * It is _not_ a valid implementation to first multiply d by
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1942
         * 2^MIN_EXPONENT and then by 2 ^ (scaleFactor %
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1943
         * MIN_EXPONENT) since even in a strictfp program double
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1944
         * rounding on underflow could occur; e.g. if the scaleFactor
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1945
         * argument was (MIN_EXPONENT - n) and the exponent of d was a
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1946
         * little less than -(MIN_EXPONENT - n), meaning the final
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1947
         * result would be subnormal.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1948
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1949
         * Since exact reproducibility of this method can be achieved
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1950
         * without any undue performance burden, there is no
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1951
         * compelling reason to allow double rounding on underflow in
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1952
         * scalb.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1953
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1954
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1955
        // magnitude of a power of two so large that scaling a finite
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1956
        // nonzero value by it would be guaranteed to over or
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1957
        // underflow; due to rounding, scaling down takes takes an
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1958
        // additional power of two which is reflected here
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1959
        final int MAX_SCALE = DoubleConsts.MAX_EXPONENT + -DoubleConsts.MIN_EXPONENT +
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1960
                              DoubleConsts.SIGNIFICAND_WIDTH + 1;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1961
        int exp_adjust = 0;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1962
        int scale_increment = 0;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1963
        double exp_delta = Double.NaN;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1964
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1965
        // Make sure scaling factor is in a reasonable range
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1966
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1967
        if(scaleFactor < 0) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1968
            scaleFactor = Math.max(scaleFactor, -MAX_SCALE);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1969
            scale_increment = -512;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1970
            exp_delta = twoToTheDoubleScaleDown;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1971
        }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1972
        else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1973
            scaleFactor = Math.min(scaleFactor, MAX_SCALE);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1974
            scale_increment = 512;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1975
            exp_delta = twoToTheDoubleScaleUp;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1976
        }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1977
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1978
        // Calculate (scaleFactor % +/-512), 512 = 2^9, using
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1979
        // technique from "Hacker's Delight" section 10-2.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1980
        int t = (scaleFactor >> 9-1) >>> 32 - 9;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1981
        exp_adjust = ((scaleFactor + t) & (512 -1)) - t;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1982
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1983
        d *= powerOfTwoD(exp_adjust);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1984
        scaleFactor -= exp_adjust;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1985
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1986
        while(scaleFactor != 0) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1987
            d *= exp_delta;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1988
            scaleFactor -= scale_increment;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1989
        }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1990
        return d;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1994
     * Returns {@code f} &times;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * 2<sup>{@code scaleFactor}</sup> rounded as if performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * by a single correctly rounded floating-point multiply to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * member of the float value set.  See the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     * Language Specification for a discussion of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * value sets.  If the exponent of the result is between {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * answer is calculated exactly.  If the exponent of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * would be larger than {@code Float.MAX_EXPONENT}, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * infinity is returned.  Note that if the result is subnormal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * precision may be lost; that is, when {@code scalb(x, n)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * <i>x</i>.  When the result is non-NaN, the result has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * sign as {@code f}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * <li> If the first argument is NaN, NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * <li> If the first argument is infinite, then an infinity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * same sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * <li> If the first argument is zero, then a zero of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * @param f number to be scaled by a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * @param scaleFactor power of 2 used to scale {@code f}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
    public static float scalb(float f, int scaleFactor) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2024
        // magnitude of a power of two so large that scaling a finite
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2025
        // nonzero value by it would be guaranteed to over or
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2026
        // underflow; due to rounding, scaling down takes takes an
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2027
        // additional power of two which is reflected here
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2028
        final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2029
                              FloatConsts.SIGNIFICAND_WIDTH + 1;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2030
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2031
        // Make sure scaling factor is in a reasonable range
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2032
        scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2033
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2034
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2035
         * Since + MAX_SCALE for float fits well within the double
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2036
         * exponent range and + float -> double conversion is exact
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2037
         * the multiplication below will be exact. Therefore, the
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2038
         * rounding that occurs when the double product is cast to
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2039
         * float will be the correctly rounded float result.  Since
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2040
         * all operations other than the final multiply will be exact,
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2041
         * it is not necessary to declare this method strictfp.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2042
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2043
        return (float)((double)f*powerOfTwoD(scaleFactor));
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2044
    }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2045
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2046
    // Constants used in scalb
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2047
    static double twoToTheDoubleScaleUp = powerOfTwoD(512);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2048
    static double twoToTheDoubleScaleDown = powerOfTwoD(-512);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2049
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2050
    /**
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2051
     * Returns a floating-point power of two in the normal range.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2052
     */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2053
    static double powerOfTwoD(int n) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2054
        assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2055
        return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2056
                                        (DoubleConsts.SIGNIFICAND_WIDTH-1))
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2057
                                       & DoubleConsts.EXP_BIT_MASK);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2058
    }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2059
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2060
    /**
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2061
     * Returns a floating-point power of two in the normal range.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2062
     */
11510
f52f50e63c9b 7123649: Remove public modifier from Math.powerOfTwoF.
darcy
parents: 10608
diff changeset
  2063
    static float powerOfTwoF(int n) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2064
        assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2065
        return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2066
                                     (FloatConsts.SIGNIFICAND_WIDTH-1))
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2067
                                    & FloatConsts.EXP_BIT_MASK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
}