Java Code Examples for org.apache.commons.math3.util.FastMath#getExponent()

The following examples show how to use org.apache.commons.math3.util.FastMath#getExponent() . 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: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a {@code double}. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a {@code double}
 * @see java.lang.Number#doubleValue()
 */
@Override
public double doubleValue() {
    double result = numerator.doubleValue() / denominator.doubleValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = FastMath.max(numerator.bitLength(),
                                 denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
        result = numerator.shiftRight(shift).doubleValue() /
            denominator.shiftRight(shift).doubleValue();
    }
    return result;
}
 
Example 2
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>double</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>double</tt>
 * @see java.lang.Number#doubleValue()
 */
@Override
public double doubleValue() {
    double result = numerator.doubleValue() / denominator.doubleValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
        result = numerator.shiftRight(shift).doubleValue() /
            denominator.shiftRight(shift).doubleValue();
    }
    return result;
}
 
Example 3
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>float</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>float</tt>.
 * @see java.lang.Number#floatValue()
 */
@Override
public float floatValue() {
    float result = numerator.floatValue() / denominator.floatValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE);
        result = numerator.shiftRight(shift).floatValue() /
            denominator.shiftRight(shift).floatValue();
    }
    return result;
}
 
Example 4
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>double</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>double</tt>
 * @see java.lang.Number#doubleValue()
 */
@Override
public double doubleValue() {
    double result = numerator.doubleValue() / denominator.doubleValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
        result = numerator.shiftRight(shift).doubleValue() /
            denominator.shiftRight(shift).doubleValue();
    }
    return result;
}
 
Example 5
Source File: SparseGradient.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public SparseGradient hypot(final SparseGradient y) {
    if (Double.isInfinite(value) || Double.isInfinite(y.value)) {
        return createConstant(Double.POSITIVE_INFINITY);
    } else if (Double.isNaN(value) || Double.isNaN(y.value)) {
        return createConstant(Double.NaN);
    } else {

        final int expX = FastMath.getExponent(value);
        final int expY = FastMath.getExponent(y.value);
        if (expX > expY + 27) {
            // y is negligible with respect to x
            return abs();
        } else if (expY > expX + 27) {
            // x is negligible with respect to y
            return y.abs();
        } else {

            // find an intermediate scale to avoid both overflow and underflow
            final int middleExp = (expX + expY) / 2;

            // scale parameters without losing precision
            final SparseGradient scaledX = scalb(-middleExp);
            final SparseGradient scaledY = y.scalb(-middleExp);

            // compute scaled hypotenuse
            final SparseGradient scaledH =
                    scaledX.multiply(scaledX).add(scaledY.multiply(scaledY)).sqrt();

            // remove scaling
            return scaledH.scalb(middleExp);

        }

    }
}
 
Example 6
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>double</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>double</tt>
 * @see java.lang.Number#doubleValue()
 */
@Override
public double doubleValue() {
    double result = numerator.doubleValue() / denominator.doubleValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
        result = numerator.shiftRight(shift).doubleValue() /
            denominator.shiftRight(shift).doubleValue();
    }
    return result;
}
 
Example 7
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>float</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>float</tt>.
 * @see java.lang.Number#floatValue()
 */
@Override
public float floatValue() {
    float result = numerator.floatValue() / denominator.floatValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE);
        result = numerator.shiftRight(shift).floatValue() /
            denominator.shiftRight(shift).floatValue();
    }
    return result;
}
 
Example 8
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>double</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>double</tt>
 * @see java.lang.Number#doubleValue()
 */
@Override
public double doubleValue() {
    double result = numerator.doubleValue() / denominator.doubleValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
        result = numerator.shiftRight(shift).doubleValue() /
            denominator.shiftRight(shift).doubleValue();
    }
    return result;
}
 
Example 9
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a {@code float}. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a {@code float}.
 * @see java.lang.Number#floatValue()
 */
@Override
public float floatValue() {
    float result = numerator.floatValue() / denominator.floatValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = FastMath.max(numerator.bitLength(),
                                 denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE);
        result = numerator.shiftRight(shift).floatValue() /
            denominator.shiftRight(shift).floatValue();
    }
    return result;
}
 
