2017-07-28 11:38:22 -07:00
|
|
|
package org.nwapw.abacus.number;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.math.RoundingMode;
|
|
|
|
|
2017-08-01 10:49:50 -07:00
|
|
|
/**
|
|
|
|
* A number that uses a BigDecimal to store its value,
|
|
|
|
* leading to infinite possible precision.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public class PreciseNumber implements NumberInterface {
|
2017-07-28 11:38:22 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number one.
|
|
|
|
*/
|
2017-08-04 09:55:24 -07:00
|
|
|
public static final PreciseNumber ONE = new PreciseNumber(BigDecimal.ONE);
|
2017-07-28 11:38:22 -07:00
|
|
|
/**
|
|
|
|
* The number zero.
|
|
|
|
*/
|
2017-08-04 09:55:24 -07:00
|
|
|
public static final PreciseNumber ZERO = new PreciseNumber(BigDecimal.ZERO);
|
2017-07-28 11:38:22 -07:00
|
|
|
/**
|
|
|
|
* The number ten.
|
|
|
|
*/
|
2017-08-04 09:55:24 -07:00
|
|
|
public static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
|
2017-07-28 11:38:22 -07:00
|
|
|
|
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-31 23:16:37 -07:00
|
|
|
return 65;
|
2017-07-28 11:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
2017-08-02 12:00:56 -07:00
|
|
|
public NumberInterface ceiling() {
|
|
|
|
String str = value.toPlainString();
|
|
|
|
int decimalIndex = str.indexOf('.');
|
|
|
|
if(decimalIndex != -1){
|
|
|
|
return this.floor().add(ONE);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberInterface floor() {
|
|
|
|
String str = value.toPlainString();
|
|
|
|
int decimalIndex = str.indexOf('.');
|
|
|
|
if(decimalIndex != -1){
|
|
|
|
return new PreciseNumber(str.substring(0, decimalIndex));
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberInterface fractionalPart() {
|
|
|
|
String str = value.toPlainString();
|
|
|
|
int decimalIndex = str.indexOf('.');
|
|
|
|
if(decimalIndex != -1){
|
|
|
|
return new PreciseNumber(str.substring(decimalIndex + 1));
|
|
|
|
}
|
|
|
|
return ZERO;
|
2017-07-31 13:25:23 -07:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:36:54 -07:00
|
|
|
@Override
|
2017-08-02 12:00:56 -07:00
|
|
|
public int intValue() {
|
|
|
|
return value.intValue();
|
2017-07-31 13:25:23 -07:00
|
|
|
}
|
|
|
|
|
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-31 23:16:37 -07:00
|
|
|
BigDecimal rounded = value.setScale(getMaxPrecision() - 15, RoundingMode.HALF_UP);
|
2017-07-28 11:38:22 -07:00
|
|
|
return rounded.stripTrailingZeros().toPlainString();
|
|
|
|
}
|
|
|
|
}
|