Refine search
private void mulShift(BigInteger multiplicand, int multiplierShift) { _significand = _significand.multiply(multiplicand); _binaryExponent += multiplierShift; // check for too much precision int sc = (_significand.bitLength() - MIN_PRECISION) & 0xFFFFFFE0; // mask makes multiples of 32 which optimises BigInteger.shiftRight if (sc > 0) { // no need to round because we have at least 8 bits of extra precision _significand = _significand.shiftRight(sc); _binaryExponent += sc; } }
sqrt0 = sqrtApproxWithDoubles(x.shiftRight(shift)).shiftLeft(shift >> 1); BigInteger sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1); if (sqrt0.equals(sqrt1)) { return sqrt0; sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1); } while (sqrt1.compareTo(sqrt0) < 0); return sqrt0;
public BigDecimal eval(List<? extends Number> parameters) { assertNotNull(parameters.get(0)); /* * From The Java Programmers Guide To numerical Computing (Ronald Mak, 2003) */ BigDecimal x = (BigDecimal) parameters.get(0); if (x.compareTo(BigDecimal.ZERO) == 0) { return new BigDecimal(0); } if (x.signum() < 0) { throw new ExpressionException("Argument to SQRT() function must not be negative"); } BigInteger n = x.movePointRight(mc.getPrecision() << 1).toBigInteger(); int bits = (n.bitLength() + 1) >> 1; BigInteger ix = n.shiftRight(bits); BigInteger ixPrev; BigInteger test; do { ixPrev = ix; ix = ix.add(n.divide(ix)).shiftRight(1); // Give other threads a chance to work; Thread.yield(); test = ix.subtract(ixPrev).abs(); } while (test.compareTo(BigInteger.ZERO) != 0 && test.compareTo(BigInteger.ONE) != 0); return new BigDecimal(ix, mc.getPrecision()); } });
private void calculate() throws UnknownHostException { final int targetSize; final BigInteger mask; if (inetAddress.getAddress().length == 4) { targetSize = 4; mask = (new BigInteger(1, MASK_IPV4)).not().shiftRight(prefixLength); } else { targetSize = 16; mask = (new BigInteger(1, MASK_IPV6)).not().shiftRight(prefixLength); } final BigInteger ipVal = new BigInteger(1, inetAddress.getAddress()); final BigInteger startIp = ipVal.and(mask); final BigInteger endIp = startIp.add(mask.not()); final byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize); final byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize); this.startAddress = InetAddress.getByAddress(startIpArr); this.endAddress = InetAddress.getByAddress(endIpArr); }
mantissa = quotAndRem[0].shiftLeft(2).add( BigInteger.valueOf((compRem * (compRem + 3)) / 2 + 1)); exponent -= 2; discardedSize = mantissa.bitLength() - 54; if (discardedSize > 0) {// (n > 54) bits = mantissa.shiftRight(discardedSize).longValue(); tempBits = bits;
private void mulShift(BigInteger multiplicand, int multiplierShift) { _significand = _significand.multiply(multiplicand); _binaryExponent += multiplierShift; // check for too much precision int sc = (_significand.bitLength() - MIN_PRECISION) & 0xFFFFFFE0; // mask makes multiples of 32 which optimises BigInteger.shiftRight if (sc > 0) { // no need to round because we have at least 8 bits of extra precision _significand = _significand.shiftRight(sc); _binaryExponent += sc; } }
sqrt0 = sqrtApproxWithDoubles(x.shiftRight(shift)).shiftLeft(shift >> 1); BigInteger sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1); if (sqrt0.equals(sqrt1)) { return sqrt0; sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1); } while (sqrt1.compareTo(sqrt0) < 0); return sqrt0;
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); BigInteger tmpDen = denominator.shiftRight(twos); // x / 2^n === x >> n int precision = Math.max(numerator.bitLength(), denominator.bitLength()); precision = (int)Math.ceil(precision / LG10);
static double bigToDouble(BigInteger x) { int exponent = absX.bitLength() - 1; long twiceSignifFloor = absX.shiftRight(shift).longValue(); long signifFloor = twiceSignifFloor >> 1;
sqrt0 = sqrtApproxWithDoubles(x.shiftRight(shift)).shiftLeft(shift >> 1); BigInteger sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1); if (sqrt0.equals(sqrt1)) { return sqrt0; sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1); } while (sqrt1.compareTo(sqrt0) < 0); return sqrt0;
public static BigInteger sqrt(BigInteger x) { BigInteger div = BigInteger.ZERO.setBit(x.bitLength()/2); BigInteger div2 = div; // Loop until we hit the same value twice in a row, or wind // up alternating. for(;;) { BigInteger y = div.add(x.divide(div)).shiftRight(1); if (y.equals(div) || y.equals(div2)) return y; div2 = div; div = y; } }
private static final double LOG2 = Math.log(2.0); /** * Computes the natural logarithm of a BigInteger. Works for really big * integers (practically unlimited) * * @param val Argument, positive integer * @return Natural logarithm, as in <tt>Math.log()</tt> */ public static double logBigInteger(BigInteger val) { int blex = val.bitLength() - 1022; // any value in 60..1023 is ok if (blex > 0) val = val.shiftRight(blex); double res = Math.log(val.doubleValue()); return blex > 0 ? res + blex * LOG2 : res; }
sqrt0 = sqrtApproxWithDoubles(x.shiftRight(shift)).shiftLeft(shift >> 1); BigInteger sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1); if (sqrt0.equals(sqrt1)) { return sqrt0; sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1); } while (sqrt1.compareTo(sqrt0) < 0); return sqrt0;
private static BigInteger isqrt(BigInteger x) { BigInteger g0 = x.shiftRight(x.bitLength() / 2); for (;;) { BigInteger g1 = g0.add(x.divide(g0)).shiftRight(1); if (g1.equals(g0)) { return g1; } g0 = g1; } }
private TenPower(int index) { BigInteger fivePowIndex = FIVE.pow(index); int bitsDueToFiveFactors = fivePowIndex.bitLength(); int px = 80 + bitsDueToFiveFactors; BigInteger fx = BigInteger.ONE.shiftLeft(px).divide(fivePowIndex); int adj = fx.bitLength() - 80; _divisor = fx.shiftRight(adj); bitsDueToFiveFactors -= adj; _divisorShift = -(bitsDueToFiveFactors+index+80); int sc = fivePowIndex.bitLength() - 68; if (sc > 0) { _multiplierShift = index + sc; _multiplicand = fivePowIndex.shiftRight(sc); } else { _multiplierShift = index; _multiplicand = fivePowIndex; } }
private void assertShiftRightArray8(BigInteger value, int rightShifts, boolean roundUp) { BigInteger expectedResult = value.shiftRight(rightShifts); if (roundUp) { expectedResult = expectedResult.add(BigInteger.ONE); } int[] ints = toInt8Array(value); Slice result = unscaledDecimal(); shiftRightArray8(ints, rightShifts, result); assertEquals(decodeUnscaledValue(result), expectedResult); }
private static BigInteger isqrt(BigInteger x) { BigInteger g0 = x.shiftRight(x.bitLength() / 2); for (;;) { BigInteger g1 = g0.add(x.divide(g0)).shiftRight(1); if (g1.equals(g0)) { return g1; } g0 = g1; } }
private TenPower(int index) { BigInteger fivePowIndex = FIVE.pow(index); int bitsDueToFiveFactors = fivePowIndex.bitLength(); int px = 80 + bitsDueToFiveFactors; BigInteger fx = BigInteger.ONE.shiftLeft(px).divide(fivePowIndex); int adj = fx.bitLength() - 80; _divisor = fx.shiftRight(adj); bitsDueToFiveFactors -= adj; _divisorShift = -(bitsDueToFiveFactors+index+80); int sc = fivePowIndex.bitLength() - 68; if (sc > 0) { _multiplierShift = index + sc; _multiplicand = fivePowIndex.shiftRight(sc); } else { _multiplierShift = index; _multiplicand = fivePowIndex; } }
@Test public void testShiftRight() { assertShiftRight(unscaledDecimal(0), 0, true, unscaledDecimal(0)); assertShiftRight(unscaledDecimal(0), 33, true, unscaledDecimal(0)); assertShiftRight(unscaledDecimal(1), 1, true, unscaledDecimal(1)); assertShiftRight(unscaledDecimal(-4), 1, true, unscaledDecimal(-2)); assertShiftRight(unscaledDecimal(1L << 32), 32, true, unscaledDecimal(1)); assertShiftRight(unscaledDecimal(1L << 31), 32, true, unscaledDecimal(1)); assertShiftRight(unscaledDecimal(1L << 31), 32, false, unscaledDecimal(0)); assertShiftRight(unscaledDecimal(3L << 33), 34, true, unscaledDecimal(2)); assertShiftRight(unscaledDecimal(3L << 33), 34, false, unscaledDecimal(1)); assertShiftRight(unscaledDecimal(BigInteger.valueOf(0x7FFFFFFFFFFFFFFFL).setBit(63).setBit(64)), 1, true, unscaledDecimal(BigInteger.ONE.shiftLeft(64))); assertShiftRight(MAX_DECIMAL, 1, true, unscaledDecimal(MAX_DECIMAL_UNSCALED_VALUE.shiftRight(1).add(BigInteger.ONE))); assertShiftRight(MIN_DECIMAL, 1, true, unscaledDecimal(MAX_DECIMAL_UNSCALED_VALUE.shiftRight(1).add(BigInteger.ONE).negate())); assertShiftRight(MAX_DECIMAL, 66, true, unscaledDecimal(MAX_DECIMAL_UNSCALED_VALUE.shiftRight(66).add(BigInteger.ONE))); }
private static BigInteger isqrt(BigInteger x) { BigInteger g0 = x.shiftRight(x.bitLength() / 2); for (;;) { BigInteger g1 = g0.add(x.divide(g0)).shiftRight(1); if (g1.equals(g0)) { return g1; } g0 = g1; } }