2017-07-24 11:13:18 -07:00
|
|
|
package org.nwapw.abacus.number;
|
|
|
|
|
2017-07-25 22:47:48 -07:00
|
|
|
/**
|
|
|
|
* An implementation of NumberInterface using a double.
|
|
|
|
*/
|
2017-07-25 13:58:09 -07:00
|
|
|
public class NaiveNumber implements NumberInterface {
|
2017-07-24 11:13:18 -07:00
|
|
|
|
2017-07-30 21:11:32 -07:00
|
|
|
/**
|
|
|
|
* The number zero.
|
|
|
|
*/
|
|
|
|
public static final NaiveNumber ZERO = new NaiveNumber(0);
|
|
|
|
/**
|
|
|
|
* The number one.
|
|
|
|
*/
|
|
|
|
public static final NaiveNumber ONE = new NaiveNumber(1);
|
2017-07-25 22:47:48 -07:00
|
|
|
/**
|
|
|
|
* The value of this number.
|
|
|
|
*/
|
2017-07-24 11:13:18 -07:00
|
|
|
private double value;
|
|
|
|
|
2017-07-28 20:04:13 -07:00
|
|
|
/**
|
|
|
|
* Creates a new NaiveNumber with the given string.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-28 20:04:13 -07:00
|
|
|
* @param value the value, which will be parsed as a double.
|
|
|
|
*/
|
|
|
|
public NaiveNumber(String value) {
|
|
|
|
this(Double.parseDouble(value));
|
|
|
|
}
|
2017-07-25 22:47:48 -07:00
|
|
|
/**
|
|
|
|
* Creates a new NaiveNumber with the given value.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-25 22:47:48 -07:00
|
|
|
* @param value the value to use.
|
|
|
|
*/
|
2017-07-24 11:13:18 -07:00
|
|
|
public NaiveNumber(double value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
2017-07-24 13:43:51 -07:00
|
|
|
@Override
|
2017-07-28 20:04:13 -07:00
|
|
|
public int getMaxPrecision() {
|
2017-07-27 10:32:09 -07:00
|
|
|
return 18;
|
2017-07-24 13:43:51 -07:00
|
|
|
}
|
|
|
|
|
2017-07-24 11:13:18 -07:00
|
|
|
@Override
|
2017-07-25 13:58:09 -07:00
|
|
|
public NumberInterface multiply(NumberInterface multiplier) {
|
2017-07-30 21:11:32 -07:00
|
|
|
return new NaiveNumber(value * ((NaiveNumber) multiplier).value);
|
2017-07-24 11:13:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-25 13:58:09 -07:00
|
|
|
public NumberInterface divide(NumberInterface divisor) {
|
2017-07-30 21:11:32 -07:00
|
|
|
return new NaiveNumber(value / ((NaiveNumber) divisor).value);
|
2017-07-24 11:13:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-25 13:58:09 -07:00
|
|
|
public NumberInterface add(NumberInterface summand) {
|
2017-07-30 21:11:32 -07:00
|
|
|
return new NaiveNumber(value + ((NaiveNumber) summand).value);
|
2017-07-24 11:13:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-25 13:58:09 -07:00
|
|
|
public NumberInterface subtract(NumberInterface subtrahend) {
|
2017-07-30 21:11:32 -07:00
|
|
|
return new NaiveNumber(value - ((NaiveNumber) subtrahend).value);
|
2017-07-24 11:13:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-25 13:58:09 -07:00
|
|
|
public NumberInterface negate() {
|
2017-07-24 11:13:18 -07:00
|
|
|
return new NaiveNumber(-value);
|
|
|
|
}
|
2017-07-24 13:57:14 -07:00
|
|
|
|
2017-07-25 11:46:15 -07:00
|
|
|
@Override
|
2017-07-25 13:58:09 -07:00
|
|
|
public NumberInterface intPow(int exponent) {
|
2017-07-30 21:11:32 -07:00
|
|
|
if (exponent == 0) {
|
2017-07-25 14:36:46 -07:00
|
|
|
return NaiveNumber.ONE;
|
|
|
|
}
|
|
|
|
boolean takeReciprocal = exponent < 0;
|
|
|
|
exponent = Math.abs(exponent);
|
2017-07-25 13:58:09 -07:00
|
|
|
NumberInterface power = this;
|
2017-07-30 21:11:32 -07:00
|
|
|
for (int currentExponent = 1; currentExponent < exponent; currentExponent++) {
|
2017-07-25 11:46:15 -07:00
|
|
|
power = power.multiply(this);
|
|
|
|
}
|
2017-07-30 21:11:32 -07:00
|
|
|
if (takeReciprocal) {
|
2017-07-25 14:36:46 -07:00
|
|
|
power = NaiveNumber.ONE.divide(power);
|
|
|
|
}
|
2017-07-25 11:46:15 -07:00
|
|
|
return power;
|
|
|
|
}
|
|
|
|
|
2017-07-24 13:57:14 -07:00
|
|
|
@Override
|
2017-07-25 13:58:09 -07:00
|
|
|
public int compareTo(NumberInterface number) {
|
2017-07-25 11:09:23 -07:00
|
|
|
NaiveNumber num = (NaiveNumber) number;
|
|
|
|
return Double.compare(value, num.value);
|
2017-07-24 13:57:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-25 11:09:23 -07:00
|
|
|
public int signum() {
|
|
|
|
return this.compareTo(ZERO);
|
2017-07-24 13:57:14 -07:00
|
|
|
}
|
|
|
|
|
2017-07-31 13:25:23 -07:00
|
|
|
@Override
|
2017-08-02 12:00:56 -07:00
|
|
|
public NumberInterface ceiling() {
|
|
|
|
return new NaiveNumber(Math.ceil(value));
|
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 NumberInterface floor() {
|
|
|
|
return new NaiveNumber(Math.floor(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberInterface fractionalPart() {
|
|
|
|
return new NaiveNumber(value - Math.floor(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int intValue() {
|
|
|
|
return (int)value;
|
2017-08-01 15:36:54 -07:00
|
|
|
}
|
|
|
|
|
2017-07-24 14:37:56 -07:00
|
|
|
@Override
|
2017-07-25 13:58:09 -07:00
|
|
|
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
2017-07-30 21:11:32 -07:00
|
|
|
if (toClass == this.getClass()) return this;
|
|
|
|
else if (toClass == PreciseNumber.class) {
|
2017-07-28 11:38:22 -07:00
|
|
|
return new PreciseNumber(Double.toString(value));
|
|
|
|
}
|
2017-07-24 14:37:56 -07:00
|
|
|
return null;
|
|
|
|
}
|
2017-07-25 11:09:23 -07:00
|
|
|
|
2017-07-30 21:11:32 -07:00
|
|
|
public String toString() {
|
2017-07-28 09:57:39 -07:00
|
|
|
double shiftBy = Math.pow(10, 10);
|
|
|
|
return Double.toString(Math.round(value * shiftBy) / shiftBy);
|
2017-07-25 13:58:09 -07:00
|
|
|
}
|
|
|
|
|
2017-07-24 11:13:18 -07:00
|
|
|
}
|
2017-07-24 14:25:59 -07:00
|
|
|
|