if ((scale == 0) || ((isZero()) && (scale < 0))) { return intStr;
/** * Returns a new {@code BigDecimal} whose value is {@code this<sup>n</sup>}. The * scale of the result is {@code n * this.scale()}. * * <p>{@code x.pow(0)} returns {@code 1}, even if {@code x == 0}. * * <p>Implementation Note: The implementation is based on the ANSI standard * X3.274-1996 algorithm. * * @throws ArithmeticException * if {@code n < 0} or {@code n > 999999999}. */ public BigDecimal pow(int n) { if (n == 0) { return ONE; } if ((n < 0) || (n > 999999999)) { throw new ArithmeticException("Invalid operation"); } long newScale = scale * (long)n; // Let be: this = [u,s] so: this^n = [u^n, s*n] return isZero() ? zeroScaledBy(newScale) : new BigDecimal(getUnscaledValue().pow(n), safeLongToInt(newScale)); }
/** * Returns a new {@code BigDecimal} whose value is {@code this * * multiplicand}. The scale of the result is the sum of the scales of the * two arguments. * * @param multiplicand * value to be multiplied with {@code this}. * @return {@code this * multiplicand}. * @throws NullPointerException * if {@code multiplicand == null}. */ public BigDecimal multiply(BigDecimal multiplicand) { long newScale = (long)this.scale + multiplicand.scale; if ((this.isZero()) || (multiplicand.isZero())) { return zeroScaledBy(newScale); } /* Let be: this = [u1,s1] and multiplicand = [u2,s2] so: * this x multiplicand = [ s1 * s2 , s1 + s2 ] */ if(this.bitLength + multiplicand.bitLength < 64) { return valueOf(this.smallValue*multiplicand.smallValue, safeLongToInt(newScale)); } return new BigDecimal(this.getUnscaledValue().multiply( multiplicand.getUnscaledValue()), safeLongToInt(newScale)); }
/** * 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)); } }
if ((n == 0) || ((isZero()) && (n > 0))) { return pow(n);
private BigDecimal movePoint(long newScale) { if (isZero()) { return zeroScaledBy(Math.max(newScale, 0)); } /* * When: 'n'== Integer.MIN_VALUE isn't possible to call to * movePointRight(-n) since -Integer.MIN_VALUE == Integer.MIN_VALUE */ if(newScale >= 0) { if(bitLength < 64) { return valueOf(smallValue, safeLongToInt(newScale)); } return new BigDecimal(getUnscaledValue(), safeLongToInt(newScale)); } if(-newScale < MathUtils.LONG_POWERS_OF_TEN.length && bitLength + LONG_POWERS_OF_TEN_BIT_LENGTH[(int)-newScale] < 64 ) { return valueOf(smallValue*MathUtils.LONG_POWERS_OF_TEN[(int)-newScale],0); } return new BigDecimal(Multiplication.multiplyByTenPow( getUnscaledValue(), safeLongToInt(-newScale)), 0); }
int lastPow = FIVE_POW.length - 1; if (divisor.isZero()) { throw new ArithmeticException("Division by zero");
long newScale = scale; if (isZero()) {
/** * 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]; } }
throw new NullPointerException("roundingMode == null"); if (divisor.isZero()) { throw new ArithmeticException("Division by zero");
/** * 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)); } }