/** * Returns the unscaled value (mantissa) of this {@code BigDecimal} instance * as a {@code BigInteger}. The unscaled value can be computed as * {@code this * 10<sup>scale</sup>}. */ public BigInteger unscaledValue() { return getUnscaledValue(); }
/** * Prepares this {@code BigDecimal} for serialization, i.e. the * non-transient field {@code intVal} is assigned. */ private void writeObject(ObjectOutputStream out) throws IOException { getUnscaledValue(); out.defaultWriteObject(); }
/** * Returns the sign of this {@code BigDecimal}. * * @return {@code -1} if {@code this < 0}, * {@code 0} if {@code this == 0}, * {@code 1} if {@code this > 0}. */ public int signum() { if( bitLength < 64) { return Long.signum( this.smallValue ); } return getUnscaledValue().signum(); }
/** * Returns a new {@code BigDecimal} whose value is the {@code -this}. The * scale of the result is the same as the scale of this. * * @return {@code -this} */ public BigDecimal negate() { if(bitLength < 63 || (bitLength == 63 && smallValue!=Long.MIN_VALUE)) { return valueOf(-smallValue,scale); } return new BigDecimal(getUnscaledValue().negate(), scale); }
/** * Returns a {@code BigDecimal} whose value is the absolute value of * {@code this}. The result is rounded according to the passed context * {@code mc}. */ public BigDecimal abs(MathContext mc) { BigDecimal result = (signum() < 0) ? negate() : new BigDecimal(getUnscaledValue(), scale); result.inplaceRound(mc); return result; }
/** * 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)); } }
private static BigDecimal addAndMult10(BigDecimal thisValue,BigDecimal augend, int diffScale) { if(diffScale < MathUtils.LONG_POWERS_OF_TEN.length && Math.max(thisValue.bitLength,augend.bitLength+LONG_POWERS_OF_TEN_BIT_LENGTH[diffScale])+1<64) { return valueOf(thisValue.smallValue+augend.smallValue*MathUtils.LONG_POWERS_OF_TEN[diffScale],thisValue.scale); } else { BigInt bi = Multiplication.multiplyByTenPow(augend.getUnscaledValue(),diffScale).getBigInt(); bi.add(thisValue.getUnscaledValue().getBigInt()); return new BigDecimal(new BigInteger(bi), thisValue.scale); } }
/** * Returns a new {@code BigDecimal} whose value is {@code this}, rounded * according to the passed context {@code mc}. * <p> * If {@code mc.precision = 0}, then no rounding is performed. * <p> * If {@code mc.precision > 0} and {@code mc.roundingMode == UNNECESSARY}, * then an {@code ArithmeticException} is thrown if the result cannot be * represented exactly within the given precision. * * @param mc * rounding mode and precision for the result of this operation. * @return {@code this} rounded according to the passed context. * @throws ArithmeticException * if {@code mc.precision > 0} and {@code mc.roundingMode == * UNNECESSARY} and this cannot be represented within the given * precision. */ public BigDecimal round(MathContext mc) { BigDecimal thisBD = new BigDecimal(getUnscaledValue(), scale); thisBD.inplaceRound(mc); return thisBD; }
return toStringImage; String intString = getUnscaledValue().toString(); if (scale == 0) { return intString; int begin = (getUnscaledValue().signum() < 0) ? 2 : 1; int end = intString.length(); long exponent = -(long)scale + end - begin;
/** * 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]; } }
/** * 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 a new {@code BigDecimal} whose value is {@code this * 10<sup>n</sup>}. * The scale of the result is {@code this.scale()} - {@code n}. * The precision of the result is the precision of {@code this}. * * <p>This method has the same effect as {@link #movePointRight}, except that * the precision is not changed. */ public BigDecimal scaleByPowerOfTen(int n) { long newScale = scale - (long)n; if(bitLength < 64) { //Taking care when a 0 is to be scaled if( smallValue==0 ){ return zeroScaledBy( newScale ); } return valueOf(smallValue, safeLongToInt(newScale)); } return new BigDecimal(getUnscaledValue(), safeLongToInt(newScale)); }
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); }
/** * 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)); }
return -thisSign; } else {// thisSign == val.signum() and diffPrecision is aprox. diffScale BigInteger thisUnscaled = this.getUnscaledValue(); BigInteger valUnscaled = val.getUnscaledValue();
return valueOf(this.smallValue*MathUtils.LONG_POWERS_OF_TEN[(int)diffScale],newScale); return new BigDecimal(Multiplication.multiplyByTenPow(getUnscaledValue(),(int)diffScale), newScale); return dividePrimitiveLongs(this.smallValue, MathUtils.LONG_POWERS_OF_TEN[(int)-diffScale], newScale,roundingMode); return divideBigIntegers(this.getUnscaledValue(),Multiplication.powerOf10(-diffScale),newScale,roundingMode);
return valueOf(this.smallValue + augend.smallValue, this.scale); return new BigDecimal(this.getUnscaledValue().add(augend.getUnscaledValue()), this.scale); } else if (diffScale > 0) {
thisSignum = this.signum(); if (thisSignum != subtrahend.signum()) { tempBI = Multiplication.multiplyByPositiveInt(this.getUnscaledValue(), 10) .add(BigInteger.valueOf(thisSignum)); } else { tempBI = this.getUnscaledValue().subtract(BigInteger.valueOf(thisSignum)); tempBI = Multiplication.multiplyByPositiveInt(tempBI, 10) .add(BigInteger.valueOf(thisSignum * 9));
BigInteger strippedBI = getUnscaledValue(); BigInteger[] quotAndRem;