public int compareTo(BigInteger val) { if (signum == val.signum) { switch (signum) { case 1: return compareMagnitude(val); case -1: return val.compareMagnitude(this); default: return 0; } } return signum > val.signum ? 1 : -1; }
/** * Compares this BigInteger with the specified BigInteger. This * method is provided in preference to individual methods for each * of the six boolean comparison operators ({@literal <}, ==, * {@literal >}, {@literal >=}, !=, {@literal <=}). The suggested * idiom for performing these comparisons is: {@code * (x.compareTo(y)} <<i>op</i>> {@code 0)}, where * <<i>op</i>> is one of the six comparison operators. * * @param val BigInteger to which this BigInteger is to be compared. * @return -1, 0 or 1 as this BigInteger is numerically less than, equal * to, or greater than {@code val}. */ public int compareTo(BigInteger val) { if (signum == val.signum) { switch (signum) { case 1: return compareMagnitude(val); case -1: return val.compareMagnitude(this); default: return 0; } } return signum > val.signum ? 1 : -1; }
/** * Compares this BigInteger with the specified BigInteger. This * method is provided in preference to individual methods for each * of the six boolean comparison operators ({@literal <}, ==, * {@literal >}, {@literal >=}, !=, {@literal <=}). The suggested * idiom for performing these comparisons is: {@code * (x.compareTo(y)} <<i>op</i>> {@code 0)}, where * <<i>op</i>> is one of the six comparison operators. * * @param val BigInteger to which this BigInteger is to be compared. * @return -1, 0 or 1 as this BigInteger is numerically less than, equal * to, or greater than {@code val}. */ public int compareTo(BigInteger val) { if (signum == val.signum) { switch (signum) { case 1: return compareMagnitude(val); case -1: return val.compareMagnitude(this); default: return 0; } } return signum > val.signum ? 1 : -1; }
/** * Returns the length of the absolute value of a BigInteger, in * decimal digits. * * @param b the BigInteger * @return the length of the unscaled value, in decimal digits */ private static int bigDigitLength(BigInteger b) { /* * Same idea as the long version, but we need a better * approximation of log10(2). Using 646456993/2^31 * is accurate up to max possible reported bitLength. */ if (b.signum == 0) return 1; int r = (int)((((long)b.bitLength() + 1) * 646456993) >>> 31); return b.compareMagnitude(bigTenToThe(r)) < 0? r : r+1; }
/** * Returns the length of the absolute value of a BigInteger, in * decimal digits. * * @param b the BigInteger * @return the length of the unscaled value, in decimal digits */ private static int bigDigitLength(BigInteger b) { /* * Same idea as the long version, but we need a better * approximation of log10(2). Using 646456993/2^31 * is accurate up to max possible reported bitLength. */ if (b.signum == 0) return 1; int r = (int)((((long)b.bitLength() + 1) * 646456993) >>> 31); return b.compareMagnitude(bigTenToThe(r)) < 0? r : r+1; }
/** * Returns a BigInteger whose value is {@code (this + val)}. * * @param val value to be added to this BigInteger. * @return {@code this + val} */ public BigInteger add(BigInteger val) { if (val.signum == 0) return this; if (signum == 0) return val; if (val.signum == signum) return new BigInteger(add(mag, val.mag), signum); int cmp = compareMagnitude(val); if (cmp == 0) return ZERO; int[] resultMag = (cmp > 0 ? subtract(mag, val.mag) : subtract(val.mag, mag)); resultMag = trustedStripLeadingZeroInts(resultMag); return new BigInteger(resultMag, cmp == signum ? 1 : -1); }
/** * Returns a BigInteger whose value is {@code (this - val)}. * * @param val value to be subtracted from this BigInteger. * @return {@code this - val} */ public BigInteger subtract(BigInteger val) { if (val.signum == 0) return this; if (signum == 0) return val.negate(); if (val.signum != signum) return new BigInteger(add(mag, val.mag), signum); int cmp = compareMagnitude(val); if (cmp == 0) return ZERO; int[] resultMag = (cmp > 0 ? subtract(mag, val.mag) : subtract(val.mag, mag)); resultMag = trustedStripLeadingZeroInts(resultMag); return new BigInteger(resultMag, cmp == signum ? 1 : -1); }
/** * Returns a BigInteger whose value is {@code (this - val)}. * * @param val value to be subtracted from this BigInteger. * @return {@code this - val} */ public BigInteger subtract(BigInteger val) { if (val.signum == 0) return this; if (signum == 0) return val.negate(); if (val.signum != signum) return new BigInteger(add(mag, val.mag), signum); int cmp = compareMagnitude(val); if (cmp == 0) return ZERO; int[] resultMag = (cmp > 0 ? subtract(mag, val.mag) : subtract(val.mag, mag)); resultMag = trustedStripLeadingZeroInts(resultMag); return new BigInteger(resultMag, cmp == signum ? 1 : -1); }
/** * Returns a BigInteger whose value is {@code (this + val)}. * * @param val value to be added to this BigInteger. * @return {@code this + val} */ public BigInteger add(BigInteger val) { if (val.signum == 0) return this; if (signum == 0) return val; if (val.signum == signum) return new BigInteger(add(mag, val.mag), signum); int cmp = compareMagnitude(val); if (cmp == 0) return ZERO; int[] resultMag = (cmp > 0 ? subtract(mag, val.mag) : subtract(val.mag, mag)); resultMag = trustedStripLeadingZeroInts(resultMag); return new BigInteger(resultMag, cmp == signum ? 1 : -1); }
/** * Returns a BigInteger whose value is {@code (this}<sup>-1</sup> {@code mod m)}. * * @param m the modulus. * @return {@code this}<sup>-1</sup> {@code mod m}. * @throws ArithmeticException {@code m} ≤ 0, or this BigInteger * has no multiplicative inverse mod m (that is, this BigInteger * is not <i>relatively prime</i> to m). */ public BigInteger modInverse(BigInteger m) { if (m.signum != 1) throw new ArithmeticException("BigInteger: modulus not positive"); if (m.equals(ONE)) return ZERO; // Calculate (this mod m) BigInteger modVal = this; if (signum < 0 || (this.compareMagnitude(m) >= 0)) modVal = this.mod(m); if (modVal.equals(ONE)) return ONE; MutableBigInteger a = new MutableBigInteger(modVal); MutableBigInteger b = new MutableBigInteger(m); MutableBigInteger result = a.mutableModInverse(b); return result.toBigInteger(1); }
ys == INFLATED) { rb = bigMultiplyPowerTen(-sdiff); return rb.compareMagnitude(val.intVal); xs == INFLATED) { rb = val.bigMultiplyPowerTen(sdiff); return this.intVal.compareMagnitude(rb); return 1; else return this.intVal.compareMagnitude(val.intVal);
ys == INFLATED) { rb = bigMultiplyPowerTen(-sdiff); return rb.compareMagnitude(val.intVal); xs == INFLATED) { rb = val.bigMultiplyPowerTen(sdiff); return this.intVal.compareMagnitude(rb); return 1; else return this.intVal.compareMagnitude(val.intVal);
/** * Returns a BigInteger whose value is {@code (this}<sup>-1</sup> {@code mod m)}. * * @param m the modulus. * @return {@code this}<sup>-1</sup> {@code mod m}. * @throws ArithmeticException {@code m} ≤ 0, or this BigInteger * has no multiplicative inverse mod m (that is, this BigInteger * is not <i>relatively prime</i> to m). */ public BigInteger modInverse(BigInteger m) { if (m.signum != 1) throw new ArithmeticException("BigInteger: modulus not positive"); if (m.equals(ONE)) return ZERO; // Calculate (this mod m) BigInteger modVal = this; if (signum < 0 || (this.compareMagnitude(m) >= 0)) modVal = this.mod(m); if (modVal.equals(ONE)) return ONE; MutableBigInteger a = new MutableBigInteger(modVal); MutableBigInteger b = new MutableBigInteger(m); MutableBigInteger result = a.mutableModInverse(b); return result.toBigInteger(1); }
this.inflate(); while ( intVal.compareMagnitude(BigInteger.TEN) >= 0 && scale > preferredScale) { if (intVal.testBit(0))
this.inflate(); while ( intVal.compareMagnitude(BigInteger.TEN) >= 0 && scale > preferredScale) { if (intVal.testBit(0))