Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#INFINITE_VALUE_CONVERSION

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#INFINITE_VALUE_CONVERSION . 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: 1_BigFraction.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 2
Source File: 1_BigFraction.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 3
Source File: 1_BigFraction.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 4
Source File: BigFraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 5
Source File: BigFraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 6
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m >>= 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 7
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 8
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 9
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 10
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 11
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 12
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m = m >> 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}
 
Example 13
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the double value.
 * <p>
 * This constructor behaves <em>differently</em> from
 * {@link #BigFraction(double, double, int)}. It converts the double value
 * exactly, considering its internal bits representation. This works for all
 * values except NaN and infinities and does not requires any loop or
 * convergence threshold.
 * </p>
 * <p>
 * Since this conversion is exact and since double numbers are sometimes
 * approximated, the fraction created may seem strange in some cases. For example,
 * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
 * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
 * because the double number passed to the constructor is not exactly 1/3
 * (this number cannot be stored exactly in IEEE754).
 * </p>
 * @see #BigFraction(double, double, int)
 * @param value the double value to convert to a fraction.
 * @exception MathIllegalArgumentException if value is NaN or infinite
 */
public BigFraction(final double value) throws MathIllegalArgumentException {
    if (Double.isNaN(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION);
    }
    if (Double.isInfinite(value)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION);
    }

    // compute m and k such that value = m * 2^k
    final long bits     = Double.doubleToLongBits(value);
    final long sign     = bits & 0x8000000000000000L;
    final long exponent = bits & 0x7ff0000000000000L;
    long m              = bits & 0x000fffffffffffffL;
    if (exponent != 0) {
        // this was a normalized number, add the implicit most significant bit
        m |= 0x0010000000000000L;
    }
    if (sign != 0) {
        m = -m;
    }
    int k = ((int) (exponent >> 52)) - 1075;
    while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
        m >>= 1;
        ++k;
    }

    if (k < 0) {
        numerator   = BigInteger.valueOf(m);
        denominator = BigInteger.ZERO.flipBit(-k);
    } else {
        numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
        denominator = BigInteger.ONE;
    }

}