jdk/src/share/classes/sun/misc/FpUtils.java
author alanb
Tue, 08 Feb 2011 19:31:44 +0000
changeset 8189 5fd29d2cbc4b
parent 7517 7303bc0e78d6
child 9266 121fb370f179
permissions -rw-r--r--
4421494: infinite loop while parsing double literal Reviewed-by: darcy, alanb Contributed-by: dmitry.nadezhin@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010 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
    // Constants used in scalb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static double twoToTheDoubleScaleUp = powerOfTwoD(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static double twoToTheDoubleScaleDown = powerOfTwoD(-512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // Helper Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // The following helper methods are used in the implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // the public recommended functions; they generally omit certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // tests for exception cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   139
     * Returns unbiased exponent of a {@code double}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static int getExponent(double d){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * Bitwise convert d to long, mask out exponent bits, shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * to the right and then subtract out double's bias adjust to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * get true exponent value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return (int)(((Double.doubleToRawLongBits(d) & DoubleConsts.EXP_BIT_MASK) >>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                      (DoubleConsts.SIGNIFICAND_WIDTH - 1)) - DoubleConsts.EXP_BIAS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   152
     * Returns unbiased exponent of a {@code float}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public static int getExponent(float f){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         * Bitwise convert f to integer, mask out exponent bits, shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * to the right and then subtract out float's bias adjust to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * get true exponent value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns a floating-point power of two in the normal range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static double powerOfTwoD(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                        (DoubleConsts.SIGNIFICAND_WIDTH-1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                       & DoubleConsts.EXP_BIT_MASK);
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 a floating-point power of two in the normal range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static float powerOfTwoF(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                     (FloatConsts.SIGNIFICAND_WIDTH-1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                    & FloatConsts.EXP_BIT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * second floating-point argument.  Note that unlike the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * 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
   188
     * does not require NaN {@code sign} arguments to be treated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * as positive values; implementations are permitted to treat some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * NaN arguments as positive and other NaN arguments as negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * to allow greater performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @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
   195
     * @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
   196
     * and the sign of {@code sign}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public static double rawCopySign(double magnitude, double sign) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return Double.longBitsToDouble((Double.doubleToRawLongBits(sign) &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                        (DoubleConsts.SIGN_BIT_MASK)) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                       (Double.doubleToRawLongBits(magnitude) &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                        (DoubleConsts.EXP_BIT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                         DoubleConsts.SIGNIF_BIT_MASK)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * second floating-point argument.  Note that unlike the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * 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
   211
     * does not require NaN {@code sign} arguments to be treated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * as positive values; implementations are permitted to treat some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * NaN arguments as positive and other NaN arguments as negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * to allow greater performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @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
   218
     * @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
   219
     * and the sign of {@code sign}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public static float rawCopySign(float magnitude, float sign) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return Float.intBitsToFloat((Float.floatToRawIntBits(sign) &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                     (FloatConsts.SIGN_BIT_MASK)) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                    (Float.floatToRawIntBits(magnitude) &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                     (FloatConsts.EXP_BIT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                      FloatConsts.SIGNIF_BIT_MASK)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /* ***************************************************************** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   233
     * 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
   234
     * floating-point value; returns {@code false} otherwise (for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * NaN and infinity arguments).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   237
     * @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
   238
     * @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
   239
     * floating-point value, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public static boolean isFinite(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return Math.abs(d) <= DoubleConsts.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   246
     * 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
   247
     * floating-point value; returns {@code false} otherwise (for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * NaN and infinity arguments).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   250
     * @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
   251
     * @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
   252
     * floating-point value, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     public static boolean isFinite(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return Math.abs(f) <= FloatConsts.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   259
     * 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
   260
     * large in magnitude, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <p>Note that this method is equivalent to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Double#isInfinite(double) Double.isInfinite} method; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * functionality is included in this class for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @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
   267
     * @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
   268
     *          infinity or negative infinity; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public static boolean isInfinite(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return Double.isInfinite(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   275
     * 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
   276
     * large in magnitude, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p>Note that this method is equivalent to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Float#isInfinite(float) Float.isInfinite} method; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * functionality is included in this class for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @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
   283
     * @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
   284
     *          negative infinity; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     public static boolean isInfinite(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         return Float.isInfinite(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   291
     * 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
   292
     * Not-a-Number (NaN) value, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p>Note that this method is equivalent to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Double#isNaN(double) Double.isNaN} method; the functionality is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * included in this class for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @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
   299
     * @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
   300
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public static boolean isNaN(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return Double.isNaN(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   307
     * 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
   308
     * Not-a-Number (NaN) value, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <p>Note that this method is equivalent to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Float#isNaN(float) Float.isNaN} method; the functionality is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * included in this class for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @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
   315
     * @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
   316
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     public static boolean isNaN(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return Float.isNaN(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   323
     * Returns {@code true} if the unordered relation holds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * between the two arguments.  When two floating-point values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * unordered, one value is neither less than, equal to, nor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * 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
   327
     * at least one argument must be a {@code NaN}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param arg1      the first argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @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
   331
     * @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
   332
     * {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public static boolean isUnordered(double arg1, double arg2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return isNaN(arg1) || isNaN(arg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   339
     * Returns {@code true} if the unordered relation holds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * between the two arguments.  When two floating-point values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * unordered, one value is neither less than, equal to, nor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * 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
   343
     * at least one argument must be a {@code NaN}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param arg1      the first argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @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
   347
     * @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
   348
     * {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     public static boolean isUnordered(float arg1, float arg2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return isNaN(arg1) || isNaN(arg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   355
     * Returns unbiased exponent of a {@code double}; for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * subnormal values, the number is treated as if it were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * normalized.  That is for all finite, non-zero, positive numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * always in the range [1, 2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param d floating-point number whose exponent is to be extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @return unbiased exponent of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public static int ilogb(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        int exponent = getExponent(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        switch (exponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        case DoubleConsts.MAX_EXPONENT+1:       // NaN or infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if( isNaN(d) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                return (1<<30);         // 2^30
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            else // infinite value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                return (1<<28);         // 2^28
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if(d == 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                return -(1<<28);        // -(2^28)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                long transducer = Double.doubleToRawLongBits(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                 * To avoid causing slow arithmetic on subnormals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                 * the scaling to determine when d's significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                 * is normalized is done in integer arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                 * (there must be at least one "1" bit in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                 * significand since zero has been screened out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                // isolate significand bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                transducer &= DoubleConsts.SIGNIF_BIT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                assert(transducer != 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                // This loop is simple and functional. We might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                // able to do something more clever that was faster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                // e.g. number of leading zero detection on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                // (transducer << (# exponent and sign bits).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                while (transducer <
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                       (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    transducer *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    exponent--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                exponent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                assert( exponent >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        exponent < DoubleConsts.MIN_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            assert( exponent >= DoubleConsts.MIN_EXPONENT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    exponent <= DoubleConsts.MAX_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   425
     * Returns unbiased exponent of a {@code float}; for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * subnormal values, the number is treated as if it were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * normalized.  That is for all finite, non-zero, positive numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * always in the range [1, 2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param f floating-point number whose exponent is to be extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @return unbiased exponent of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     public static int ilogb(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        int exponent = getExponent(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        switch (exponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if( isNaN(f) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                return (1<<30);         // 2^30
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            else // infinite value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                return (1<<28);         // 2^28
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            if(f == 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                return -(1<<28);        // -(2^28)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                int transducer = Float.floatToRawIntBits(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                 * To avoid causing slow arithmetic on subnormals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                 * the scaling to determine when f's significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                 * is normalized is done in integer arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                 * (there must be at least one "1" bit in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                 * significand since zero has been screened out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                // isolate significand bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                transducer &= FloatConsts.SIGNIF_BIT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                assert(transducer != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                // This loop is simple and functional. We might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                // able to do something more clever that was faster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                // e.g. number of leading zero detection on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                // (transducer << (# exponent and sign bits).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                while (transducer <
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                       (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    transducer *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    exponent--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                exponent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                assert( exponent >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                        FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                        exponent < FloatConsts.MIN_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            assert( exponent >= FloatConsts.MIN_EXPONENT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    exponent <= FloatConsts.MAX_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * The scalb operation should be reasonably fast; however, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * are tradeoffs in writing a method to minimize the worst case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * performance and writing a method to minimize the time for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * expected common inputs.  Some processors operate very slowly on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * subnormal operands, taking hundreds or thousands of cycles for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * one floating-point add or multiply as opposed to, say, four
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * cycles for normal operands.  For processors with very slow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * subnormal execution, scalb would be fastest if written entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * with integer operations; in other words, scalb would need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * include the logic of performing correct rounding of subnormal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * values.  This could be reasonably done in at most a few hundred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * cycles.  However, this approach may penalize normal operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * since at least the exponent of the floating-point argument must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * be examined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * The approach taken in this implementation is a compromise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * Floating-point multiplication is used to do most of the work;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * but knowingly multiplying by a subnormal scaling factor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * avoided.  However, the floating-point argument is not examined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * to see whether or not it is subnormal since subnormal inputs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * are assumed to be rare.  At most three multiplies are needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * scale from the largest to smallest exponent ranges (scaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * down, at most two multiplies are needed if subnormal scaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * factors are allowed).  However, in this implementation an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * expensive integer remainder operation is avoided at the cost of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * requiring five floating-point multiplies in the worst case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * which should still be a performance win.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * If scaling of entire arrays is a concern, it would probably be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * more efficient to provide a double[] scalb(double[], int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * version of scalb to avoid having to recompute the needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * scaling factors for each floating-point value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   531
     * Return {@code d} &times;
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   532
     * 2<sup>{@code scale_factor}</sup> rounded as if performed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * by a single correctly rounded floating-point multiply to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * member of the double value set.  See <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * href="http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208">&sect;4.2.3</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Language Specification</a> for a discussion of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * 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
   539
     * {@code double}'s minimum exponent and maximum exponent,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * 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
   541
     * result would be larger than {@code doubles}'s maximum
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * 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
   543
     * 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
   544
     * n)} is subnormal, {@code scalb(scalb(x, n), -n)} may
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * 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
   546
     * the same sign as {@code d}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * <li> If the first argument is NaN, NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <li> If the first argument is infinite, then an infinity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * same sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <li> If the first argument is zero, then a zero of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @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
   559
     * @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
   560
     * @return {@code d * }2<sup>{@code scale_factor}</sup>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    public static double scalb(double d, int scale_factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         * This method does not need to be declared strictfp to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         * compute the same correct result on all platforms.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         * scaling up, it does not matter what order the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         * multiply-store operations are done; the result will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
         * finite or overflow regardless of the operation ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
         * However, to get the correct result when scaling down, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
         * particular ordering must be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         * When scaling down, the multiply-store operations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
         * sequenced so that it is not possible for two consecutive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         * multiply-stores to return subnormal results.  If one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         * multiply-store result is subnormal, the next multiply will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * round it away to zero.  This is done by first multiplying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         * by 2 ^ (scale_factor % n) and then multiplying several
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * times by by 2^n as needed where n is the exponent of number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         * that is a covenient power of two.  In this way, at most one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         * real rounding error occurs.  If the double value set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * being used exclusively, the rounding will occur on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         * multiply.  If the double-extended-exponent value set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * being used, the products will (perhaps) be exact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         * stores to d are guaranteed to round to the double value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         * set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
         * It is _not_ a valid implementation to first multiply d by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         * 2^MIN_EXPONENT and then by 2 ^ (scale_factor %
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         * MIN_EXPONENT) since even in a strictfp program double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         * rounding on underflow could occur; e.g. if the scale_factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * argument was (MIN_EXPONENT - n) and the exponent of d was a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         * little less than -(MIN_EXPONENT - n), meaning the final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * result would be subnormal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         * Since exact reproducibility of this method can be achieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * without any undue performance burden, there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         * compelling reason to allow double rounding on underflow in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         * scalb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        // magnitude of a power of two so large that scaling a finite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        // nonzero value by it would be guaranteed to over or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        // underflow; due to rounding, scaling down takes takes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        // additional power of two which is reflected here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        final int MAX_SCALE = DoubleConsts.MAX_EXPONENT + -DoubleConsts.MIN_EXPONENT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                              DoubleConsts.SIGNIFICAND_WIDTH + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        int exp_adjust = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        int scale_increment = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        double exp_delta = Double.NaN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        // Make sure scaling factor is in a reasonable range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if(scale_factor < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            scale_factor = Math.max(scale_factor, -MAX_SCALE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            scale_increment = -512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            exp_delta = twoToTheDoubleScaleDown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            scale_factor = Math.min(scale_factor, MAX_SCALE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            scale_increment = 512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            exp_delta = twoToTheDoubleScaleUp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        // Calculate (scale_factor % +/-512), 512 = 2^9, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        // technique from "Hacker's Delight" section 10-2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        int t = (scale_factor >> 9-1) >>> 32 - 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        exp_adjust = ((scale_factor + t) & (512 -1)) - t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        d *= powerOfTwoD(exp_adjust);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        scale_factor -= exp_adjust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        while(scale_factor != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            d *= exp_delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            scale_factor -= scale_increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   641
     * Return {@code f} &times;
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   642
     * 2<sup>{@code scale_factor}</sup> rounded as if performed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * by a single correctly rounded floating-point multiply to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * member of the float value set.  See <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * href="http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208">&sect;4.2.3</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Language Specification</a> for a discussion of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * value set. 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
   649
     * {@code float}'s minimum exponent and maximum exponent, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * 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
   651
     * would be larger than {@code float}'s maximum exponent, an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * 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
   653
     * 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
   654
     * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * <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
   656
     * sign as {@code f}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * <li> If the first argument is NaN, NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * <li> If the first argument is infinite, then an infinity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * same sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <li> If the first argument is zero, then a zero of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @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
   669
     * @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
   670
     * @return {@code f * }2<sup>{@code scale_factor}</sup>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     public static float scalb(float f, int scale_factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        // magnitude of a power of two so large that scaling a finite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        // nonzero value by it would be guaranteed to over or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        // underflow; due to rounding, scaling down takes takes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        // additional power of two which is reflected here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                              FloatConsts.SIGNIFICAND_WIDTH + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        // Make sure scaling factor is in a reasonable range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        scale_factor = Math.max(Math.min(scale_factor, MAX_SCALE), -MAX_SCALE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
         * Since + MAX_SCALE for float fits well within the double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
         * exponent range and + float -> double conversion is exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
         * the multiplication below will be exact. Therefore, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
         * rounding that occurs when the double product is cast to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
         * float will be the correctly rounded float result.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         * all operations other than the final multiply will be exact,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         * it is not necessary to declare this method strictfp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        return (float)((double)f*powerOfTwoD(scale_factor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Returns the floating-point number adjacent to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * argument in the direction of the second argument.  If both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * arguments compare as equal the second argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * <li> If either argument is a NaN, then NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   706
     * <li> If both arguments are signed zeros, {@code direction}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * is returned unchanged (as implied by the requirement of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * returning the second argument if the arguments compare as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * equal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   711
     * <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
   712
     * &plusmn;{@code Double.MIN_VALUE} and {@code direction}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * 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
   714
     * magnitude, then a zero with the same sign as {@code start}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   717
     * <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
   718
     * {@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
   719
     * 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
   720
     * same sign as {@code start} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   722
     * <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
   723
     * {@code Double.MAX_VALUE} and {@code direction} has a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * 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
   725
     * infinity with same sign as {@code start} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @param start     starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @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
   730
     * {@code start}'s neighbors or {@code start} should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * be returned
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   732
     * @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
   733
     * direction of {@code direction}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    public static double nextAfter(double start, double direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
         * The cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
         * nextAfter(+infinity, 0)  == MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
         * nextAfter(+infinity, +infinity)  == +infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         * nextAfter(-infinity, 0)  == -MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         * nextAfter(-infinity, -infinity)  == -infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
         * are naturally handled without any additional testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        // First check for NaN values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if (isNaN(start) || isNaN(direction)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            // return a NaN derived from the input NaN(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            return start + direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        } else if (start == direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            return direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        } else {        // start > direction or start < direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            // then bitwise convert start to integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            long transducer = Double.doubleToRawLongBits(start + 0.0d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
             * IEEE 754 floating-point numbers are lexicographically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
             * ordered if treated as signed- magnitude integers .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
             * Since Java's integers are two's complement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
             * incrementing" the two's complement representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
             * logically negative floating-point value *decrements*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
             * the signed-magnitude representation. Therefore, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
             * the integer representation of a floating-point values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
             * is less than zero, the adjustment to the representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
             * is in the opposite direction than would be expected at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
             * first .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            if (direction > start) { // Calculate next greater value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                transducer = transducer + (transducer >= 0L ? 1L:-1L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            } else  { // Calculate next lesser value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                assert direction < start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                if (transducer > 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    --transducer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    if (transducer < 0L )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                        ++transducer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                     * transducer==0, the result is -MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                     * The transition from zero (implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                     * positive) to the smallest negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                     * signed magnitude value must be done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                     * explicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                        transducer = DoubleConsts.SIGN_BIT_MASK | 1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return Double.longBitsToDouble(transducer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * Returns the floating-point number adjacent to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * argument in the direction of the second argument.  If both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * arguments compare as equal, the second argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * <li> If either argument is a NaN, then NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   806
     * <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
   807
     * zero with the same sign as {@code direction} is returned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * (as implied by the requirement of returning the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * if the arguments compare as equal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   811
     * <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
   812
     * &plusmn;{@code Float.MIN_VALUE} and {@code direction}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * 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
   814
     * magnitude, then a zero with the same sign as {@code start}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   817
     * <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
   818
     * {@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
   819
     * 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
   820
     * same sign as {@code start} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   822
     * <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
   823
     * {@code Float.MAX_VALUE} and {@code direction} has a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * 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
   825
     * infinity with same sign as {@code start} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @param start     starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @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
   830
     * {@code start}'s neighbors or {@code start} should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * be returned
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   832
     * @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
   833
     * direction of {@code direction}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     public static float nextAfter(float start, double direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * The cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         * nextAfter(+infinity, 0)  == MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
         * nextAfter(+infinity, +infinity)  == +infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
         * nextAfter(-infinity, 0)  == -MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
         * nextAfter(-infinity, -infinity)  == -infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         * are naturally handled without any additional testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        // First check for NaN values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        if (isNaN(start) || isNaN(direction)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            // return a NaN derived from the input NaN(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            return start + (float)direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        } else if (start == direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            return (float)direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        } else {        // start > direction or start < direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            // then bitwise convert start to integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            int transducer = Float.floatToRawIntBits(start + 0.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
             * IEEE 754 floating-point numbers are lexicographically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
             * ordered if treated as signed- magnitude integers .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
             * Since Java's integers are two's complement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
             * incrementing" the two's complement representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
             * logically negative floating-point value *decrements*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
             * the signed-magnitude representation. Therefore, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
             * the integer representation of a floating-point values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
             * is less than zero, the adjustment to the representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
             * is in the opposite direction than would be expected at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
             * first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            if (direction > start) {// Calculate next greater value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                transducer = transducer + (transducer >= 0 ? 1:-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            } else  { // Calculate next lesser value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                assert direction < start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                if (transducer > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                    --transducer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                    if (transducer < 0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                        ++transducer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                     * transducer==0, the result is -MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                     * The transition from zero (implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                     * positive) to the smallest negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                     * signed magnitude value must be done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                     * explicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                        transducer = FloatConsts.SIGN_BIT_MASK | 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            return Float.intBitsToFloat(transducer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   897
     * Returns the floating-point value adjacent to {@code d} in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * 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
   899
     * 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
   900
     * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * 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
   902
     * {@code nextAfter} call.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * <li> If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * <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
   912
     * {@code Double.MIN_VALUE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @param d  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @return The adjacent floating-point value closer to positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    public static double nextUp(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        if( isNaN(d) || d == Double.POSITIVE_INFINITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            d += 0.0d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                                           ((d >= 0.0d)?+1L:-1L));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   932
     * Returns the floating-point value adjacent to {@code f} in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * 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
   934
     * 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
   935
     * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * 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
   937
     * {@code nextAfter} call.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * <li> If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * <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
   947
     * {@code Float.MIN_VALUE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @param f  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @return The adjacent floating-point value closer to positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     public static float nextUp(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        if( isNaN(f) || f == FloatConsts.POSITIVE_INFINITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            f += 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                                        ((f >= 0.0f)?+1:-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
   967
     * Returns the floating-point value adjacent to {@code d} in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * 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
   969
     * 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
   970
     * 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
   971
     * {@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
   972
     * equivalent {@code nextAfter} call.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * <li> If the argument is negative infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * <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
   982
     * {@code -Double.MIN_VALUE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * @param d  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * @return The adjacent floating-point value closer to negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    public static double nextDown(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        if( isNaN(d) || d == Double.NEGATIVE_INFINITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            if (d == 0.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                return -Double.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                                               ((d > 0.0d)?-1L:+1L));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
  1004
     * Returns the floating-point value adjacent to {@code f} in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * 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
  1006
     * 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
  1007
     * 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
  1008
     * {@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
  1009
     * equivalent {@code nextAfter} call.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * <li> If the argument is negative infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * <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
  1019
     * {@code -Float.MIN_VALUE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * @param f  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @return The adjacent floating-point value closer to negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    public static double nextDown(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if( isNaN(f) || f == Float.NEGATIVE_INFINITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            if (f == 0.0f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                return -Float.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                                            ((f > 0.0f)?-1:+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * 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
  1043
     * {@code sign} argument is always treated as if it were
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @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
  1048
     * @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
  1049
     * and the sign of {@code sign}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    public static double copySign(double magnitude, double sign) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        return rawCopySign(magnitude, (isNaN(sign)?1.0d:sign));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * 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
  1060
     * {@code sign} argument is always treated as if it were
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @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
  1065
     * @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
  1066
     * and the sign of {@code sign}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     public static float copySign(float magnitude, float sign) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        return rawCopySign(magnitude, (isNaN(sign)?1.0f:sign));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * 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
  1075
     * {@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
  1076
     * floating-point value and the {@code double} value next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * larger in magnitude.  Note that for non-NaN <i>x</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * <li> If the argument is positive or negative infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * <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
  1086
     * {@code Double.MIN_VALUE}.
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
  1087
     * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * the result is equal to 2<sup>971</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * @param d the floating-point value whose ulp is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * @return the size of an ulp of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    public static double ulp(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        int exp = getExponent(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        switch(exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        case DoubleConsts.MAX_EXPONENT+1:       // NaN or infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            return Math.abs(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            return Double.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            if (exp >= DoubleConsts.MIN_EXPONENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                return powerOfTwoD(exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                // return a subnormal result; left shift integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                // representation of Double.MIN_VALUE appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                // number of positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                return Double.longBitsToDouble(1L <<
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                (exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * 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
  1126
     * {@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
  1127
     * floating-point value and the {@code float} value next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * larger in magnitude.  Note that for non-NaN <i>x</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * <li> If the argument is positive or negative infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * <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
  1137
     * {@code Float.MIN_VALUE}.
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
  1138
     * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * the result is equal to 2<sup>104</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * @param f the floating-point value whose ulp is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @return the size of an ulp of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     public static float ulp(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        int exp = getExponent(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        switch(exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            return Math.abs(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            return FloatConsts.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            if (exp >= FloatConsts.MIN_EXPONENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                return powerOfTwoF(exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                // return a subnormal result; left shift integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                // representation of FloatConsts.MIN_VALUE appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                // number of positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                return Float.intBitsToFloat(1 <<
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                (exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * Returns the signum function of the argument; zero if the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * is zero, 1.0 if the argument is greater than zero, -1.0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * argument is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * <li> If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     *      result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @param d the floating-point value whose signum is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * @return the signum function of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    public static double signum(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        return (d == 0.0 || isNaN(d))?d:copySign(1.0, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Returns the signum function of the argument; zero if the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * is zero, 1.0f if the argument is greater than zero, -1.0f if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * argument is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * <li> If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     *      result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @param f the floating-point value whose signum is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * @return the signum function of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    public static float signum(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        return (f == 0.0f || isNaN(f))?f:copySign(1.0f, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
}