Java Code Examples for java.math.BigInteger#abs()
The following examples show how to use
java.math.BigInteger#abs() .
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: ECAlgorithms.java From ripple-lib-java with ISC License | 6 votes |
/** * Simple shift-and-add multiplication. Serves as reference implementation * to verify (possibly faster) implementations, and for very small scalars. * * @param p * The point to multiply. * @param k * The multiplier. * @return The result of the point multiplication <code>kP</code>. */ public static ECPoint referenceMultiply(ECPoint p, BigInteger k) { BigInteger x = k.abs(); ECPoint q = p.getCurve().getInfinity(); int t = x.bitLength(); if (t > 0) { if (x.testBit(0)) { q = p; } for (int i = 1; i < t; i++) { p = p.twice(); if (x.testBit(i)) { q = q.add(p); } } } return k.signum() < 0 ? q.negate() : q; }
Example 2
Source File: ECAlgorithms.java From RipplePower with Apache License 2.0 | 6 votes |
static ECPoint implSumOfMultiplies(ECPoint[] ps, BigInteger[] ks) { int count = ps.length; boolean[] negs = new boolean[count]; WNafPreCompInfo[] infos = new WNafPreCompInfo[count]; byte[][] wnafs = new byte[count][]; for (int i = 0; i < count; ++i) { BigInteger ki = ks[i]; negs[i] = ki.signum() < 0; ki = ki.abs(); int width = Math.max(2, Math.min(16, WNafUtil.getWindowSize(ki.bitLength()))); infos[i] = WNafUtil.precompute(ps[i], width, true); wnafs[i] = WNafUtil.generateWindowNaf(width, ki); } return implSumOfMultiplies(negs, infos, wnafs); }
Example 3
Source File: RationalChap14Quiz19.java From BUPTJava with Apache License 2.0 | 6 votes |
private static BigInteger gcd1(BigInteger n, BigInteger d) { BigInteger n1 = n.abs(); BigInteger n2 = d.abs(); BigInteger temp; if(n1.equals(new BigInteger("0"))){ // 分子为0, 直接返回1即可 return new BigInteger("1"); } if(n1.compareTo(n2) <= 0) { // 确保n1 > n2 temp = n1; n1 = n2; n2 = temp; } temp = n1.mod(n2); while(!temp.equals(new BigInteger("0"))){ n1 = n2; n2 = temp; temp = n1.mod(n2); } return n2; }
Example 4
Source File: BouncyCastleCrypto.java From fabric-api with Apache License 2.0 | 6 votes |
@Override public byte[] getPublicKeyAtOffset(byte[] publicKey, byte[] offset) { BigInteger offsetInt = new BigInteger(publicKey); boolean invert = false; if (offsetInt.compareTo(BigInteger.ZERO) < 0) { invert = true; offsetInt = offsetInt.abs(); } ECPoint oG = curve.getG().multiply(offsetInt); if (invert) { oG = oG.negate(); } return oG.add(curve.getCurve().decodePoint(publicKey)).getEncoded(true); }
Example 5
Source File: IntMath.java From jpexs-decompiler with GNU General Public License v3.0 | 6 votes |
/** * Returns a long whose value is the greatest common divisor of * <tt>abs(a)</tt> and <tt>abs(b)</tt>. Returns 0 if * <tt>a==0 && b==0</tt>. * * @param a value with with the GCD is to be computed. * @param b value with with the GCD is to be computed. * @return <tt>GCD(a, b)</tt> */ public static BigInteger gcd(BigInteger a, BigInteger b) { // Quelle: // Herrmann, D. (1992). Algorithmen Arbeitsbuch. // Bonn, München Paris: Addison Wesley. // ggt6, Seite 63 a = a.abs(); b = b.abs(); while (a.compareTo(BigInteger.ZERO) > 0 && b.compareTo(BigInteger.ZERO) > 0) { a = a.mod(b); if (a.compareTo(BigInteger.ZERO) > 0) b = b.mod(a); } return a.add(b); }
Example 6
Source File: ECAlgorithms.java From ripple-lib-java with ISC License | 6 votes |
static ECPoint implSumOfMultiplies(ECPoint[] ps, ECPointMap pointMap, BigInteger[] ks) { int halfCount = ps.length, fullCount = halfCount << 1; boolean[] negs = new boolean[fullCount]; WNafPreCompInfo[] infos = new WNafPreCompInfo[fullCount]; byte[][] wnafs = new byte[fullCount][]; for (int i = 0; i < halfCount; ++i) { int j0 = i << 1, j1 = j0 + 1; BigInteger kj0 = ks[j0]; negs[j0] = kj0.signum() < 0; kj0 = kj0.abs(); BigInteger kj1 = ks[j1]; negs[j1] = kj1.signum() < 0; kj1 = kj1.abs(); int width = Math.max(2, Math.min(16, WNafUtil.getWindowSize(Math.max(kj0.bitLength(), kj1.bitLength())))); ECPoint P = ps[i], Q = WNafUtil.mapPointWithPrecomp(P, width, true, pointMap); infos[j0] = WNafUtil.getWNafPreCompInfo(P); infos[j1] = WNafUtil.getWNafPreCompInfo(Q); wnafs[j0] = WNafUtil.generateWindowNaf(width, kj0); wnafs[j1] = WNafUtil.generateWindowNaf(width, kj1); } return implSumOfMultiplies(negs, infos, wnafs); }
Example 7
Source File: Gcd.java From symja_android_library with GNU General Public License v3.0 | 6 votes |
public BigInteger gcd_euclid_withoutDivision(BigInteger m, BigInteger n) { m = m.abs(); n = n.abs(); int mCmp1 = m.compareTo(I_1); int nCmp1 = n.compareTo(I_1); if (mCmp1>0 && nCmp1>0) { int cmp = m.compareTo(n); while (cmp != 0) { //LOG.debug("m = " + m + ", n = " + n + ", cmp = " + cmp); if (cmp > 0) { m = m.subtract(n); } else { n = n.subtract(m); } cmp = m.compareTo(n); } return m; } if (mCmp1<0) return n; if (nCmp1<0) return m; // else one argument is 1 return I_1; }
Example 8
Source File: BPSWTest.java From symja_android_library with GNU General Public License v3.0 | 5 votes |
public boolean isProbablePrime(BigInteger N) { N = N.abs(); // sign is irrelevant if (!N.testBit(0)) return N.equals(I_2); // even N>2 is not prime // For small N, trial division is much faster than BPSW if (N.bitLength() < 32) { return TDivPrimeTest.getInstance().isPrime(N.intValue()); } // Test residues % 30030. Note that N<30030 have been exclude by trial division above. if (!primeRestsMod30030.contains(N.mod(BIG_30030).intValue())) return false; // The Lucas test is not carried out if N fails the base 2 Miller-Rabin test. return millerRabinTest.testSingleBase(N, I_2) && lucasTest.isStrongProbablePrime(N); }
Example 9
Source File: IntegerToBigInteger.java From copybook4java with MIT License | 5 votes |
@Override public byte[] from(Object value, int length, int decimals, boolean addPadding) { if(value == null && this.defaultValue == null) { return null; } BigInteger i = value != null ? (BigInteger)value : new BigInteger(this.defaultValue); if(i.signum() == -1) { throw new TypeConverterException("Number can not be negative"); } byte[] strBytes; BigInteger absValue = i.abs(); if(absValue.signum() == 0) { // Make sure we fill the strBytes with the number of decimals plus one extra zero strBytes = new byte[decimals + 1]; Arrays.fill(strBytes, ("0".getBytes(this.charset))[0]); } else { strBytes = absValue.toString().getBytes(this.charset); } if(strBytes.length > length) { throw new TypeConverterException("Field to small for value: " + length + " < " + strBytes.length); } if(addPadding) { strBytes = padBytes(strBytes, length); } return strBytes; }
Example 10
Source File: DiscoverEndomorphisms.java From ripple-lib-java with ISC License | 5 votes |
private static boolean isLessThanSqrt(BigInteger a, BigInteger b) { a = a.abs(); b = b.abs(); int target = b.bitLength(), maxBits = a.bitLength() * 2, minBits = maxBits - 1; return minBits <= target && (maxBits < target || a.multiply(a).compareTo(b) < 0); }
Example 11
Source File: ECFieldElement.java From RipplePower with Apache License 2.0 | 5 votes |
protected BigInteger modReduce(BigInteger x) { if (r != null) { boolean negative = x.signum() < 0; if (negative) { x = x.abs(); } int qLen = q.bitLength(); boolean rIsOne = r.equals(ECConstants.ONE); while (x.bitLength() > (qLen + 1)) { BigInteger u = x.shiftRight(qLen); BigInteger v = x.subtract(u.shiftLeft(qLen)); if (!rIsOne) { u = u.multiply(r); } x = u.add(v); } while (x.compareTo(q) >= 0) { x = x.subtract(q); } if (negative && x.signum() != 0) { x = q.subtract(x); } } else { x = x.mod(q); } return x; }
Example 12
Source File: BitwiseShift.java From binnavi with Apache License 2.0 | 5 votes |
@Override public String toString() { if (m_rhs instanceof Literal) { final BigInteger value = ((Literal) m_rhs).getValue(); if (value.compareTo(BigInteger.ZERO) < 0) { return "(" + m_lhs + " >> " + value.abs() + ")"; } else { return "(" + m_lhs + " << " + m_rhs + ")"; } } else { return "(" + m_lhs + " SHIFT " + m_rhs + ")"; } }
Example 13
Source File: Primality.java From symja_android_library with GNU General Public License v3.0 | 5 votes |
/** * Least common multiple * * @param arg1 * @param arg2 * @return */ public static BigInteger lcm(BigInteger arg1, BigInteger arg2) { if (arg1.equals(BigInteger.ZERO) || arg2.equals(BigInteger.ZERO)) { return BigInteger.ZERO; } BigInteger a = arg1.abs(); BigInteger b = arg2.abs(); BigInteger gcd = arg1.gcd(arg2); return a.multiply(b).divide(gcd); }
Example 14
Source File: PrPTest.java From symja_android_library with GNU General Public License v3.0 | 5 votes |
/** * @param N * @return first prime > N */ public BigInteger nextProbablePrime(BigInteger N) { // Java's built-in function using a sieve to identify prime candidates is stronger for N>=256 bit. if (N.bitLength()>=256) return N.nextProbablePrime(); N = N.abs(); // sign is irrelevant if (N.bitLength()<=1) return I_2; // skip argument and make even argument odd N = N.testBit(0) ? N.add(I_2) : N.add(I_1); // loop to next prime while (!isProbablePrime(N)) N = N.add(I_2); return N; }
Example 15
Source File: BPSWTest.java From symja_android_library with GNU General Public License v3.0 | 5 votes |
/** * @param N * @return first prime > N */ public BigInteger nextProbablePrime(BigInteger N) { // Java's built-in function using a sieve to identify prime candidates is stronger for N>=256 bit. if (N.bitLength()>=256) return N.nextProbablePrime(); N = N.abs(); // sign is irrelevant if (N.bitLength()<=1) return I_2; // skip argument and make even argument odd N = N.testBit(0) ? N.add(I_2) : N.add(I_1); // loop to next prime while (!isProbablePrime(N)) N = N.add(I_2); return N; }
Example 16
Source File: BigIntegerCompareTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * abs() for a negative number */ public void testAbsNegative() { byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; int aSign = -1; byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger result = aNumber.abs(); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for(int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
Example 17
Source File: DoubleUtils.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
static double bigToDouble(BigInteger x) { // This is an extremely fast implementation of BigInteger.doubleValue(). JDK patch pending. BigInteger absX = x.abs(); int exponent = absX.bitLength() - 1; // exponent == floor(log2(abs(x))) if (exponent < Long.SIZE - 1) { return x.longValue(); } else if (exponent > MAX_EXPONENT) { return x.signum() * POSITIVE_INFINITY; } /* * We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make rounding * easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us round up or * down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and signifFloor the * top SIGNIFICAND_BITS + 1. * * It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent). */ int shift = exponent - SIGNIFICAND_BITS - 1; long twiceSignifFloor = absX.shiftRight(shift).longValue(); long signifFloor = twiceSignifFloor >> 1; signifFloor &= SIGNIFICAND_MASK; // remove the implied bit /* * We round up if either the fractional part of signif is strictly greater than 0.5 (which is * true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is * >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set). */ boolean increment = (twiceSignifFloor & 1) != 0 && ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift); long signifRounded = increment? signifFloor + 1 : signifFloor; long bits = (long) ((exponent + EXPONENT_BIAS)) << SIGNIFICAND_BITS; bits += signifRounded; /* * If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to * the exponent. This is exactly the behavior we get from just adding signifRounded to bits * directly. If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to * Double.POSITIVE_INFINITY. */ bits |= x.signum() & SIGN_MASK; return longBitsToDouble(bits); }
Example 18
Source File: DoubleUtils.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
static double bigToDouble(BigInteger x) { // This is an extremely fast implementation of BigInteger.doubleValue(). JDK patch pending. BigInteger absX = x.abs(); int exponent = absX.bitLength() - 1; // exponent == floor(log2(abs(x))) if (exponent < Long.SIZE - 1) { return x.longValue(); } else if (exponent > MAX_EXPONENT) { return x.signum() * POSITIVE_INFINITY; } /* * We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make rounding * easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us round up or * down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and signifFloor the * top SIGNIFICAND_BITS + 1. * * It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent). */ int shift = exponent - SIGNIFICAND_BITS - 1; long twiceSignifFloor = absX.shiftRight(shift).longValue(); long signifFloor = twiceSignifFloor >> 1; signifFloor &= SIGNIFICAND_MASK; // remove the implied bit /* * We round up if either the fractional part of signif is strictly greater than 0.5 (which is * true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is * >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set). */ boolean increment = (twiceSignifFloor & 1) != 0 && ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift); long signifRounded = increment? signifFloor + 1 : signifFloor; long bits = (long) ((exponent + EXPONENT_BIAS)) << SIGNIFICAND_BITS; bits += signifRounded; /* * If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to * the exponent. This is exactly the behavior we get from just adding signifRounded to bits * directly. If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to * Double.POSITIVE_INFINITY. */ bits |= x.signum() & SIGN_MASK; return longBitsToDouble(bits); }
Example 19
Source File: MathFunction.java From ddal with Apache License 2.0 | 4 votes |
public static Number abs(Number number) { if (number == null) { return null; } if (number instanceof Byte) { byte b = number.byteValue(); if (b < 0) { return (byte) -b; } } else if (number instanceof Short) { short s = number.shortValue(); if (s < 0) { return (short) -s; } } else if (number instanceof Integer) { int i = number.intValue(); if (i < 0) { return (int) -i; } } else if (number instanceof Long) { long l = number.longValue(); if (l < 0) { return (long) -l; } } else if (number instanceof Float) { float f = number.floatValue(); if (f < 0) { return (float) -f; } } else if (number instanceof Double) { double d = number.doubleValue(); if (d < 0) { return (double) -d; } } else if (number instanceof BigDecimal) { BigDecimal bigDecimal = (BigDecimal) number; return bigDecimal.abs(); } else if (number instanceof BigInteger) { BigInteger bigInteger = (BigInteger) number; bigInteger.abs(); } else { throw new UnsupportedOperationException("Can't calculate absolute value for type of " + number.getClass()); } return number; }
Example 20
Source File: BigIntegerOperators.java From totallylazy with Apache License 2.0 | 4 votes |
@Override public Number absolute(BigInteger value) { return value.abs(); }