Example 10
Source File: 1_BigFraction.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>double</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>double</tt>
 * @see java.lang.Number#doubleValue()
 */
@Override
public double doubleValue() {
    double result = numerator.doubleValue() / denominator.doubleValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
        result = numerator.shiftRight(shift).doubleValue() /
            denominator.shiftRight(shift).doubleValue();
    }
    return result;
}
 
Example 11
Source File: BigFraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>float</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>float</tt>.
 * @see java.lang.Number#floatValue()
 */
@Override
public float floatValue() {
    float result = numerator.floatValue() / denominator.floatValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE);
        result = numerator.shiftRight(shift).floatValue() /
            denominator.shiftRight(shift).floatValue();
    }
    return result;
}
 
Example 12
Source File: BigFraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>double</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>double</tt>
 * @see java.lang.Number#doubleValue()
 */
@Override
public double doubleValue() {
    double result = numerator.doubleValue() / denominator.doubleValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
        result = numerator.shiftRight(shift).doubleValue() /
            denominator.shiftRight(shift).doubleValue();
    }
    return result;
}
 
Example 13
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>float</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>float</tt>.
 * @see java.lang.Number#floatValue()
 */
@Override
public float floatValue() {
    float result = numerator.floatValue() / denominator.floatValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE);
        result = numerator.shiftRight(shift).floatValue() /
            denominator.shiftRight(shift).floatValue();
    }
    return result;
}
 
Example 14
Source File: BigFraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>double</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>double</tt>
 * @see java.lang.Number#doubleValue()
 */
@Override
public double doubleValue() {
    double result = numerator.doubleValue() / denominator.doubleValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
        result = numerator.shiftRight(shift).doubleValue() /
            denominator.shiftRight(shift).doubleValue();
    }
    return result;
}
 
Example 15
Source File: 1_BigFraction.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>float</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>float</tt>.
 * @see java.lang.Number#floatValue()
 */
@Override
public float floatValue() {
    float result = numerator.floatValue() / denominator.floatValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE);
        result = numerator.shiftRight(shift).floatValue() /
            denominator.shiftRight(shift).floatValue();
    }
    return result;
}
 
Example 16
Source File: 1_BigFraction.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>double</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>double</tt>
 * @see java.lang.Number#doubleValue()
 */
@Override
public double doubleValue() {
    double result = numerator.doubleValue() / denominator.doubleValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
        result = numerator.shiftRight(shift).doubleValue() /
            denominator.shiftRight(shift).doubleValue();
    }
    return result;
}
 
Example 17
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>double</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>double</tt>
 * @see java.lang.Number#doubleValue()
 */
@Override
public double doubleValue() {
    double result = numerator.doubleValue() / denominator.doubleValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
        result = numerator.shiftRight(shift).doubleValue() /
            denominator.shiftRight(shift).doubleValue();
    }
    return result;
}
 
Example 18
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Gets the fraction as a <tt>float</tt>. This calculates the fraction as
 * the numerator divided by denominator.
 * </p>
 *
 * @return the fraction as a <tt>float</tt>.
 * @see java.lang.Number#floatValue()
 */
@Override
public float floatValue() {
    float result = numerator.floatValue() / denominator.floatValue();
    if (Double.isNaN(result)) {
        // Numerator and/or denominator must be out of range:
        // Calculate how far to shift them to put them in range.
        int shift = Math.max(numerator.bitLength(),
                             denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE);
        result = numerator.shiftRight(shift).floatValue() /
            denominator.shiftRight(shift).floatValue();
    }
    return result;
}
 
Example 19
Source File: DerivativeStructure.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return the exponent of the instance value, removing the bias.
 * <p>
 * For double numbers of the form 2<sup>x</sup>, the unbiased
 * exponent is exactly x.
 * </p>
 * @return exponent for instance in IEEE754 representation, without bias
 */
public int getExponent() {
    return FastMath.getExponent(data[0]);
}
 
Example 20
Source File: DerivativeStructure.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return the exponent of the instance value, removing the bias.
 * <p>
 * For double numbers of the form 2<sup>x</sup>, the unbiased
 * exponent is exactly x.
 * </p>
 * @return exponent for instance in IEEE754 representation, without bias
 */
public int getExponent() {
    return FastMath.getExponent(data[0]);
}