jdk/src/share/classes/sun/misc/FpUtils.java
author darcy
Sun, 18 Sep 2011 18:14:07 -0700
changeset 10598 efd29b4b3e67
parent 9734 b33a24d77590
child 10608 7cfca36fc79b
permissions -rw-r--r--
7091682: Move sun.misc.FpUtils code into java.lang.Math Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.misc.FloatConsts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.misc.DoubleConsts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
    32
 * The class {@code FpUtils} contains static utility methods for
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
    33
 * manipulating and inspecting {@code float} and
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
    34
 * {@code double} floating-point numbers.  These methods include
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * functionality recommended or required by the IEEE 754
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * floating-point standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class FpUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * The methods in this class are reasonably implemented using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * direct or indirect bit-level manipulation of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * values.  However, having access to the IEEE 754 recommended
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * functions would obviate the need for most programmers to engage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * in floating-point bit-twiddling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * An IEEE 754 number has three fields, from most significant bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * to to least significant, sign, exponent, and significand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *  msb                                lsb
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * [sign|exponent|  fractional_significand]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Using some encoding cleverness, explained below, the high order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * bit of the logical significand does not need to be explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * stored, thus "fractional_significand" instead of simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * "significand" in the figure above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * For finite normal numbers, the numerical value encoded is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * (-1)^sign * 2^(exponent)*(1.fractional_significand)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Most finite floating-point numbers are normalized; the exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * value is reduced until the leading significand bit is 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Therefore, the leading 1 is redundant and is not explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * stored.  If a numerical value is so small it cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * normalized, it has a subnormal representation. Subnormal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * numbers don't have a leading 1 in their significand; subnormals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * are encoding using a special exponent value.  In other words,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * the high-order bit of the logical significand can be elided in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * from the representation in either case since the bit's value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * implicit from the exponent value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The exponent field uses a biased representation; if the bits of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * the exponent are interpreted as a unsigned integer E, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * exponent represented is E - E_bias where E_bias depends on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * floating-point format.  E can range between E_min and E_max,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * constants which depend on the floating-point format.  E_min and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * E_max are -126 and +127 for float, -1022 and +1023 for double.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The 32-bit float format has 1 sign bit, 8 exponent bits, and 23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * bits for the significand (which is logically 24 bits wide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * because of the implicit bit).  The 64-bit double format has 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * sign bit, 11 exponent bits, and 52 bits for the significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * (logically 53 bits).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Subnormal numbers and zero have the special exponent value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * E_min -1; the numerical value represented by a subnormal is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * (-1)^sign * 2^(E_min)*(0.fractional_significand)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Zero is represented by all zero bits in the exponent and all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * zero bits in the significand; zero can have either sign.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Infinity and NaN are encoded using the exponent value E_max +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * 1.  Signed infinities have all significand bits zero; NaNs have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * at least one non-zero significand bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * The details of IEEE 754 floating-point encoding will be used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * the methods below without further comment.  For further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * exposition on IEEE 754 numbers, see "IEEE Standard for Binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Floating-Point Arithmetic" ANSI/IEEE Std 754-1985 or William
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Kahan's "Lecture Notes on the Status of IEEE Standard 754 for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Binary Floating-Point Arithmetic",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * http://www.cs.berkeley.edu/~wkahan/ieee754status/ieee754.ps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Many of this class's methods are members of the set of IEEE 754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * recommended functions or similar functions recommended or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * required by IEEE 754R.  Discussion of various implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * techniques for these functions have occurred in:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * W.J. Cody and Jerome T. Coonen, "Algorithm 772 Functions to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Support the IEEE Standard for Binary Floating-Point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Arithmetic," ACM Transactions on Mathematical Software,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * vol. 19, no. 4, December 1993, pp. 443-451.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Joseph D. Darcy, "Writing robust IEEE recommended functions in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * ``100% Pure Java''(TM)," University of California, Berkeley
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * technical report UCB//CSD-98-1009.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Don't let anyone instantiate this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private FpUtils() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    // Helper Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // The following helper methods are used in the implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // the public recommended functions; they generally omit certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // tests for exception cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   135
     * Returns unbiased exponent of a {@code double}.
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   136
     * @deprecated Use Math.getExponent.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   138
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static int getExponent(double d){
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   140
        return Math.getExponent(d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   144
     * Returns unbiased exponent of a {@code float}.
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   145
     * @deprecated Use Math.getExponent.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   147
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static int getExponent(float f){
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   149
        return Math.getExponent(f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * second floating-point argument.  Note that unlike the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * FpUtils#copySign(double, double) copySign} method, this method
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   157
     * does not require NaN {@code sign} arguments to be treated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * as positive values; implementations are permitted to treat some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * NaN arguments as positive and other NaN arguments as negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * to allow greater performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param sign   the parameter providing the sign of the result
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   164
     * @return a value with the magnitude of {@code magnitude}
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   165
     * and the sign of {@code sign}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @author Joseph D. Darcy
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   167
     * @deprecated Use Math.copySign.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   169
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public static double rawCopySign(double magnitude, double sign) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   171
        return Math.copySign(magnitude, sign);
2
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 first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * second floating-point argument.  Note that unlike the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * FpUtils#copySign(float, float) copySign} method, this method
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   178
     * does not require NaN {@code sign} arguments to be treated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * as positive values; implementations are permitted to treat some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * NaN arguments as positive and other NaN arguments as negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * to allow greater performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param sign   the parameter providing the sign of the result
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   185
     * @return a value with the magnitude of {@code magnitude}
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   186
     * and the sign of {@code sign}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @author Joseph D. Darcy
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   188
     * @deprecated Use Math.copySign.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   190
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public static float rawCopySign(float magnitude, float sign) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   192
        return Math.copySign(magnitude, sign);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /* ***************************************************************** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   198
     * Returns {@code true} if the argument is a finite
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   199
     * floating-point value; returns {@code false} otherwise (for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * NaN and infinity arguments).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   202
     * @param d the {@code double} value to be tested
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   203
     * @return {@code true} if the argument is a finite
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   204
     * floating-point value, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public static boolean isFinite(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return Math.abs(d) <= DoubleConsts.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   211
     * Returns {@code true} if the argument is a finite
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   212
     * floating-point value; returns {@code false} otherwise (for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * NaN and infinity arguments).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   215
     * @param f the {@code float} value to be tested
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   216
     * @return {@code true} if the argument is a finite
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   217
     * floating-point value, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     public static boolean isFinite(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return Math.abs(f) <= FloatConsts.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   224
     * Returns {@code true} if the specified number is infinitely
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   225
     * large in magnitude, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <p>Note that this method is equivalent to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Double#isInfinite(double) Double.isInfinite} method; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * functionality is included in this class for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param   d   the value to be tested.
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   232
     * @return  {@code true} if the value of the argument is positive
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   233
     *          infinity or negative infinity; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static boolean isInfinite(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return Double.isInfinite(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   240
     * Returns {@code true} if the specified number is infinitely
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   241
     * large in magnitude, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>Note that this method is equivalent to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Float#isInfinite(float) Float.isInfinite} method; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * functionality is included in this class for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param   f   the value to be tested.
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   248
     * @return  {@code true} if the argument is positive infinity or
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   249
     *          negative infinity; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     public static boolean isInfinite(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         return Float.isInfinite(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   256
     * Returns {@code true} if the specified number is a
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   257
     * Not-a-Number (NaN) value, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <p>Note that this method is equivalent to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Double#isNaN(double) Double.isNaN} method; the functionality is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * included in this class for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param   d   the value to be tested.
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   264
     * @return  {@code true} if the value of the argument is NaN;
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   265
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public static boolean isNaN(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return Double.isNaN(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   272
     * Returns {@code true} if the specified number is a
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   273
     * Not-a-Number (NaN) value, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <p>Note that this method is equivalent to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Float#isNaN(float) Float.isNaN} method; the functionality is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * included in this class for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param   f   the value to be tested.
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   280
     * @return  {@code true} if the argument is NaN;
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   281
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     public static boolean isNaN(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return Float.isNaN(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   288
     * Returns {@code true} if the unordered relation holds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * between the two arguments.  When two floating-point values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * unordered, one value is neither less than, equal to, nor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * greater than the other.  For the unordered relation to be true,
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   292
     * at least one argument must be a {@code NaN}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param arg1      the first argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param arg2      the second argument
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   296
     * @return {@code true} if at least one argument is a NaN,
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   297
     * {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public static boolean isUnordered(double arg1, double arg2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return isNaN(arg1) || isNaN(arg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   304
     * Returns {@code true} if the unordered relation holds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * between the two arguments.  When two floating-point values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * unordered, one value is neither less than, equal to, nor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * greater than the other.  For the unordered relation to be true,
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   308
     * at least one argument must be a {@code NaN}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param arg1      the first argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param arg2      the second argument
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   312
     * @return {@code true} if at least one argument is a NaN,
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   313
     * {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     public static boolean isUnordered(float arg1, float arg2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return isNaN(arg1) || isNaN(arg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   320
     * Returns unbiased exponent of a {@code double}; for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * subnormal values, the number is treated as if it were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * normalized.  That is for all finite, non-zero, positive numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * always in the range [1, 2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @param d floating-point number whose exponent is to be extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @return unbiased exponent of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public static int ilogb(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        int exponent = getExponent(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        switch (exponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        case DoubleConsts.MAX_EXPONENT+1:       // NaN or infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if( isNaN(d) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                return (1<<30);         // 2^30
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            else // infinite value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                return (1<<28);         // 2^28
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if(d == 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                return -(1<<28);        // -(2^28)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                long transducer = Double.doubleToRawLongBits(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                 * To avoid causing slow arithmetic on subnormals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                 * the scaling to determine when d's significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                 * is normalized is done in integer arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                 * (there must be at least one "1" bit in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                 * significand since zero has been screened out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                // isolate significand bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                transducer &= DoubleConsts.SIGNIF_BIT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                assert(transducer != 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                // This loop is simple and functional. We might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                // able to do something more clever that was faster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                // e.g. number of leading zero detection on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                // (transducer << (# exponent and sign bits).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                while (transducer <
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                       (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    transducer *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    exponent--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                exponent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                assert( exponent >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        exponent < DoubleConsts.MIN_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            assert( exponent >= DoubleConsts.MIN_EXPONENT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    exponent <= DoubleConsts.MAX_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   390
     * Returns unbiased exponent of a {@code float}; for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * subnormal values, the number is treated as if it were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * normalized.  That is for all finite, non-zero, positive numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * always in the range [1, 2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param f floating-point number whose exponent is to be extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @return unbiased exponent of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     public static int ilogb(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        int exponent = getExponent(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        switch (exponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if( isNaN(f) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                return (1<<30);         // 2^30
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            else // infinite value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                return (1<<28);         // 2^28
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if(f == 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                return -(1<<28);        // -(2^28)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                int transducer = Float.floatToRawIntBits(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                 * To avoid causing slow arithmetic on subnormals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                 * the scaling to determine when f's significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                 * is normalized is done in integer arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                 * (there must be at least one "1" bit in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                 * significand since zero has been screened out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                // isolate significand bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                transducer &= FloatConsts.SIGNIF_BIT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                assert(transducer != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                // This loop is simple and functional. We might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                // able to do something more clever that was faster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                // e.g. number of leading zero detection on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                // (transducer << (# exponent and sign bits).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                while (transducer <
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                       (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    transducer *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    exponent--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                exponent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                assert( exponent >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        exponent < FloatConsts.MIN_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            assert( exponent >= FloatConsts.MIN_EXPONENT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    exponent <= FloatConsts.MAX_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * The scalb operation should be reasonably fast; however, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * are tradeoffs in writing a method to minimize the worst case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * performance and writing a method to minimize the time for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * expected common inputs.  Some processors operate very slowly on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * subnormal operands, taking hundreds or thousands of cycles for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * one floating-point add or multiply as opposed to, say, four
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * cycles for normal operands.  For processors with very slow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * subnormal execution, scalb would be fastest if written entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * with integer operations; in other words, scalb would need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * include the logic of performing correct rounding of subnormal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * values.  This could be reasonably done in at most a few hundred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * cycles.  However, this approach may penalize normal operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * since at least the exponent of the floating-point argument must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * be examined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * The approach taken in this implementation is a compromise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Floating-point multiplication is used to do most of the work;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * but knowingly multiplying by a subnormal scaling factor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * avoided.  However, the floating-point argument is not examined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * to see whether or not it is subnormal since subnormal inputs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * are assumed to be rare.  At most three multiplies are needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * scale from the largest to smallest exponent ranges (scaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * down, at most two multiplies are needed if subnormal scaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * factors are allowed).  However, in this implementation an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * expensive integer remainder operation is avoided at the cost of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * requiring five floating-point multiplies in the worst case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * which should still be a performance win.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * If scaling of entire arrays is a concern, it would probably be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * more efficient to provide a double[] scalb(double[], int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * version of scalb to avoid having to recompute the needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * scaling factors for each floating-point value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   496
     * Return {@code d} &times;
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   497
     * 2<sup>{@code scale_factor}</sup> rounded as if performed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * by a single correctly rounded floating-point multiply to a
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   499
     * member of the double value set.  See section 4.2.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   500
     * <cite>The Java&trade; Language Specification</cite>
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   501
     * for a discussion of floating-point
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * value sets.  If the exponent of the result is between the
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   503
     * {@code double}'s minimum exponent and maximum exponent,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * the answer is calculated exactly.  If the exponent of the
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   505
     * result would be larger than {@code doubles}'s maximum
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * exponent, an infinity is returned.  Note that if the result is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   507
     * subnormal, precision may be lost; that is, when {@code scalb(x,
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   508
     * n)} is subnormal, {@code scalb(scalb(x, n), -n)} may
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * not equal <i>x</i>.  When the result is non-NaN, the result has
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   510
     * the same sign as {@code d}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <li> If the first argument is NaN, NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <li> If the first argument is infinite, then an infinity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * same sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * <li> If the first argument is zero, then a zero of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @param d number to be scaled by a power of two.
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   523
     * @param scale_factor power of 2 used to scale {@code d}
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   524
     * @return {@code d * }2<sup>{@code scale_factor}</sup>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @author Joseph D. Darcy
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   526
     * @deprecated Use Math.scalb.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   528
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    public static double scalb(double d, int scale_factor) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   530
        return Math.scalb(d, scale_factor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   534
     * Return {@code f} &times;
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   535
     * 2<sup>{@code scale_factor}</sup> rounded as if performed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * by a single correctly rounded floating-point multiply to a
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   537
     * member of the float value set.  See section 4.2.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   538
     * <cite>The Java&trade; Language Specification</cite>
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   539
     * for a discussion of floating-point
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   540
     * value sets. If the exponent of the result is between the
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   541
     * {@code float}'s minimum exponent and maximum exponent, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * answer is calculated exactly.  If the exponent of the result
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   543
     * would be larger than {@code float}'s maximum exponent, an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * infinity is returned.  Note that if the result is subnormal,
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   545
     * precision may be lost; that is, when {@code scalb(x, n)}
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   546
     * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * <i>x</i>.  When the result is non-NaN, the result has the same
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   548
     * sign as {@code f}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * <li> If the first argument is NaN, NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <li> If the first argument is infinite, then an infinity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * same sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * <li> If the first argument is zero, then a zero of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param f number to be scaled by a power of two.
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   561
     * @param scale_factor power of 2 used to scale {@code f}
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   562
     * @return {@code f * }2<sup>{@code scale_factor}</sup>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @author Joseph D. Darcy
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   564
     * @deprecated Use Math.scalb.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   566
    @Deprecated
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   567
    public static float scalb(float f, int scale_factor) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   568
        return Math.scalb(f, scale_factor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Returns the floating-point number adjacent to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * argument in the direction of the second argument.  If both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * arguments compare as equal the second argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <li> If either argument is a NaN, then NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   581
     * <li> If both arguments are signed zeros, {@code direction}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * is returned unchanged (as implied by the requirement of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * returning the second argument if the arguments compare as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * equal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   586
     * <li> If {@code start} is
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   587
     * &plusmn;{@code Double.MIN_VALUE} and {@code direction}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * has a value such that the result should have a smaller
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   589
     * magnitude, then a zero with the same sign as {@code start}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   592
     * <li> If {@code start} is infinite and
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   593
     * {@code direction} has a value such that the result should
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   594
     * have a smaller magnitude, {@code Double.MAX_VALUE} with the
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   595
     * same sign as {@code start} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   597
     * <li> If {@code start} is equal to &plusmn;
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   598
     * {@code Double.MAX_VALUE} and {@code direction} has a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * value such that the result should have a larger magnitude, an
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   600
     * infinity with same sign as {@code start} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @param start     starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param direction value indicating which of
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   605
     * {@code start}'s neighbors or {@code start} should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * be returned
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   607
     * @return The floating-point number adjacent to {@code start} in the
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   608
     * direction of {@code direction}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @author Joseph D. Darcy
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   610
     * @deprecated Use Math.nextAfter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   612
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public static double nextAfter(double start, double direction) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   614
        return Math.nextAfter(start, direction);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Returns the floating-point number adjacent to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * argument in the direction of the second argument.  If both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * arguments compare as equal, the second argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <li> If either argument is a NaN, then NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   627
     * <li> If both arguments are signed zeros, a {@code float}
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   628
     * zero with the same sign as {@code direction} is returned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * (as implied by the requirement of returning the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * if the arguments compare as equal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   632
     * <li> If {@code start} is
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   633
     * &plusmn;{@code Float.MIN_VALUE} and {@code direction}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * has a value such that the result should have a smaller
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   635
     * magnitude, then a zero with the same sign as {@code start}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   638
     * <li> If {@code start} is infinite and
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   639
     * {@code direction} has a value such that the result should
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   640
     * have a smaller magnitude, {@code Float.MAX_VALUE} with the
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   641
     * same sign as {@code start} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   643
     * <li> If {@code start} is equal to &plusmn;
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   644
     * {@code Float.MAX_VALUE} and {@code direction} has a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * value such that the result should have a larger magnitude, an
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   646
     * infinity with same sign as {@code start} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @param start     starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @param direction value indicating which of
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   651
     * {@code start}'s neighbors or {@code start} should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * be returned
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   653
     * @return The floating-point number adjacent to {@code start} in the
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   654
     * direction of {@code direction}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @author Joseph D. Darcy
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   656
     * @deprecated Use Math.nextAfter.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   658
    @Deprecated
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   659
    public static float nextAfter(float start, double direction) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   660
        return Math.nextAfter(start, direction);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   664
     * Returns the floating-point value adjacent to {@code d} in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * the direction of positive infinity.  This method is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   666
     * semantically equivalent to {@code nextAfter(d,
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   667
     * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * implementation may run faster than its equivalent
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   669
     * {@code nextAfter} call.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * <li> If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * <li> If the argument is zero, the result is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   679
     * {@code Double.MIN_VALUE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @param d  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @return The adjacent floating-point value closer to positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @author Joseph D. Darcy
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   687
     * @deprecated use Math.nextUp.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   689
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public static double nextUp(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   691
        return Math.nextUp(d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   695
     * Returns the floating-point value adjacent to {@code f} in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * the direction of positive infinity.  This method is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   697
     * semantically equivalent to {@code nextAfter(f,
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   698
     * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * implementation may run faster than its equivalent
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   700
     * {@code nextAfter} call.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <li> If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <li> If the argument is zero, the result is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   710
     * {@code Float.MIN_VALUE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @param f  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @return The adjacent floating-point value closer to positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @author Joseph D. Darcy
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   718
     * @deprecated Use Math.nextUp.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   720
    @Deprecated
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   721
    public static float nextUp(float f) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   722
        return Math.nextUp(f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   726
     * Returns the floating-point value adjacent to {@code d} in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * the direction of negative infinity.  This method is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   728
     * semantically equivalent to {@code nextAfter(d,
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   729
     * Double.NEGATIVE_INFINITY)}; however, a
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   730
     * {@code nextDown} implementation may run faster than its
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   731
     * equivalent {@code nextAfter} call.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * <li> If the argument is negative infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * <li> If the argument is zero, the result is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   741
     * {@code -Double.MIN_VALUE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @param d  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @return The adjacent floating-point value closer to negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    public static double nextDown(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        if( isNaN(d) || d == Double.NEGATIVE_INFINITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            if (d == 0.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                return -Double.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                                               ((d > 0.0d)?-1L:+1L));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   763
     * Returns the floating-point value adjacent to {@code f} in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * the direction of negative infinity.  This method is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   765
     * semantically equivalent to {@code nextAfter(f,
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   766
     * Float.NEGATIVE_INFINITY)}; however, a
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   767
     * {@code nextDown} implementation may run faster than its
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   768
     * equivalent {@code nextAfter} call.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * <li> If the argument is negative infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <li> If the argument is zero, the result is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   778
     * {@code -Float.MIN_VALUE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @param f  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @return The adjacent floating-point value closer to negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    public static double nextDown(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if( isNaN(f) || f == Float.NEGATIVE_INFINITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if (f == 0.0f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                return -Float.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                                            ((f > 0.0f)?-1:+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * second floating-point argument.  For this method, a NaN
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   802
     * {@code sign} argument is always treated as if it were
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @param sign   the parameter providing the sign of the result
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   807
     * @return a value with the magnitude of {@code magnitude}
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   808
     * and the sign of {@code sign}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @since 1.5
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   811
     * @deprecated Use StrictMath.copySign.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   813
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    public static double copySign(double magnitude, double sign) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   815
        return StrictMath.copySign(magnitude, sign);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * second floating-point argument.  For this method, a NaN
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   821
     * {@code sign} argument is always treated as if it were
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @param sign   the parameter providing the sign of the result
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   826
     * @return a value with the magnitude of {@code magnitude}
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   827
     * and the sign of {@code sign}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @author Joseph D. Darcy
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   829
     * @deprecated Use StrictMath.copySign.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   831
    @Deprecated
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   832
    public static float copySign(float magnitude, float sign) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   833
        return StrictMath.copySign(magnitude, sign);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * Returns the size of an ulp of the argument.  An ulp of a
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   838
     * {@code double} value is the positive distance between this
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   839
     * floating-point value and the {@code double} value next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * larger in magnitude.  Note that for non-NaN <i>x</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * <li> If the argument is positive or negative infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * <li> If the argument is positive or negative zero, then the result is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   849
     * {@code Double.MIN_VALUE}.
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   850
     * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * the result is equal to 2<sup>971</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * @param d the floating-point value whose ulp is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @return the size of an ulp of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @since 1.5
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   858
     * @deprecated Use Math.ulp.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   860
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    public static double ulp(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   862
        return Math.ulp(d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Returns the size of an ulp of the argument.  An ulp of a
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   867
     * {@code float} value is the positive distance between this
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   868
     * floating-point value and the {@code float} value next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * larger in magnitude.  Note that for non-NaN <i>x</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * <li> If the argument is positive or negative infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * <li> If the argument is positive or negative zero, then the result is
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   878
     * {@code Float.MIN_VALUE}.
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   879
     * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * the result is equal to 2<sup>104</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @param f the floating-point value whose ulp is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @return the size of an ulp of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @since 1.5
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   887
     * @deprecated Use Math.ulp.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   889
     @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     public static float ulp(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   891
        return Math.ulp(f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Returns the signum function of the argument; zero if the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * is zero, 1.0 if the argument is greater than zero, -1.0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * argument is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * <li> If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *      result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @param d the floating-point value whose signum is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * @return the signum function of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @since 1.5
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   910
     * @deprecated Use Math.signum.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   912
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    public static double signum(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   914
        return Math.signum(d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * Returns the signum function of the argument; zero if the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * is zero, 1.0f if the argument is greater than zero, -1.0f if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * argument is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * <li> If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *      result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @param f the floating-point value whose signum is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * @return the signum function of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * @since 1.5
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   933
     * @deprecated Use Math.signum.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     */
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   935
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    public static float signum(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9734
diff changeset
   937
        return Math.signum(f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
}