1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-04 09:58:30 -08:00
Abacus/src/main/java/org/nwapw/abacus/number/PreciseNumber.java

127 lines
3.4 KiB
Java
Raw Normal View History

package org.nwapw.abacus.number;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
2017-07-30 21:11:32 -07:00
public class PreciseNumber implements NumberInterface {
/**
* 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);
/**
* The value of the PreciseNumber.
*/
BigDecimal value;
/**
* Constructs a precise number from the given string.
2017-07-30 21:11:32 -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) {
value = new BigDecimal(string);
}
/**
* Constructs a precise number from the given BigDecimal.
2017-07-30 21:11:32 -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) {
this.value = value;
}
@Override
public int getMaxPrecision() {
2017-07-31 23:16:37 -07:00
return 65;
}
@Override
public NumberInterface multiply(NumberInterface multiplier) {
return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value));
}
@Override
public NumberInterface divide(NumberInterface divisor) {
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.getMaxPrecision(), RoundingMode.HALF_UP));
}
@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) {
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++) {
power = power.multiply(this);
}
2017-07-30 21:11:32 -07:00
if (takeReciprocal) {
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();
}
@Override
public int ceiling() {
return (int) Math.ceil(value.doubleValue());
}
@Override
public int floor() {
return (int) Math.floor(value.doubleValue());
}
@Override
2017-07-30 21:11:32 -07:00
public NumberInterface negate() {
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()) {
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);
return rounded.stripTrailingZeros().toPlainString();
}
}