/** * Multiplies a number by a power of ten. * This method is used in {@code BigDecimal} class. * @param val the number to be multiplied * @param exp a positive {@code long} exponent * @return {@code val * 10<sup>exp</sup>} */ static BigInteger multiplyByTenPow(BigInteger val, long exp) { // PRE: exp >= 0 return ((exp < tenPows.length) ? multiplyByPositiveInt(val, tenPows[(int)exp]) : val.multiply(powerOf10(exp))); }
/** * Returns this {@code BigDecimal} as a big integer instance. A fractional * part is discarded. * * @return this {@code BigDecimal} as a big integer instance. */ public BigInteger toBigInteger() { if ((scale == 0) || (isZero())) { return getUnscaledValue(); } else if (scale < 0) { return getUnscaledValue().multiply(Multiplication.powerOf10(-(long)scale)); } else {// (scale > 0) return getUnscaledValue().divide(Multiplication.powerOf10(scale)); } }
/** * Returns the precision of this {@code BigDecimal}. The precision is the * number of decimal digits used to represent this decimal. It is equivalent * to the number of digits of the unscaled value. The precision of {@code 0} * is {@code 1} (independent of the scale). * * @return the precision of this {@code BigDecimal}. */ public int precision() { // Return the cached value if we have one. if (precision != 0) { return precision; } if (bitLength == 0) { precision = 1; } else if (bitLength < 64) { precision = decimalDigitsInLong(smallValue); } else { int decimalDigits = 1 + (int) ((bitLength - 1) * LOG10_2); // If after division the number isn't zero, there exists an additional digit if (getUnscaledValue().divide(Multiplication.powerOf10(decimalDigits)).signum() != 0) { decimalDigits++; } precision = decimalDigits; } return precision; }
/** * Returns this {@code BigDecimal} as a big integer instance if it has no * fractional part. If this {@code BigDecimal} has a fractional part, i.e. * if rounding would be necessary, an {@code ArithmeticException} is thrown. * * @return this {@code BigDecimal} as a big integer value. * @throws ArithmeticException * if rounding is necessary. */ public BigInteger toBigIntegerExact() { if ((scale == 0) || (isZero())) { return getUnscaledValue(); } else if (scale < 0) { return getUnscaledValue().multiply(Multiplication.powerOf10(-(long)scale)); } else {// (scale > 0) BigInteger[] integerAndFraction; // An optimization before do a heavy division if ((scale > approxPrecision()) || (scale > getUnscaledValue().getLowestSetBit())) { throw new ArithmeticException("Rounding necessary"); } integerAndFraction = getUnscaledValue().divideAndRemainder(Multiplication.powerOf10(scale)); if (integerAndFraction[1].signum() != 0) { // It exists a non-zero fractional part throw new ArithmeticException("Rounding necessary"); } return integerAndFraction[0]; } }
divisor.getUnscaledValue().multiply(Multiplication.powerOf10(diffScale)) ); quotAndRem[0] = quotAndRem[0].multiply(Multiplication.powerOf10(newScale)); } else {// CASE s2 > s1: long compRemDiv; quotAndRem = this.getUnscaledValue().multiply(Multiplication.powerOf10(exp)). divideAndRemainder(divisor.getUnscaledValue()); if (compRemDiv == 0) { quotAndRem[1] = quotAndRem[1].multiply(Multiplication.powerOf10(exp)). divide(divisor.getUnscaledValue()); compRemDiv = Math.abs(quotAndRem[1].signum());
return dividePrimitiveLongs(this.smallValue, MathUtils.LONG_POWERS_OF_TEN[(int)-diffScale], newScale,roundingMode); return divideBigIntegers(this.getUnscaledValue(),Multiplication.powerOf10(-diffScale),newScale,roundingMode);
BigInteger sizeOfFraction = Multiplication.powerOf10(discardedPrecision); BigInteger[] integerAndFraction = getUnscaledValue().divideAndRemainder(sizeOfFraction); long newScale = (long)scale - discardedPrecision;
integralValue = getUnscaledValue().divide( divisor.getUnscaledValue() ); } else if (newScale > 0) { powerOfTen = Multiplication.powerOf10(newScale); integralValue = getUnscaledValue().divide( divisor.getUnscaledValue().multiply(powerOfTen) ); integralValue = integralValue.multiply(powerOfTen); } else {// (newScale < 0) powerOfTen = Multiplication.powerOf10(-newScale); integralValue = getUnscaledValue().multiply(powerOfTen).divide( divisor.getUnscaledValue() );
quotAndRem[0] = getUnscaledValue().multiply( Multiplication.powerOf10(trailingZeros) ); newScale += trailingZeros;
/** * Multiplies a number by a power of ten. * This method is used in {@code BigDecimal} class. * @param val the number to be multiplied * @param exp a positive {@code long} exponent * @return {@code val * 10<sup>exp</sup>} */ static BigInteger multiplyByTenPow(BigInteger val, long exp) { // PRE: exp >= 0 return ((exp < tenPows.length) ? multiplyByPositiveInt(val, tenPows[(int)exp]) : val.multiply(powerOf10(exp))); }
/** * Multiplies a number by a power of ten. * This method is used in {@code BigDecimal} class. * @param val the number to be multiplied * @param exp a positive {@code long} exponent * @return {@code val * 10<sup>exp</sup>} */ static BigInteger multiplyByTenPow(BigInteger val, long exp) { // PRE: exp >= 0 return ((exp < tenPows.length) ? multiplyByPositiveInt(val, tenPows[(int)exp]) : val.multiply(powerOf10(exp))); }
/** * Multiplies a number by a power of ten. * This method is used in {@code BigDecimal} class. * @param val the number to be multiplied * @param exp a positive {@code long} exponent * @return {@code val * 10<sup>exp</sup>} */ static BigInteger multiplyByTenPow(BigInteger val, long exp) { // PRE: exp >= 0 return ((exp < tenPows.length) ? multiplyByPositiveInt(val, tenPows[(int)exp]) : val.multiply(powerOf10(exp))); }
/** * Multiplies a number by a power of ten. * This method is used in {@code BigDecimal} class. * @param val the number to be multiplied * @param exp a positive {@code long} exponent * @return {@code val * 10<sup>exp</sup>} */ static BigInteger multiplyByTenPow(BigInteger val, long exp) { // PRE: exp >= 0 return ((exp < tenPows.length) ? multiplyByPositiveInt(val, tenPows[(int)exp]) : val.multiply(powerOf10(exp))); }
/** * Multiplies a number by a power of ten. * This method is used in {@code BigDecimal} class. * @param val the number to be multiplied * @param exp a positive {@code long} exponent * @return {@code val * 10<sup>exp</sup>} */ static BigInteger multiplyByTenPow(BigInteger val, long exp) { // PRE: exp >= 0 return ((exp < tenPows.length) ? multiplyByPositiveInt(val, tenPows[(int)exp]) : val.multiply(powerOf10(exp))); }
/** * Multiplies a number by a power of ten. * This method is used in {@code BigDecimal} class. * @param val the number to be multiplied * @param exp a positive {@code long} exponent * @return {@code val * 10<sup>exp</sup>} */ static BigInteger multiplyByTenPow(BigInteger val, long exp) { // PRE: exp >= 0 return ((exp < tenPows.length) ? multiplyByPositiveInt(val, tenPows[(int)exp]) : val.multiply(powerOf10(exp))); }
/** * Returns this {@code BigDecimal} as a big integer instance. A fractional * part is discarded. * * @return this {@code BigDecimal} as a big integer instance. */ public BigInteger toBigInteger() { if ((scale == 0) || (isZero())) { return getUnscaledValue(); } else if (scale < 0) { return getUnscaledValue().multiply(Multiplication.powerOf10(-(long)scale)); } else {// (scale > 0) return getUnscaledValue().divide(Multiplication.powerOf10(scale)); } }
/** * Returns this {@code BigDecimal} as a big integer instance. A fractional * part is discarded. * * @return this {@code BigDecimal} as a big integer instance. */ public BigInteger toBigInteger() { if ((scale == 0) || (isZero())) { return getUnscaledValue(); } else if (scale < 0) { return getUnscaledValue().multiply(Multiplication.powerOf10(-(long)scale)); } else {// (scale > 0) return getUnscaledValue().divide(Multiplication.powerOf10(scale)); } }
/** * Returns this {@code BigDecimal} as a big integer instance. A fractional * part is discarded. * * @return this {@code BigDecimal} as a big integer instance. */ public BigInteger toBigInteger() { if ((scale == 0) || (isZero())) { return getUnscaledValue(); } else if (scale < 0) { return getUnscaledValue().multiply(Multiplication.powerOf10(-(long)scale)); } else {// (scale > 0) return getUnscaledValue().divide(Multiplication.powerOf10(scale)); } }