Refine search
public ExpandedDouble(long rawBits) { int biasedExp = (int) (rawBits >> 52); if (biasedExp == 0) { // sub-normal numbers BigInteger frac = BigInteger.valueOf(rawBits).and(BI_FRAC_MASK); int expAdj = 64 - frac.bitLength(); _significand = frac.shiftLeft(expAdj); _binaryExponent = (biasedExp & 0x07FF) - 1023 - expAdj; } else { _significand = getFrac(rawBits); _binaryExponent = (biasedExp & 0x07FF) - 1023; } }
Bernoulli b = new Bernoulli(); BigInteger fourn = BigInteger.valueOf(4); BigInteger fac = BigInteger.valueOf(2); for (int i = 2;; i++) { Rational f = b.at(2 * i).abs(); fourn = fourn.shiftLeft(2); fac = fac.multiply(BigInteger.valueOf(2 * i)).multiply(BigInteger.valueOf(2 * i - 1)); f = f.multiply(fourn).multiply(fourn.subtract(BigInteger.ONE)).divide(fac); xpowi = multiplyRound(xpowi, xhighprSq); BigDecimal c = multiplyRound(xpowi, f);
this(BigInteger.valueOf(numerator), BigInteger.valueOf(denominator)); this(BigInteger.valueOf(numerator), BigInteger.ONE, true); tmpNumerator = tmpNumerator.multiply(BigInteger.ONE.shiftLeft(exponent)); else tmpDenominator = tmpDenominator.multiply(BigInteger.ONE.shiftLeft(-exponent)); return new BigFraction(numerator.multiply(f.denominator).add(denominator.multiply(f.numerator)), denominator.multiply(f.denominator)); return new BigFraction(numerator.add(denominator.multiply(b)), denominator, true); throw new IllegalArgumentException("Null argument"); return new BigFraction(numerator.multiply(f.denominator).subtract(denominator.multiply(f.numerator)), denominator.multiply(f.denominator)); throw new IllegalArgumentException("Null argument"); return new BigFraction(denominator.subtract(numerator), denominator, true); unscaled = unscaled.shiftLeft(fives - twos); //x * 2^n === x << n else if (fives < twos) unscaled = unscaled.multiply(FIVE.pow(twos - fives));
@GwtIncompatible // TODO public void testSqrtHalfDown() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { BigInteger result = BigIntegerMath.sqrt(x, HALF_DOWN); BigInteger plusHalfSquared = result.pow(2).add(result).shiftLeft(2).add(ONE); BigInteger x4 = x.shiftLeft(2); // sqrt(x) <= result + 0.5, so 4 * x <= (result + 0.5)^2 * 4 // (result + 0.5)^2 * 4 = (result^2 + result)*4 + 1 assertTrue(x4.compareTo(plusHalfSquared) <= 0); BigInteger minusHalfSquared = result.pow(2).subtract(result).shiftLeft(2).add(ONE); // sqrt(x) > result - 0.5, so 4 * x > (result - 0.5)^2 * 4 // (result - 0.5)^2 * 4 = (result^2 - result)*4 + 1 assertTrue(result.equals(ZERO) || x4.compareTo(minusHalfSquared) > 0); } }
/** * Convert this object to {@link BigInteger}. Do not use this method in a * performance sensitive place. * * @return BigInteger to represent this object */ public BigInteger toBigIntegerSlow() { BigInteger bigInt = BigInteger.valueOf(v[3] & SqlMathUtil.LONG_MASK); bigInt = bigInt.shiftLeft(32); bigInt = bigInt.add(BigInteger.valueOf(v[2] & SqlMathUtil.LONG_MASK)); bigInt = bigInt.shiftLeft(32); bigInt = bigInt.add(BigInteger.valueOf(v[1] & SqlMathUtil.LONG_MASK)); bigInt = bigInt.shiftLeft(32); bigInt = bigInt.add(BigInteger.valueOf(v[0] & SqlMathUtil.LONG_MASK)); return bigInt; }
public ExpandedDouble(long rawBits) { int biasedExp = (int) (rawBits >> 52); if (biasedExp == 0) { // sub-normal numbers BigInteger frac = BigInteger.valueOf(rawBits).and(BI_FRAC_MASK); int expAdj = 64 - frac.bitLength(); _significand = frac.shiftLeft(expAdj); _binaryExponent = (biasedExp & 0x07FF) - 1023 - expAdj; } else { BigInteger frac = getFrac(rawBits); _significand = frac; _binaryExponent = (biasedExp & 0x07FF) - 1023; } }
/** * Convert the given value in unsigned 64-bit representation into its actual * value. That is, all return values of this function will be positive. * * @param value * The unsigned 64-bit value to convert. * @return The value of the given 64-bit unsigned value. */ public static BigInteger fromUInt64(long value) { return BigInteger.valueOf(value).and( BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE)); }
@Test public void testRescale() { assertEquals(rescale(unscaledDecimal(10), 0), unscaledDecimal(10L)); assertEquals(rescale(unscaledDecimal(10), -20), unscaledDecimal(0L)); assertEquals(rescale(unscaledDecimal(15), -1), unscaledDecimal(2)); assertEquals(rescale(unscaledDecimal(1050), -3), unscaledDecimal(1)); assertEquals(rescale(unscaledDecimal(15), 1), unscaledDecimal(150)); assertEquals(rescale(unscaledDecimal(-14), -1), unscaledDecimal(-1)); assertEquals(rescale(unscaledDecimal(-14), 1), unscaledDecimal(-140)); assertEquals(rescale(unscaledDecimal(0), 1), unscaledDecimal(0)); assertEquals(rescale(unscaledDecimal(5), -1), unscaledDecimal(1)); assertEquals(rescale(unscaledDecimal(10), 10), unscaledDecimal(100000000000L)); assertEquals(rescale(unscaledDecimal("150000000000000000000"), -20), unscaledDecimal(2)); assertEquals(rescale(unscaledDecimal("-140000000000000000000"), -20), unscaledDecimal(-1)); assertEquals(rescale(unscaledDecimal("50000000000000000000"), -20), unscaledDecimal(1)); assertEquals(rescale(unscaledDecimal("150500000000000000000"), -18), unscaledDecimal(151)); assertEquals(rescale(unscaledDecimal("-140000000000000000000"), -18), unscaledDecimal(-140)); assertEquals(rescale(unscaledDecimal(BigInteger.ONE.shiftLeft(63)), -18), unscaledDecimal(9L)); assertEquals(rescale(unscaledDecimal(BigInteger.ONE.shiftLeft(62)), -18), unscaledDecimal(5L)); assertEquals(rescale(unscaledDecimal(BigInteger.ONE.shiftLeft(62)), -19), unscaledDecimal(0L)); assertEquals(rescale(MAX_DECIMAL, -1), unscaledDecimal(MAX_DECIMAL_UNSCALED_VALUE.divide(BigInteger.TEN).add(BigInteger.ONE))); assertEquals(rescale(MIN_DECIMAL, -10), unscaledDecimal(MIN_DECIMAL_UNSCALED_VALUE.divide(BigInteger.valueOf(10000000000L)).subtract(BigInteger.ONE))); assertEquals(rescale(unscaledDecimal(1), 37), unscaledDecimal("10000000000000000000000000000000000000")); assertEquals(rescale(unscaledDecimal(-1), 37), unscaledDecimal("-10000000000000000000000000000000000000")); assertEquals(rescale(unscaledDecimal("10000000000000000000000000000000000000"), -37), unscaledDecimal(1)); }
private static void writeBigInteger(OutputStream output, BigInteger value) throws IOException { // encode the signed number as a positive integer value = value.shiftLeft(1); int sign = value.signum(); if (sign < 0) { value = value.negate(); value = value.subtract(ONE); } int length = value.bitLength(); while (true) { long lowBits = value.longValue() & 0x7fffffffffffffffL; length -= 63; // write out the next 63 bits worth of data for (int i = 0; i < 9; ++i) { // if this is the last byte, leave the high bit off if (length <= 0 && (lowBits & ~0x7f) == 0) { output.write((byte) lowBits); return; } else { output.write((byte) (0x80 | (lowBits & 0x7f))); lowBits >>>= 7; } } value = value.shiftRight(63); } } }
@GwtIncompatible // TODO public void testSqrtHalfUp() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { BigInteger result = BigIntegerMath.sqrt(x, HALF_UP); BigInteger plusHalfSquared = result.pow(2).add(result).shiftLeft(2).add(ONE); BigInteger x4 = x.shiftLeft(2); // sqrt(x) < result + 0.5, so 4 * x < (result + 0.5)^2 * 4 // (result + 0.5)^2 * 4 = (result^2 + result)*4 + 1 assertTrue(x4.compareTo(plusHalfSquared) < 0); BigInteger minusHalfSquared = result.pow(2).subtract(result).shiftLeft(2).add(ONE); // sqrt(x) > result - 0.5, so 4 * x > (result - 0.5)^2 * 4 // (result - 0.5)^2 * 4 = (result^2 - result)*4 + 1 assertTrue(result.equals(ZERO) || x4.compareTo(minusHalfSquared) >= 0); } }
/** * Convert the given value in unsigned 64-bit representation into its actual * value. That is, all return values of this function will be positive. * * @param value * The unsigned 64-bit value to convert. * @return The value of the given 64-bit unsigned value. */ public static BigInteger fromUInt64(long value) { return BigInteger.valueOf(value).and( BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE)); }