static boolean isFinite(double d) { return getExponent(d) <= MAX_DOUBLE_EXPONENT; }
static boolean isFinite(double d) { return getExponent(d) <= MAX_EXPONENT; }
static boolean isNormal(double d) { return getExponent(d) >= MIN_EXPONENT; }
static boolean isNormal(double d) { return getExponent(d) >= MIN_DOUBLE_EXPONENT; }
static long getSignificand(double d) { checkArgument(isFinite(d), "not a normal value"); int exponent = getExponent(d); long bits = Double.doubleToRawLongBits(d); bits &= SIGNIFICAND_MASK; return (exponent == MIN_DOUBLE_EXPONENT - 1) ? bits << 1 : bits | IMPLICIT_BIT; }
static long getSignificand(double d) { checkArgument(isFinite(d), "not a normal value"); int exponent = getExponent(d); long bits = doubleToRawLongBits(d); bits &= SIGNIFICAND_MASK; return (exponent == MIN_EXPONENT - 1) ? bits << 1 : bits | IMPLICIT_BIT; }
/** * Returns {@code true} if {@code x} represents a mathematical integer. * * <p>This is equivalent to, but not necessarily implemented as, the expression {@code * !Double.isNaN(x) && !Double.isInfinite(x) && x == Math.rint(x)}. */ public static boolean isMathematicalInteger(double x) { return isFinite(x) && (x == 0.0 || SIGNIFICAND_BITS - Long.numberOfTrailingZeros(getSignificand(x)) <= getExponent(x)); }
/** * Returns the {@code BigInteger} value that is equal to {@code x} rounded with the specified * rounding mode, if possible. * * @throws ArithmeticException if * <ul> * <li>{@code x} is infinite or NaN * <li>{@code x} is not a mathematical integer and {@code mode} is * {@link RoundingMode#UNNECESSARY} * </ul> */ public static BigInteger roundToBigInteger(double x, RoundingMode mode) { x = roundIntermediate(x, mode); if (MIN_LONG_AS_DOUBLE - x < 1.0 & x < MAX_LONG_AS_DOUBLE_PLUS_ONE) { return BigInteger.valueOf((long) x); } int exponent = getExponent(x); if (exponent < 0) { return BigInteger.ZERO; } long significand = getSignificand(x); BigInteger result = BigInteger.valueOf(significand).shiftLeft(exponent - SIGNIFICAND_BITS); return (x < 0) ? result.negate() : result; }
/** * Returns {@code d * 2^scale}. */ static strictfp double scalb(double d, int scale) { // TODO: replace with Math.scalb in JDK 6 int exponent = getExponent(d); switch (exponent) { case MAX_DOUBLE_EXPONENT + 1: // NaN, infinity return d; case MIN_DOUBLE_EXPONENT - 1: return d * StrictMath.pow(2.0, scale); default: int newExponent = exponent + scale; if (MIN_DOUBLE_EXPONENT <= newExponent & newExponent <= MAX_DOUBLE_EXPONENT) { long bits = Double.doubleToRawLongBits(d); bits &= ~EXPONENT_MASK; bits |= ((long) (newExponent + EXPONENT_BIAS)) << SIGNIFICAND_BITS; return Double.longBitsToDouble(bits); } return d * StrictMath.pow(2.0, scale); } }
/** * Returns {@code true} if {@code x} represents a mathematical integer. * * <p>This is equivalent to, but not necessarily implemented as, the expression {@code * !Double.isNaN(x) && !Double.isInfinite(x) && x == Math.rint(x)}. */ @GwtIncompatible("java.lang.Math.getExponent, com.google.common.math.DoubleUtils") public static boolean isMathematicalInteger(double x) { return isFinite(x) && (x == 0.0 || SIGNIFICAND_BITS - Long.numberOfTrailingZeros(getSignificand(x)) <= getExponent(x)); }
/** * Returns the {@code BigInteger} value that is equal to {@code x} rounded with the specified * rounding mode, if possible. * * @throws ArithmeticException if * <ul> * <li>{@code x} is infinite or NaN * <li>{@code x} is not a mathematical integer and {@code mode} is * {@link RoundingMode#UNNECESSARY} * </ul> */ @GwtIncompatible("#roundIntermediate, java.lang.Math.getExponent, " + "com.google.common.math.DoubleUtils") public static BigInteger roundToBigInteger(double x, RoundingMode mode) { x = roundIntermediate(x, mode); if (MIN_LONG_AS_DOUBLE - x < 1.0 & x < MAX_LONG_AS_DOUBLE_PLUS_ONE) { return BigInteger.valueOf((long) x); } int exponent = getExponent(x); long significand = getSignificand(x); BigInteger result = BigInteger.valueOf(significand).shiftLeft(exponent - SIGNIFICAND_BITS); return (x < 0) ? result.negate() : result; }
public static int log2(double x, RoundingMode mode) { checkArgument(x > 0.0 && isFinite(x), "x must be positive and finite"); int exponent = getExponent(x); if (!isNormal(x)) { return log2(x * IMPLICIT_BIT, mode) - SIGNIFICAND_BITS;
public static int log2(double x, RoundingMode mode) { checkArgument(x > 0.0 && isFinite(x), "x must be positive and finite"); int exponent = getExponent(x); if (!isNormal(x)) { return log2(x * IMPLICIT_BIT, mode) - SIGNIFICAND_BITS;