2017-07-28 11:38:22 -07:00
|
|
|
package org.nwapw.abacus.number;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
2017-07-31 13:25:23 -07:00
|
|
|
import java.math.MathContext;
|
2017-07-28 11:38:22 -07:00
|
|
|
import java.math.RoundingMode;
|
|
|
|
|
2017-07-30 21:11:32 -07:00
|
|
|
public class PreciseNumber implements NumberInterface {
|
2017-07-28 11:38:22 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number one.
|
|
|
|
*/
|
|
|
|
static final PreciseNumber ONE = new PreciseNumber(BigDecimal.ONE);
|
|
|
|
/**
|
|
|
|
* The number zero.
|
|
|
|
*/
|
|
|
|
static final PreciseNumber ZERO = new PreciseNumber(BigDecimal.ZERO);
|
|
|
|
/**
|
|
|
|
* The number ten.
|
|
|
|
*/
|
|
|
|
static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
|
|
|
|
|
2017-07-28 14:55:01 -07:00
|
|
|
/**
|
|
|
|
* The value of the PreciseNumber.
|
|
|
|
*/
|
|
|
|
BigDecimal value;
|
2017-07-28 11:38:22 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a precise number from the given string.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-28 11:38:22 -07:00
|
|
|
* @param string a string representation of the number meeting the same conditions
|
|
|
|
* as the BidDecimal(String) constructor.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public PreciseNumber(String string) {
|
2017-07-28 11:38:22 -07:00
|
|
|
value = new BigDecimal(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a precise number from the given BigDecimal.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-28 11:38:22 -07:00
|
|
|
* @param value a BigDecimal object representing the value of the number.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public PreciseNumber(BigDecimal value) {
|
2017-07-28 11:38:22 -07:00
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-28 20:04:13 -07:00
|
|
|
public int getMaxPrecision() {
|
2017-07-28 11:38:22 -07:00
|
|
|
return 54;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberInterface multiply(NumberInterface multiplier) {
|
2017-07-31 13:25:23 -07:00
|
|
|
return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value));
|
2017-07-28 11:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberInterface divide(NumberInterface divisor) {
|
2017-07-28 20:04:13 -07:00
|
|
|
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.getMaxPrecision(), RoundingMode.HALF_UP));
|
2017-07-28 11:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberInterface add(NumberInterface summand) {
|
|
|
|
return new PreciseNumber(value.add(((PreciseNumber) summand).value));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberInterface subtract(NumberInterface subtrahend) {
|
|
|
|
return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberInterface intPow(int exponent) {
|
2017-07-30 21:11:32 -07:00
|
|
|
if (exponent == 0) {
|
2017-07-28 11:38:22 -07:00
|
|
|
return PreciseNumber.ONE;
|
|
|
|
}
|
|
|
|
boolean takeReciprocal = exponent < 0;
|
|
|
|
exponent = Math.abs(exponent);
|
|
|
|
NumberInterface power = this;
|
2017-07-30 21:11:32 -07:00
|
|
|
for (int currentExponent = 1; currentExponent < exponent; currentExponent++) {
|
2017-07-28 11:38:22 -07:00
|
|
|
power = power.multiply(this);
|
|
|
|
}
|
2017-07-30 21:11:32 -07:00
|
|
|
if (takeReciprocal) {
|
2017-07-28 11:38:22 -07:00
|
|
|
power = PreciseNumber.ONE.divide(power);
|
|
|
|
}
|
|
|
|
return power;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compareTo(NumberInterface number) {
|
|
|
|
return value.compareTo(((PreciseNumber) number).value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int signum() {
|
|
|
|
return value.signum();
|
|
|
|
}
|
|
|
|
|
2017-07-31 13:25:23 -07:00
|
|
|
@Override
|
|
|
|
public int ceiling() {
|
|
|
|
return (int) Math.ceil(value.doubleValue());
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:38:22 -07:00
|
|
|
@Override
|
2017-07-30 21:11:32 -07:00
|
|
|
public NumberInterface negate() {
|
2017-07-28 11:38:22 -07:00
|
|
|
return new PreciseNumber(value.negate());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
2017-07-30 21:11:32 -07:00
|
|
|
if (toClass == this.getClass()) {
|
2017-07-28 11:38:22 -07:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2017-07-28 20:04:13 -07:00
|
|
|
BigDecimal rounded = value.setScale(getMaxPrecision() - 4, RoundingMode.HALF_UP);
|
2017-07-28 11:38:22 -07:00
|
|
|
return rounded.stripTrailingZeros().toPlainString();
|
|
|
|
}
|
|
|
|
}
|