Refine search
@GwtIncompatible // TODO private static BigInteger sqrtApproxWithDoubles(BigInteger x) { return DoubleMath.roundToBigInteger(Math.sqrt(DoubleUtils.bigToDouble(x)), HALF_EVEN); }
/** * Returns {@code true} if {@code x} is exactly equal to {@code 2^k} for some finite integer * {@code k}. */ @GwtIncompatible // com.google.common.math.DoubleUtils public static boolean isPowerOfTwo(double x) { if (x > 0.0 && isFinite(x)) { long significand = getSignificand(x); return (significand & (significand - 1)) == 0; } return false; }
@GwtIncompatible // java.lang.Math.getExponent, com.google.common.math.DoubleUtils @SuppressWarnings("fallthrough") 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; switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(isPowerOfTwo(x)); break; case CEILING: increment = !isPowerOfTwo(x); break; case DOWN: case HALF_EVEN: case HALF_UP: double xScaled = scaleNormalize(x);
@GwtIncompatible // #isMathematicalInteger, com.google.common.math.DoubleUtils static double roundIntermediate(double x, RoundingMode mode) { if (!isFinite(x)) { throw new ArithmeticException("input is infinite or NaN"); checkRoundingUnnecessary(isMathematicalInteger(x)); return x; if (x >= 0.0 || isMathematicalInteger(x)) { return x; } else { if (x <= 0.0 || isMathematicalInteger(x)) { return x; } else {
/** * 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> */ // #roundIntermediate, java.lang.Math.getExponent, com.google.common.math.DoubleUtils @GwtIncompatible 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; }
/** * 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)); }
@GwtIncompatible // com.google.common.math.DoubleUtils @CanIgnoreReturnValue private static double checkFinite(double argument) { checkArgument(isFinite(argument)); return argument; }
static double roundIntermediate(double x, RoundingMode mode) { if (!isFinite(x)) { throw new ArithmeticException("input is infinite or NaN"); checkRoundingUnnecessary(isMathematicalInteger(x)); return x; if (x >= 0.0 || isMathematicalInteger(x)) { return x; } else { if (x <= 0.0 || isMathematicalInteger(x)) { return x; } else {
static double roundIntermediate(double x, RoundingMode mode) { if (!isFinite(x)) { throw new ArithmeticException("input is infinite or NaN"); checkRoundingUnnecessary(isMathematicalInteger(x)); return x; if (isMathematicalInteger(x)) { return x; } else { if (isMathematicalInteger(x)) { return x; } else if (x >= 0.0) { double z = x + 0.5; return (z == x) ? x : next(z, false); // x + 0.5 - epsilon } else { double z = x - 0.5; return (z == x) ? x : next(z, true); // x - 0.5 + epsilon
/** * Returns {@code true} if {@code x} is exactly equal to {@code 2^k} for some finite integer * {@code k}. */ public static boolean isPowerOfTwo(double x) { return x > 0.0 && isFinite(x) && LongMath.isPowerOfTwo(getSignificand(x)); }
/** * Start building an instance which maps {@code x = x1} to {@code y = y1}. Both arguments must be * finite. Call either {@link LinearTransformationBuilder#and} or {@link * LinearTransformationBuilder#withSlope} on the returned object to finish building the instance. */ public static LinearTransformationBuilder mapping(double x1, double y1) { checkArgument(isFinite(x1) && isFinite(y1)); return new LinearTransformationBuilder(x1, y1); }
private static BigInteger sqrtApproxWithDoubles(BigInteger x) { return DoubleMath.roundToBigInteger(Math.sqrt(DoubleUtils.bigToDouble(x)), HALF_EVEN); }
/** * Returns the <a href="http://en.wikipedia.org/wiki/Variance#Sample_variance">unbiased sample * variance</a> of the values. If this dataset is a sample drawn from a population, this is an * unbiased estimator of the population variance of the population. The count must be greater than * one. * * <p>This is not guaranteed to return zero when the dataset consists of the same value multiple * times, due to numerical errors. However, it is guaranteed never to return a negative result. * * <h3>Non-finite values</h3> * * <p>If the dataset contains any non-finite values ({@link Double#POSITIVE_INFINITY}, {@link * Double#NEGATIVE_INFINITY}, or {@link Double#NaN}) then the result is {@link Double#NaN}. * * @throws IllegalStateException if the dataset is empty or contains a single value */ public final double sampleVariance() { checkState(count > 1); if (isNaN(sumOfSquaresOfDeltas)) { return NaN; } return ensureNonNegative(sumOfSquaresOfDeltas) / (count - 1); }
@AndroidIncompatible // TODO(cpovirk): File bug for BigDecimal.doubleValue(). public void testBigToDouble() { for (BigInteger b : ALL_BIGINTEGER_CANDIDATES) { if (b.doubleValue() != DoubleUtils.bigToDouble(b)) { failFormat( "Converting %s to double: expected doubleValue %s but got bigToDouble %s", b, b.doubleValue(), DoubleUtils.bigToDouble(b)); } } }
@GwtIncompatible // #isMathematicalInteger, com.google.common.math.DoubleUtils static double roundIntermediate(double x, RoundingMode mode) { if (!isFinite(x)) { throw new ArithmeticException("input is infinite or NaN"); checkRoundingUnnecessary(isMathematicalInteger(x)); return x; if (x >= 0.0 || isMathematicalInteger(x)) { return x; } else { if (x <= 0.0 || isMathematicalInteger(x)) { return x; } else {
/** * 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> */ // #roundIntermediate, java.lang.Math.getExponent, com.google.common.math.DoubleUtils @GwtIncompatible 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; }
@GwtIncompatible // com.google.common.math.DoubleUtils @CanIgnoreReturnValue private static double checkFinite(double argument) { checkArgument(isFinite(argument)); return argument; }
static double roundIntermediate(double x, RoundingMode mode) { if (!isFinite(x)) { throw new ArithmeticException("input is infinite or NaN"); checkRoundingUnnecessary(isMathematicalInteger(x)); return x; if (x >= 0.0 || isMathematicalInteger(x)) { return x; } else { if (x <= 0.0 || isMathematicalInteger(x)) { return x; } else {
/** * Returns {@code true} if {@code x} is exactly equal to {@code 2^k} for some finite integer * {@code k}. */ public static boolean isPowerOfTwo(double x) { return x > 0.0 && isFinite(x) && LongMath.isPowerOfTwo(getSignificand(x)); }
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; }