Java Code Examples for sun.misc.FloatConsts#MAX_EXPONENT

The following examples show how to use sun.misc.FloatConsts#MAX_EXPONENT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FpUtils.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Return <code>f </code>&times;
 * 2<sup><code>scale_factor</code></sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See <a
 * href="http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208">&sect;4.2.3</a>
 * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
 * Language Specification</a> for a discussion of floating-point
 * value set. If the exponent of the result is between the
 * <code>float</code>'s minimum exponent and maximum exponent, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than <code>float</code>'s maximum exponent, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when <code>scalb(x, n)</code>
 * is subnormal, <code>scalb(scalb(x, n), -n)</code> may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as <code>f</code>.
 *
 *<p>
 * Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scale_factor power of 2 used to scale <code>f</code>
 * @return <code>f * </code>2<sup><code>scale_factor</code></sup>
 * @author Joseph D. Darcy
 */
 public static float scalb(float f, int scale_factor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scale_factor = Math.max(Math.min(scale_factor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scale_factor));
}
 
Example 2
Source File: FpUtils.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 3
Source File: FpUtils.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 4
Source File: Math.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 5
Source File: FpUtils.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Return <code>f </code>&times;
 * 2<sup><code>scale_factor</code></sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See <a
 * href="http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208">&sect;4.2.3</a>
 * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
 * Language Specification</a> for a discussion of floating-point
 * value set. If the exponent of the result is between the
 * <code>float</code>'s minimum exponent and maximum exponent, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than <code>float</code>'s maximum exponent, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when <code>scalb(x, n)</code>
 * is subnormal, <code>scalb(scalb(x, n), -n)</code> may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as <code>f</code>.
 *
 *<p>
 * Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scale_factor power of 2 used to scale <code>f</code>
 * @return <code>f * </code>2<sup><code>scale_factor</code></sup>
 * @author Joseph D. Darcy
 */
 public static float scalb(float f, int scale_factor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scale_factor = Math.max(Math.min(scale_factor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scale_factor));
}
 
Example 6
Source File: FpUtils.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a <code>float</code>; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28
    // break;

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }
    // break;

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    // break;
    }
}
 
Example 7
Source File: Math.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 8
Source File: Math.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 9
Source File: FpUtils.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 10
Source File: Math.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 11
Source File: Math.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 12
Source File: FpUtils.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a <code>float</code>; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28
    // break;

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }
    // break;

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    // break;
    }
}
 
Example 13
Source File: FpUtils.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 14
Source File: Math.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 15
Source File: Math.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 16
Source File: Math.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 17
Source File: FpUtils.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 18
Source File: FpUtils.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 19
Source File: Math.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 20
Source File: Math.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}