jdk/src/share/classes/java/lang/Math.java
author bpb
Fri, 11 Apr 2014 13:12:11 -0700
changeset 23745 7898c52fcfb4
parent 19851 7b6ff45c39ce
child 24253 ce29e10e4b41
permissions -rw-r--r--
8035427: Math.random() JavaDoc: missing maximum returned value Summary: Add some documentation amplifying the description of Math.random(). Reviewed-by: psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19851
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
     2
 * Copyright (c) 1994, 2013, 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
19851
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   649
     * rounding to positive infinity.
9269
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) {
19851
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   668
        int intBits = Float.floatToRawIntBits(a);
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   669
        int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   670
                >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   671
        int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   672
                + FloatConsts.EXP_BIAS) - biasedExp;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   673
        if ((shift & -32) == 0) { // shift >= 0 && shift < 32
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   674
            // a is a finite number such that pow(2,-32) <= ulp(a) < 1
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   675
            int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   676
                    | (FloatConsts.SIGNIF_BIT_MASK + 1));
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   677
            if (intBits < 0) {
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   678
                r = -r;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   679
            }
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   680
            // In the comments below each Java expression evaluates to the value
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   681
            // the corresponding mathematical expression:
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   682
            // (r) evaluates to a / ulp(a)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   683
            // (r >> shift) evaluates to floor(a * 2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   684
            // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   685
            // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   686
            return ((r >> shift) + 1) >> 1;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   687
        } else {
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   688
            // a is either
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   689
            // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   690
            // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   691
            // - an infinity or NaN
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   692
            return (int) a;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   693
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   697
     * Returns the closest {@code long} to the argument, with ties
19851
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   698
     * rounding to positive infinity.
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   699
     *
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   700
     * <p>Special cases:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * <ul><li>If the argument is NaN, the result is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * <li>If the argument is negative infinity or any value less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * equal to the value of {@code Long.MIN_VALUE}, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * equal to the value of {@code Long.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * <li>If the argument is positive infinity or any value greater than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * equal to the value of {@code Long.MAX_VALUE}, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * equal to the value of {@code Long.MAX_VALUE}.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @param   a   a floating-point value to be rounded to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *          {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @return  the value of the argument rounded to the nearest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *          {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @see     java.lang.Long#MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @see     java.lang.Long#MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    public static long round(double a) {
19851
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   717
        long longBits = Double.doubleToRawLongBits(a);
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   718
        long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   719
                >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   720
        long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   721
                + DoubleConsts.EXP_BIAS) - biasedExp;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   722
        if ((shift & -64) == 0) { // shift >= 0 && shift < 64
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   723
            // a is a finite number such that pow(2,-64) <= ulp(a) < 1
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   724
            long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   725
                    | (DoubleConsts.SIGNIF_BIT_MASK + 1));
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   726
            if (longBits < 0) {
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   727
                r = -r;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   728
            }
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   729
            // In the comments below each Java expression evaluates to the value
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   730
            // the corresponding mathematical expression:
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   731
            // (r) evaluates to a / ulp(a)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   732
            // (r >> shift) evaluates to floor(a * 2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   733
            // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   734
            // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   735
            return ((r >> shift) + 1) >> 1;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   736
        } else {
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   737
            // a is either
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   738
            // - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   739
            // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   740
            // - an infinity or NaN
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   741
            return (long) a;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   742
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
19583
828d85603705 6470700: Math.random() / Math.initRNG() uses "double checked locking"
bpb
parents: 19406
diff changeset
   745
    private static final class RandomNumberGeneratorHolder {
828d85603705 6470700: Math.random() / Math.initRNG() uses "double checked locking"
bpb
parents: 19406
diff changeset
   746
        static final Random randomNumberGenerator = new Random();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * Returns a {@code double} value with a positive sign, greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * than or equal to {@code 0.0} and less than {@code 1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Returned values are chosen pseudorandomly with (approximately)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * uniform distribution from that range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * <p>When this method is first called, it creates a single new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * pseudorandom-number generator, exactly as if by the expression
5781
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   757
     *
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   758
     * <blockquote>{@code new java.util.Random()}</blockquote>
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   759
     *
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   760
     * This new pseudorandom-number generator is used thereafter for
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   761
     * all calls to this method and is used nowhere else.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * <p>This method is properly synchronized to allow correct use by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * more than one thread. However, if many threads need to generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * pseudorandom numbers at a great rate, it may reduce contention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * for each thread to have its own pseudorandom-number generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *
23745
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   768
     * @apiNote
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   769
     * As the largest {@code double} value less than {@code 1.0}
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   770
     * is {@code Math.nextDown(1.0)}, a value {@code x} in the closed range
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   771
     * {@code [x1,x2]} where {@code x1<=x2} may be defined by the statements
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   772
     *
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   773
     * <blockquote><pre>{@code
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   774
     * double f = Math.random()/Math.nextDown(1.0);
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   775
     * double x = x1*(1.0 - f) + x2*f;
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   776
     * }</pre></blockquote>
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   777
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @return  a pseudorandom {@code double} greater than or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * to {@code 0.0} and less than {@code 1.0}.
23745
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   780
     * @see #nextDown(double)
5781
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   781
     * @see Random#nextDouble()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    public static double random() {
19583
828d85603705 6470700: Math.random() / Math.initRNG() uses "double checked locking"
bpb
parents: 19406
diff changeset
   784
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   788
     * Returns the sum of its arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   789
     * throwing an exception if the result overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   790
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   791
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   792
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   793
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   794
     * @throws ArithmeticException if the result overflows an int
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   795
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   796
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   797
    public static int addExact(int x, int y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   798
        int r = x + y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   799
        // HD 2-12 Overflow iff both arguments have the opposite sign of the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   800
        if (((x ^ r) & (y ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   801
            throw new ArithmeticException("integer overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   802
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   803
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   804
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   805
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   806
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   807
     * Returns the sum of its arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   808
     * throwing an exception if the result overflows a {@code long}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   809
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   810
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   811
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   812
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   813
     * @throws ArithmeticException if the result overflows a long
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   814
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   815
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   816
    public static long addExact(long x, long y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   817
        long r = x + y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   818
        // HD 2-12 Overflow iff both arguments have the opposite sign of the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   819
        if (((x ^ r) & (y ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   820
            throw new ArithmeticException("long overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   821
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   822
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   823
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   824
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   825
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   826
     * Returns the difference of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   827
     * throwing an exception if the result overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   828
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   829
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   830
     * @param y the second value to subtract from the first
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   831
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   832
     * @throws ArithmeticException if the result overflows an int
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   833
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   834
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   835
    public static int subtractExact(int x, int y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   836
        int r = x - y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   837
        // HD 2-12 Overflow iff the arguments have different signs and
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   838
        // the sign of the result is different than the sign of x
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   839
        if (((x ^ y) & (x ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   840
            throw new ArithmeticException("integer overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   841
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   842
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   843
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   844
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   845
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   846
     * Returns the difference of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   847
     * throwing an exception if the result overflows a {@code long}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   848
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   849
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   850
     * @param y the second value to subtract from the first
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   851
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   852
     * @throws ArithmeticException if the result overflows a long
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   853
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   854
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   855
    public static long subtractExact(long x, long y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   856
        long r = x - y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   857
        // HD 2-12 Overflow iff the arguments have different signs and
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   858
        // the sign of the result is different than the sign of x
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   859
        if (((x ^ y) & (x ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   860
            throw new ArithmeticException("long overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   861
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   862
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   863
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   864
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   865
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   866
     * Returns the product of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   867
     * throwing an exception if the result overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   868
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   869
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   870
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   871
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   872
     * @throws ArithmeticException if the result overflows an int
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   873
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   874
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   875
    public static int multiplyExact(int x, int y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   876
        long r = (long)x * (long)y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   877
        if ((int)r != r) {
19406
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   878
            throw new ArithmeticException("integer overflow");
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   879
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   880
        return (int)r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   881
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   882
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   883
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   884
     * Returns the product of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   885
     * throwing an exception if the result overflows a {@code long}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   886
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   887
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   888
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   889
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   890
     * @throws ArithmeticException if the result overflows a long
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   891
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   892
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   893
    public static long multiplyExact(long x, long y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   894
        long r = x * y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   895
        long ax = Math.abs(x);
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   896
        long ay = Math.abs(y);
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   897
        if (((ax | ay) >>> 31 != 0)) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   898
            // Some bits greater than 2^31 that might cause overflow
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   899
            // Check the result using the divide operator
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   900
            // and check for the special case of Long.MIN_VALUE * -1
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   901
           if (((y != 0) && (r / y != x)) ||
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   902
               (x == Long.MIN_VALUE && y == -1)) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   903
                throw new ArithmeticException("long overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   904
            }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   905
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   906
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   907
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   908
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   909
    /**
19406
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   910
     * Returns the argument incremented by one, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   911
     * result overflows an {@code int}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   912
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   913
     * @param a the value to increment
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   914
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   915
     * @throws ArithmeticException if the result overflows an int
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   916
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   917
     */
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   918
    public static int incrementExact(int a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   919
        if (a == Integer.MAX_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   920
            throw new ArithmeticException("integer overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   921
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   922
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   923
        return a + 1;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   924
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   925
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   926
    /**
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   927
     * Returns the argument incremented by one, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   928
     * result overflows a {@code long}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   929
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   930
     * @param a the value to increment
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   931
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   932
     * @throws ArithmeticException if the result overflows a long
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   933
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   934
     */
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   935
    public static long incrementExact(long a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   936
        if (a == Long.MAX_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   937
            throw new ArithmeticException("long overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   938
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   939
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   940
        return a + 1L;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   941
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   942
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   943
    /**
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   944
     * Returns the argument decremented by one, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   945
     * result overflows an {@code int}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   946
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   947
     * @param a the value to decrement
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   948
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   949
     * @throws ArithmeticException if the result overflows an int
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   950
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   951
     */
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   952
    public static int decrementExact(int a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   953
        if (a == Integer.MIN_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   954
            throw new ArithmeticException("integer overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   955
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   956
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   957
        return a - 1;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   958
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   959
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   960
    /**
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   961
     * Returns the argument decremented by one, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   962
     * result overflows a {@code long}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   963
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   964
     * @param a the value to decrement
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   965
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   966
     * @throws ArithmeticException if the result overflows a long
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   967
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   968
     */
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   969
    public static long decrementExact(long a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   970
        if (a == Long.MIN_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   971
            throw new ArithmeticException("long overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   972
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   973
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   974
        return a - 1L;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   975
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   976
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   977
    /**
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   978
     * Returns the negation of the argument, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   979
     * result overflows an {@code int}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   980
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   981
     * @param a the value to negate
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   982
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   983
     * @throws ArithmeticException if the result overflows an int
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   984
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   985
     */
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   986
    public static int negateExact(int a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   987
        if (a == Integer.MIN_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   988
            throw new ArithmeticException("integer overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   989
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   990
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   991
        return -a;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   992
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   993
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   994
    /**
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   995
     * Returns the negation of the argument, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   996
     * result overflows a {@code long}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   997
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   998
     * @param a the value to negate
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   999
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1000
     * @throws ArithmeticException if the result overflows a long
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1001
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1002
     */
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1003
    public static long negateExact(long a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1004
        if (a == Long.MIN_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1005
            throw new ArithmeticException("long overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1006
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1007
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1008
        return -a;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1009
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1010
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1011
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1012
     * Returns the value of the {@code long} argument;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1013
     * throwing an exception if the value overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1014
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1015
     * @param value the long value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1016
     * @return the argument as an int
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1017
     * @throws ArithmeticException if the {@code argument} overflows an int
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1018
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1019
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1020
    public static int toIntExact(long value) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1021
        if ((int)value != value) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1022
            throw new ArithmeticException("integer overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1023
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1024
        return (int)value;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1025
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1026
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1027
    /**
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1028
     * Returns the largest (closest to positive infinity)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1029
     * {@code int} value that is less than or equal to the algebraic quotient.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1030
     * There is one special case, if the dividend is the
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1031
     * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1032
     * then integer overflow occurs and
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1033
     * the result is equal to the {@code Integer.MIN_VALUE}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1034
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1035
     * Normal integer division operates under the round to zero rounding mode
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1036
     * (truncation).  This operation instead acts under the round toward
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1037
     * negative infinity (floor) rounding mode.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1038
     * The floor rounding mode gives different results than truncation
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1039
     * when the exact result is negative.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1040
     * <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1041
     *   <li>If the signs of the arguments are the same, the results of
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1042
     *       {@code floorDiv} and the {@code /} operator are the same.  <br>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1043
     *       For example, {@code floorDiv(4, 3) == 1} and {@code (4 / 3) == 1}.</li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1044
     *   <li>If the signs of the arguments are different,  the quotient is negative and
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1045
     *       {@code floorDiv} returns the integer less than or equal to the quotient
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1046
     *       and the {@code /} operator returns the integer closest to zero.<br>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1047
     *       For example, {@code floorDiv(-4, 3) == -2},
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1048
     *       whereas {@code (-4 / 3) == -1}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1049
     *   </li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1050
     * </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1051
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1052
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1053
     * @param x the dividend
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1054
     * @param y the divisor
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1055
     * @return the largest (closest to positive infinity)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1056
     * {@code int} value that is less than or equal to the algebraic quotient.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1057
     * @throws ArithmeticException if the divisor {@code y} is zero
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1058
     * @see #floorMod(int, int)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1059
     * @see #floor(double)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1060
     * @since 1.8
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1061
     */
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1062
    public static int floorDiv(int x, int y) {
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1063
        int r = x / y;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1064
        // if the signs are different and modulo not zero, round down
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1065
        if ((x ^ y) < 0 && (r * y != x)) {
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1066
            r--;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1067
        }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1068
        return r;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1069
    }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1070
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1071
    /**
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1072
     * Returns the largest (closest to positive infinity)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1073
     * {@code long} value that is less than or equal to the algebraic quotient.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1074
     * There is one special case, if the dividend is the
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1075
     * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1076
     * then integer overflow occurs and
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1077
     * the result is equal to the {@code Long.MIN_VALUE}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1078
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1079
     * Normal integer division operates under the round to zero rounding mode
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1080
     * (truncation).  This operation instead acts under the round toward
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1081
     * negative infinity (floor) rounding mode.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1082
     * The floor rounding mode gives different results than truncation
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1083
     * when the exact result is negative.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1084
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1085
     * For examples, see {@link #floorDiv(int, int)}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1086
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1087
     * @param x the dividend
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1088
     * @param y the divisor
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1089
     * @return the largest (closest to positive infinity)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1090
     * {@code long} value that is less than or equal to the algebraic quotient.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1091
     * @throws ArithmeticException if the divisor {@code y} is zero
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1092
     * @see #floorMod(long, long)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1093
     * @see #floor(double)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1094
     * @since 1.8
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1095
     */
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1096
    public static long floorDiv(long x, long y) {
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1097
        long r = x / y;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1098
        // if the signs are different and modulo not zero, round down
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1099
        if ((x ^ y) < 0 && (r * y != x)) {
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1100
            r--;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1101
        }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1102
        return r;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1103
    }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1104
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1105
    /**
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1106
     * Returns the floor modulus of the {@code int} arguments.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1107
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1108
     * The floor modulus is {@code x - (floorDiv(x, y) * y)},
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1109
     * has the same sign as the divisor {@code y}, and
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1110
     * is in the range of {@code -abs(y) < r < +abs(y)}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1111
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1112
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1113
     * The relationship between {@code floorDiv} and {@code floorMod} is such that:
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1114
     * <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1115
     *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1116
     * </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1117
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1118
     * The difference in values between {@code floorMod} and
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1119
     * the {@code %} operator is due to the difference between
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1120
     * {@code floorDiv} that returns the integer less than or equal to the quotient
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1121
     * and the {@code /} operator that returns the integer closest to zero.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1122
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1123
     * Examples:
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1124
     * <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1125
     *   <li>If the signs of the arguments are the same, the results
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1126
     *       of {@code floorMod} and the {@code %} operator are the same.  <br>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1127
     *       <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1128
     *       <li>{@code floorMod(4, 3) == 1}; &nbsp; and {@code (4 % 3) == 1}</li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1129
     *       </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1130
     *   <li>If the signs of the arguments are different, the results differ from the {@code %} operator.<br>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1131
     *      <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1132
     *      <li>{@code floorMod(+4, -3) == -2}; &nbsp; and {@code (+4 % -3) == +1} </li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1133
     *      <li>{@code floorMod(-4, +3) == +2}; &nbsp; and {@code (-4 % +3) == -1} </li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1134
     *      <li>{@code floorMod(-4, -3) == -1}; &nbsp; and {@code (-4 % -3) == -1 } </li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1135
     *      </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1136
     *   </li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1137
     * </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1138
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1139
     * If the signs of arguments are unknown and a positive modulus
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1140
     * is needed it can be computed as {@code (floorMod(x, y) + abs(y)) % abs(y)}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1141
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1142
     * @param x the dividend
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1143
     * @param y the divisor
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1144
     * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1145
     * @throws ArithmeticException if the divisor {@code y} is zero
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1146
     * @see #floorDiv(int, int)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1147
     * @since 1.8
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1148
     */
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1149
    public static int floorMod(int x, int y) {
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1150
        int r = x - floorDiv(x, y) * y;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1151
        return r;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1152
    }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1153
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1154
    /**
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1155
     * Returns the floor modulus of the {@code long} arguments.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1156
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1157
     * The floor modulus is {@code x - (floorDiv(x, y) * y)},
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1158
     * has the same sign as the divisor {@code y}, and
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1159
     * is in the range of {@code -abs(y) < r < +abs(y)}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1160
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1161
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1162
     * The relationship between {@code floorDiv} and {@code floorMod} is such that:
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1163
     * <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1164
     *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1165
     * </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1166
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1167
     * For examples, see {@link #floorMod(int, int)}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1168
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1169
     * @param x the dividend
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1170
     * @param y the divisor
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1171
     * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1172
     * @throws ArithmeticException if the divisor {@code y} is zero
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1173
     * @see #floorDiv(long, long)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1174
     * @since 1.8
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1175
     */
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1176
    public static long floorMod(long x, long y) {
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1177
        return x - floorDiv(x, y) * y;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1178
    }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1179
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1180
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * Returns the absolute value of an {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * <p>Note that if the argument is equal to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * {@link Integer#MIN_VALUE}, the most negative representable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * {@code int} value, the result is that same value, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    public static int abs(int a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        return (a < 0) ? -a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * Returns the absolute value of a {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * <p>Note that if the argument is equal to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * {@link Long#MIN_VALUE}, the most negative representable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * {@code long} value, the result is that same value, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    public static long abs(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return (a < 0) ? -a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * Returns the absolute value of a {@code float} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * <ul><li>If the argument is positive zero or negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * <li>If the argument is infinite, the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * <li>If the argument is NaN, the result is NaN.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * In other words, the result is the same as the value of the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    public static float abs(float a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        return (a <= 0.0F) ? 0.0F - a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * Returns the absolute value of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * <ul><li>If the argument is positive zero or negative zero, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * <li>If the argument is infinite, the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * <li>If the argument is NaN, the result is NaN.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * In other words, the result is the same as the value of the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    public static double abs(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        return (a <= 0.0D) ? 0.0D - a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * Returns the greater of two {@code int} values. That is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * result is the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * {@link Integer#MAX_VALUE}. If the arguments have the same value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    public static int max(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * Returns the greater of two {@code long} values. That is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * result is the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * {@link Long#MAX_VALUE}. If the arguments have the same value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    public static long max(long a, long b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1280
    // Use raw bit-wise conversions on guaranteed non-NaN arguments.
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1281
    private static long negativeZeroFloatBits  = Float.floatToRawIntBits(-0.0f);
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1282
    private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * Returns the greater of two {@code float} values.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * the result is the argument closer to positive infinity. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * negative zero to be strictly smaller than positive zero. If one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * argument is positive zero and the other negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    public static float max(float a, float b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1299
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1300
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1301
        if ((a == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1302
            (b == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1303
            (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1304
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * Returns the greater of two {@code double} values.  That
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * is, the result is the argument closer to positive infinity. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * the arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * negative zero to be strictly smaller than positive zero. If one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * argument is positive zero and the other negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    public static double max(double a, double b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1325
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1326
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1327
        if ((a == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1328
            (b == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1329
            (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1330
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * Returns the smaller of two {@code int} values. That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * the result the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * {@link Integer#MIN_VALUE}.  If the arguments have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * value, the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    public static int min(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * Returns the smaller of two {@code long} values. That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * the result is the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * {@link Long#MIN_VALUE}. If the arguments have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * value, the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    public static long min(long a, long b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * Returns the smaller of two {@code float} values.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * the result is the value closer to negative infinity. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * negative zero to be strictly smaller than positive zero.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * one argument is positive zero and the other is negative zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * the result is negative zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    public static float min(float a, float b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1379
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1380
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1381
        if ((a == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1382
            (b == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1383
            (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1384
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * Returns the smaller of two {@code double} values.  That
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * is, the result is the value closer to negative infinity. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * negative zero to be strictly smaller than positive zero. If one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * argument is positive zero and the other is negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * result is negative zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    public static double min(double a, double b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1405
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1406
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1407
        if ((a == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1408
            (b == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1409
            (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1410
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    /**
10122
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1417
     * 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
  1418
     * the last place, of a {@code double} value is the positive
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1419
     * distance between this floating-point value and the {@code
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1420
     * double} value next larger in magnitude.  Note that for non-NaN
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1421
     * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * <li> If the argument is positive or negative infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * <li> If the argument is positive or negative zero, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * {@code Double.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * the result is equal to 2<sup>971</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * @param d the floating-point value whose ulp is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * @return the size of an ulp of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    public static double ulp(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1440
        int exp = getExponent(d);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1441
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1442
        switch(exp) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1443
        case DoubleConsts.MAX_EXPONENT+1:       // NaN or infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1444
            return Math.abs(d);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1445
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1446
        case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1447
            return Double.MIN_VALUE;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1448
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1449
        default:
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1450
            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
  1451
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1452
            // 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
  1453
            exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1454
            if (exp >= DoubleConsts.MIN_EXPONENT) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1455
                return powerOfTwoD(exp);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1456
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1457
            else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1458
                // return a subnormal result; left shift integer
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1459
                // representation of Double.MIN_VALUE appropriate
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1460
                // number of positions
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1461
                return Double.longBitsToDouble(1L <<
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1462
                (exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) ));
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1463
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1464
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    /**
10122
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1468
     * 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
  1469
     * the last place, of a {@code float} value is the positive
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1470
     * distance between this floating-point value and the {@code
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1471
     * float} value next larger in magnitude.  Note that for non-NaN
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1472
     * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * <li> If the argument is positive or negative infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * <li> If the argument is positive or negative zero, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * {@code Float.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * the result is equal to 2<sup>104</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * @param f the floating-point value whose ulp is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * @return the size of an ulp of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    public static float ulp(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1491
        int exp = getExponent(f);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1492
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1493
        switch(exp) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1494
        case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1495
            return Math.abs(f);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1496
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1497
        case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1498
            return FloatConsts.MIN_VALUE;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1499
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1500
        default:
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1501
            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
  1502
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1503
            // 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
  1504
            exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1505
            if (exp >= FloatConsts.MIN_EXPONENT) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1506
                return powerOfTwoF(exp);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1507
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1508
            else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1509
                // return a subnormal result; left shift integer
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1510
                // representation of FloatConsts.MIN_VALUE appropriate
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1511
                // number of positions
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1512
                return Float.intBitsToFloat(1 <<
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1513
                (exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) ));
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
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * Returns the signum function of the argument; zero if the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * is zero, 1.0 if the argument is greater than zero, -1.0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * argument is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * <li> If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     *      result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * @param d the floating-point value whose signum is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * @return the signum function of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    public static double signum(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1536
        return (d == 0.0 || Double.isNaN(d))?d:copySign(1.0, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * Returns the signum function of the argument; zero if the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * is zero, 1.0f if the argument is greater than zero, -1.0f if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * argument is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * <li> If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     *      result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * @param f the floating-point value whose signum is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * @return the signum function of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    public static float signum(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1557
        return (f == 0.0f || Float.isNaN(f))?f:copySign(1.0f, f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * Returns the hyperbolic sine of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * The hyperbolic sine of <i>x</i> is defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * where <i>e</i> is {@linkplain Math#E Euler's number}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * <li>If the argument is infinite, then the result is an infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * with the same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * <p>The computed result must be within 2.5 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * @param   x The number whose hyperbolic sine is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * @return  The hyperbolic sine of {@code x}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    public static double sinh(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        return StrictMath.sinh(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * Returns the hyperbolic cosine of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * The hyperbolic cosine of <i>x</i> is defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * (<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>)/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * where <i>e</i> is {@linkplain Math#E Euler's number}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * <li>If the argument is infinite, then the result is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * <li>If the argument is zero, then the result is {@code 1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * <p>The computed result must be within 2.5 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * @param   x The number whose hyperbolic cosine is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * @return  The hyperbolic cosine of {@code x}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
    public static double cosh(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        return StrictMath.cosh(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * Returns the hyperbolic tangent of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * The hyperbolic tangent of <i>x</i> is defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * (<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
  1621
     * in other words, {@linkplain Math#sinh
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * that the absolute value of the exact tanh is always less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * {@code +1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * <li>If the argument is negative infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * {@code -1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * <p>The computed result must be within 2.5 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * The result of {@code tanh} for any finite input must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * an absolute value less than or equal to 1.  Note that once the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * exact result of tanh is within 1/2 of an ulp of the limit value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * of &plusmn;1, correctly signed &plusmn;{@code 1.0} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * @param   x The number whose hyperbolic tangent is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * @return  The hyperbolic tangent of {@code x}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    public static double tanh(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        return StrictMath.tanh(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * Returns sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * without intermediate overflow or underflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * <li> If either argument is infinite, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * <li> If either argument is NaN and neither argument is infinite,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * <p>The computed result must be within 1 ulp of the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * result.  If one parameter is held constant, the results must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * semi-monotonic in the other parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * @param x a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * @param y a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * without intermediate overflow or underflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    public static double hypot(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        return StrictMath.hypot(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * Returns <i>e</i><sup>x</sup>&nbsp;-1.  Note that for values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * <i>x</i> near 0, the exact sum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * {@code expm1(x)}&nbsp;+&nbsp;1 is much closer to the true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * <li>If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * <li>If the argument is negative infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * -1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * Results must be semi-monotonic.  The result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * {@code expm1} for any finite input must be greater than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * equal to {@code -1.0}.  Note that once the exact result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1 is within 1/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * ulp of the limit value -1, {@code -1.0} should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * @param   x   the exponent to raise <i>e</i> to in the computation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     *              <i>e</i><sup>{@code x}</sup>&nbsp;-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * @return  the value <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    public static double expm1(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        return StrictMath.expm1(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * Returns the natural logarithm of the sum of the argument and 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * Note that for small values {@code x}, the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * {@code log1p(x)} is much closer to the true result of ln(1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * + {@code x}) than the floating-point evaluation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * {@code log(1.0+x)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * <li>If the argument is NaN or less than -1, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * <li>If the argument is negative one, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * @param   x   a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * @return the value ln({@code x}&nbsp;+&nbsp;1), the natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * log of {@code x}&nbsp;+&nbsp;1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
    public static double log1p(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        return StrictMath.log1p(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * second floating-point argument.  Note that unlike the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * StrictMath#copySign(double, double) StrictMath.copySign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * method, this method does not require NaN {@code sign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * arguments to be treated as positive values; implementations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * permitted to treat some NaN arguments as positive and other NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * arguments as negative to allow greater performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * @param sign   the parameter providing the sign of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * @return a value with the magnitude of {@code magnitude}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * and the sign of {@code sign}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    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
  1777
        return Double.longBitsToDouble((Double.doubleToRawLongBits(sign) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1778
                                        (DoubleConsts.SIGN_BIT_MASK)) |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1779
                                       (Double.doubleToRawLongBits(magnitude) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1780
                                        (DoubleConsts.EXP_BIT_MASK |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1781
                                         DoubleConsts.SIGNIF_BIT_MASK)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     * second floating-point argument.  Note that unlike the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * StrictMath#copySign(float, float) StrictMath.copySign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * method, this method does not require NaN {@code sign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * arguments to be treated as positive values; implementations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * permitted to treat some NaN arguments as positive and other NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * arguments as negative to allow greater performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * @param sign   the parameter providing the sign of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * @return a value with the magnitude of {@code magnitude}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * and the sign of {@code sign}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    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
  1800
        return Float.intBitsToFloat((Float.floatToRawIntBits(sign) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1801
                                     (FloatConsts.SIGN_BIT_MASK)) |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1802
                                    (Float.floatToRawIntBits(magnitude) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1803
                                     (FloatConsts.EXP_BIT_MASK |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1804
                                      FloatConsts.SIGNIF_BIT_MASK)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * Returns the unbiased exponent used in the representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * {@code float}.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * <li>If the argument is NaN or infinite, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * {@link Float#MAX_EXPONENT} + 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * <li>If the argument is zero or subnormal, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * {@link Float#MIN_EXPONENT} -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * @param f a {@code float} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * @return the unbiased exponent of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    public static int getExponent(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1822
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1823
         * 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
  1824
         * 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
  1825
         * get true exponent value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1826
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1827
        return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1828
                (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * Returns the unbiased exponent used in the representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * {@code double}.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     * <li>If the argument is NaN or infinite, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     * {@link Double#MAX_EXPONENT} + 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     * <li>If the argument is zero or subnormal, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * {@link Double#MIN_EXPONENT} -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * @param d a {@code double} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * @return the unbiased exponent of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    public static int getExponent(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1846
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1847
         * 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
  1848
         * 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
  1849
         * get true exponent value.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1850
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1851
        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
  1852
                      (DoubleConsts.SIGNIFICAND_WIDTH - 1)) - DoubleConsts.EXP_BIAS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * Returns the floating-point number adjacent to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * argument in the direction of the second argument.  If both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * arguments compare as equal the second argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * <li> If either argument is a NaN, then NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * <li> If both arguments are signed zeros, {@code direction}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * is returned unchanged (as implied by the requirement of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * returning the second argument if the arguments compare as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * equal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * <li> If {@code start} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * &plusmn;{@link Double#MIN_VALUE} and {@code direction}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * has a value such that the result should have a smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * magnitude, then a zero with the same sign as {@code start}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * <li> If {@code start} is infinite and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * {@code direction} has a value such that the result should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * have a smaller magnitude, {@link Double#MAX_VALUE} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * <li> If {@code start} is equal to &plusmn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * {@link Double#MAX_VALUE} and {@code direction} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * value such that the result should have a larger magnitude, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * infinity with same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * @param start  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * @param direction value indicating which of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * {@code start}'s neighbors or {@code start} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * @return The floating-point number adjacent to {@code start} in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * direction of {@code direction}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
    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
  1896
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1897
         * The cases:
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1898
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1899
         * nextAfter(+infinity, 0)  == MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1900
         * nextAfter(+infinity, +infinity)  == +infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1901
         * nextAfter(-infinity, 0)  == -MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1902
         * nextAfter(-infinity, -infinity)  == -infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1903
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1904
         * are naturally handled without any additional testing
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1905
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1906
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1907
        // First check for NaN values
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1908
        if (Double.isNaN(start) || Double.isNaN(direction)) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1909
            // 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
  1910
            return start + direction;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1911
        } else if (start == direction) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1912
            return direction;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1913
        } else {        // start > direction or start < direction
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1914
            // 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
  1915
            // then bitwise convert start to integer.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1916
            long transducer = Double.doubleToRawLongBits(start + 0.0d);
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
            /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1919
             * IEEE 754 floating-point numbers are lexicographically
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1920
             * ordered if treated as signed- magnitude integers .
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1921
             * Since Java's integers are two's complement,
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1922
             * incrementing" the two's complement representation of a
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1923
             * logically negative floating-point value *decrements*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1924
             * the signed-magnitude representation. Therefore, when
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1925
             * the integer representation of a floating-point values
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1926
             * 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
  1927
             * 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
  1928
             * first .
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1929
             */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1930
            if (direction > start) { // Calculate next greater value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1931
                transducer = transducer + (transducer >= 0L ? 1L:-1L);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1932
            } else  { // Calculate next lesser value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1933
                assert direction < start;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1934
                if (transducer > 0L)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1935
                    --transducer;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1936
                else
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1937
                    if (transducer < 0L )
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1938
                        ++transducer;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1939
                    /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1940
                     * transducer==0, the result is -MIN_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1941
                     *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1942
                     * The transition from zero (implicitly
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1943
                     * positive) to the smallest negative
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1944
                     * signed magnitude value must be done
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1945
                     * explicitly.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1946
                     */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1947
                    else
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1948
                        transducer = DoubleConsts.SIGN_BIT_MASK | 1L;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1949
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1950
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1951
            return Double.longBitsToDouble(transducer);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1952
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * Returns the floating-point number adjacent to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * argument in the direction of the second argument.  If both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * arguments compare as equal a value equivalent to the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * <li> If either argument is a NaN, then NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * <li> If both arguments are signed zeros, a value equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * to {@code direction} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     * <li> If {@code start} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     * &plusmn;{@link Float#MIN_VALUE} and {@code direction}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     * has a value such that the result should have a smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * magnitude, then a zero with the same sign as {@code start}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     * <li> If {@code start} is infinite and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * {@code direction} has a value such that the result should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * have a smaller magnitude, {@link Float#MAX_VALUE} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     * <li> If {@code start} is equal to &plusmn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * {@link Float#MAX_VALUE} and {@code direction} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * value such that the result should have a larger magnitude, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     * infinity with same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * @param start  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * @param direction value indicating which of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     * {@code start}'s neighbors or {@code start} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     * be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     * @return The floating-point number adjacent to {@code start} in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     * direction of {@code direction}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
    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
  1995
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1996
         * The cases:
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1997
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1998
         * nextAfter(+infinity, 0)  == MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1999
         * nextAfter(+infinity, +infinity)  == +infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2000
         * nextAfter(-infinity, 0)  == -MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2001
         * nextAfter(-infinity, -infinity)  == -infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2002
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2003
         * are naturally handled without any additional testing
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2004
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2005
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2006
        // First check for NaN values
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2007
        if (Float.isNaN(start) || Double.isNaN(direction)) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2008
            // 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
  2009
            return start + (float)direction;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2010
        } else if (start == direction) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2011
            return (float)direction;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2012
        } else {        // start > direction or start < direction
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2013
            // 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
  2014
            // then bitwise convert start to integer.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2015
            int transducer = Float.floatToRawIntBits(start + 0.0f);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2016
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2017
            /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2018
             * IEEE 754 floating-point numbers are lexicographically
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2019
             * ordered if treated as signed- magnitude integers .
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2020
             * Since Java's integers are two's complement,
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2021
             * incrementing" the two's complement representation of a
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2022
             * logically negative floating-point value *decrements*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2023
             * the signed-magnitude representation. Therefore, when
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2024
             * the integer representation of a floating-point values
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2025
             * 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
  2026
             * 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
  2027
             * first.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2028
             */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2029
            if (direction > start) {// Calculate next greater value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2030
                transducer = transducer + (transducer >= 0 ? 1:-1);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2031
            } else  { // Calculate next lesser value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2032
                assert direction < start;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2033
                if (transducer > 0)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2034
                    --transducer;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2035
                else
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2036
                    if (transducer < 0 )
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2037
                        ++transducer;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2038
                    /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2039
                     * transducer==0, the result is -MIN_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2040
                     *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2041
                     * The transition from zero (implicitly
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2042
                     * positive) to the smallest negative
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2043
                     * signed magnitude value must be done
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2044
                     * explicitly.
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
                    else
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2047
                        transducer = FloatConsts.SIGN_BIT_MASK | 1;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2048
            }
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
            return Float.intBitsToFloat(transducer);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2051
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * Returns the floating-point value adjacent to {@code d} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * the direction of positive infinity.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * semantically equivalent to {@code nextAfter(d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * implementation may run faster than its equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * {@code nextAfter} call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * <li> If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * <li> If the argument is zero, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * {@link Double#MIN_VALUE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * @param d starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     * @return The adjacent floating-point value closer to positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
    public static double nextUp(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2080
        if( Double.isNaN(d) || d == Double.POSITIVE_INFINITY)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2081
            return d;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2082
        else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2083
            d += 0.0d;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2084
            return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2085
                                           ((d >= 0.0d)?+1L:-1L));
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2086
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * Returns the floating-point value adjacent to {@code f} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * the direction of positive infinity.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * semantically equivalent to {@code nextAfter(f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * implementation may run faster than its equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * {@code nextAfter} call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * <li> If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * <li> If the argument is zero, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * {@link Float#MIN_VALUE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     * @param f starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     * @return The adjacent floating-point value closer to positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
    public static float nextUp(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2115
        if( Float.isNaN(f) || f == FloatConsts.POSITIVE_INFINITY)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2116
            return f;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2117
        else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2118
            f += 0.0f;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2119
            return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2120
                                        ((f >= 0.0f)?+1:-1));
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2121
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
10608
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2124
    /**
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2125
     * Returns the floating-point value adjacent to {@code d} in
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2126
     * the direction of negative infinity.  This method is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2127
     * semantically equivalent to {@code nextAfter(d,
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2128
     * Double.NEGATIVE_INFINITY)}; however, a
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2129
     * {@code nextDown} implementation may run faster than its
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2130
     * equivalent {@code nextAfter} call.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2131
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2132
     * <p>Special Cases:
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2133
     * <ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2134
     * <li> If the argument is NaN, the result is NaN.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2135
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2136
     * <li> If the argument is negative infinity, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2137
     * negative infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2138
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2139
     * <li> If the argument is zero, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2140
     * {@code -Double.MIN_VALUE}
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2141
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2142
     * </ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2143
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2144
     * @param d  starting floating-point value
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2145
     * @return The adjacent floating-point value closer to negative
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2146
     * infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2147
     * @since 1.8
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2148
     */
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2149
    public static double nextDown(double d) {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2150
        if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2151
            return d;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2152
        else {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2153
            if (d == 0.0)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2154
                return -Double.MIN_VALUE;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2155
            else
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2156
                return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2157
                                               ((d > 0.0d)?-1L:+1L));
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2158
        }
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2159
    }
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2160
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2161
    /**
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2162
     * Returns the floating-point value adjacent to {@code f} in
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2163
     * the direction of negative infinity.  This method is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2164
     * semantically equivalent to {@code nextAfter(f,
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2165
     * Float.NEGATIVE_INFINITY)}; however, a
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2166
     * {@code nextDown} implementation may run faster than its
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2167
     * equivalent {@code nextAfter} call.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2168
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2169
     * <p>Special Cases:
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2170
     * <ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2171
     * <li> If the argument is NaN, the result is NaN.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2172
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2173
     * <li> If the argument is negative infinity, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2174
     * negative infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2175
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2176
     * <li> If the argument is zero, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2177
     * {@code -Float.MIN_VALUE}
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2178
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2179
     * </ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2180
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2181
     * @param f  starting floating-point value
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2182
     * @return The adjacent floating-point value closer to negative
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2183
     * infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2184
     * @since 1.8
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2185
     */
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2186
    public static float nextDown(float f) {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2187
        if (Float.isNaN(f) || f == Float.NEGATIVE_INFINITY)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2188
            return f;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2189
        else {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2190
            if (f == 0.0f)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2191
                return -Float.MIN_VALUE;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2192
            else
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2193
                return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2194
                                            ((f > 0.0f)?-1:+1));
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2195
        }
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2196
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  2199
     * Returns {@code d} &times;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
     * 2<sup>{@code scaleFactor}</sup> rounded as if performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     * by a single correctly rounded floating-point multiply to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * member of the double value set.  See the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * Language Specification for a discussion of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     * value sets.  If the exponent of the result is between {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * answer is calculated exactly.  If the exponent of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     * would be larger than {@code Double.MAX_EXPONENT}, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * infinity is returned.  Note that if the result is subnormal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     * precision may be lost; that is, when {@code scalb(x, n)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * <i>x</i>.  When the result is non-NaN, the result has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * sign as {@code d}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     * <li> If the first argument is NaN, NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * <li> If the first argument is infinite, then an infinity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * same sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * <li> If the first argument is zero, then a zero of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     * @param d number to be scaled by a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     * @param scaleFactor power of 2 used to scale {@code d}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     * @return {@code d} &times; 2<sup>{@code scaleFactor}</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
    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
  2229
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2230
         * 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
  2231
         * 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
  2232
         * 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
  2233
         * 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
  2234
         * finite or overflow regardless of the operation ordering.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2235
         * 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
  2236
         * particular ordering must be used.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2237
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2238
         * When scaling down, the multiply-store operations are
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2239
         * 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
  2240
         * multiply-stores to return subnormal results.  If one
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2241
         * 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
  2242
         * 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
  2243
         * by 2 ^ (scaleFactor % n) and then multiplying several
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2244
         * 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
  2245
         * 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
  2246
         * 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
  2247
         * 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
  2248
         * 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
  2249
         * 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
  2250
         * 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
  2251
         * set.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2252
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2253
         * 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
  2254
         * 2^MIN_EXPONENT and then by 2 ^ (scaleFactor %
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2255
         * 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
  2256
         * 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
  2257
         * 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
  2258
         * 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
  2259
         * result would be subnormal.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2260
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2261
         * 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
  2262
         * without any undue performance burden, there is no
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2263
         * 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
  2264
         * scalb.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2265
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2266
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2267
        // 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
  2268
        // 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
  2269
        // 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
  2270
        // additional power of two which is reflected here
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2271
        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
  2272
                              DoubleConsts.SIGNIFICAND_WIDTH + 1;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2273
        int exp_adjust = 0;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2274
        int scale_increment = 0;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2275
        double exp_delta = Double.NaN;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2276
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2277
        // 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
  2278
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2279
        if(scaleFactor < 0) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2280
            scaleFactor = Math.max(scaleFactor, -MAX_SCALE);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2281
            scale_increment = -512;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2282
            exp_delta = twoToTheDoubleScaleDown;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2283
        }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2284
        else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2285
            scaleFactor = Math.min(scaleFactor, MAX_SCALE);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2286
            scale_increment = 512;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2287
            exp_delta = twoToTheDoubleScaleUp;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2288
        }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2289
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2290
        // Calculate (scaleFactor % +/-512), 512 = 2^9, using
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2291
        // technique from "Hacker's Delight" section 10-2.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2292
        int t = (scaleFactor >> 9-1) >>> 32 - 9;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2293
        exp_adjust = ((scaleFactor + t) & (512 -1)) - t;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2294
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2295
        d *= powerOfTwoD(exp_adjust);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2296
        scaleFactor -= exp_adjust;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2297
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2298
        while(scaleFactor != 0) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2299
            d *= exp_delta;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2300
            scaleFactor -= scale_increment;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2301
        }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2302
        return d;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  2306
     * Returns {@code f} &times;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     * 2<sup>{@code scaleFactor}</sup> rounded as if performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     * by a single correctly rounded floating-point multiply to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * member of the float value set.  See the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     * Language Specification for a discussion of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     * value sets.  If the exponent of the result is between {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * answer is calculated exactly.  If the exponent of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * would be larger than {@code Float.MAX_EXPONENT}, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * infinity is returned.  Note that if the result is subnormal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     * precision may be lost; that is, when {@code scalb(x, n)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     * <i>x</i>.  When the result is non-NaN, the result has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     * sign as {@code f}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * <li> If the first argument is NaN, NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * <li> If the first argument is infinite, then an infinity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     * same sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     * <li> If the first argument is zero, then a zero of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
     * sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     * @param f number to be scaled by a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * @param scaleFactor power of 2 used to scale {@code f}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
    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
  2336
        // 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
  2337
        // 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
  2338
        // 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
  2339
        // additional power of two which is reflected here
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2340
        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
  2341
                              FloatConsts.SIGNIFICAND_WIDTH + 1;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2342
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2343
        // 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
  2344
        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
  2345
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2346
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2347
         * 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
  2348
         * exponent range and + float -> double conversion is exact
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2349
         * the multiplication below will be exact. Therefore, the
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2350
         * 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
  2351
         * 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
  2352
         * 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
  2353
         * 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
  2354
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2355
        return (float)((double)f*powerOfTwoD(scaleFactor));
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2356
    }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2357
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2358
    // Constants used in scalb
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2359
    static double twoToTheDoubleScaleUp = powerOfTwoD(512);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2360
    static double twoToTheDoubleScaleDown = powerOfTwoD(-512);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2361
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2362
    /**
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2363
     * 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
  2364
     */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2365
    static double powerOfTwoD(int n) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2366
        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
  2367
        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
  2368
                                        (DoubleConsts.SIGNIFICAND_WIDTH-1))
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2369
                                       & DoubleConsts.EXP_BIT_MASK);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2370
    }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2371
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2372
    /**
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2373
     * 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
  2374
     */
11510
f52f50e63c9b 7123649: Remove public modifier from Math.powerOfTwoF.
darcy
parents: 10608
diff changeset
  2375
    static float powerOfTwoF(int n) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2376
        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
  2377
        return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2378
                                     (FloatConsts.SIGNIFICAND_WIDTH-1))
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2379
                                    & FloatConsts.EXP_BIT_MASK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
}