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-25 22:47:48 -07:00
|
|
|
/**
|
|
|
|
* The value of this number.
|
|
|
|
*/
|
2017-07-24 11:13:18 -07:00
|
|
|
private double value;
|
|
|
|
|
2017-07-25 22:47:48 -07:00
|
|
|
/**
|
|
|
|
* Creates a new NaiveNumber with the given value.
|
|
|
|
* @param value the value to use.
|
|
|
|
*/
|
2017-07-24 11:13:18 -07:00
|
|
|
public NaiveNumber(double value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:47:48 -07:00
|
|
|
/**
|
|
|
|
* The number zero.
|
|
|
|
*/
|
2017-07-25 13:58:09 -07:00
|
|
|
public static final NaiveNumber ZERO = new NaiveNumber(0);
|
2017-07-25 22:47:48 -07:00
|
|
|
/**
|
|
|
|
* The number one.
|
|
|
|
*/
|
2017-07-25 13:58:09 -07:00
|
|
|
public static final NaiveNumber ONE = new NaiveNumber(1);
|
2017-07-25 11:09:23 -07:00
|
|
|
|
2017-07-24 13:43:51 -07:00
|
|
|
@Override
|
|
|
|
public int precision() {
|
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-24 11:13:18 -07:00
|
|
|
return new NaiveNumber(value * ((NaiveNumber)multiplier).value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-25 13:58:09 -07:00
|
|
|
public NumberInterface divide(NumberInterface divisor) {
|
2017-07-24 13:44:09 -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-24 13:44:09 -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-24 13:44:09 -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-25 14:36:46 -07:00
|
|
|
if(exponent == 0){
|
|
|
|
return NaiveNumber.ONE;
|
|
|
|
}
|
|
|
|
boolean takeReciprocal = exponent < 0;
|
|
|
|
exponent = Math.abs(exponent);
|
2017-07-25 13:58:09 -07:00
|
|
|
NumberInterface power = this;
|
2017-07-25 14:18:00 -07:00
|
|
|
for(int currentExponent = 1; currentExponent < exponent; currentExponent++){
|
2017-07-25 11:46:15 -07:00
|
|
|
power = power.multiply(this);
|
|
|
|
}
|
2017-07-25 14:36:46 -07:00
|
|
|
if(takeReciprocal){
|
|
|
|
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-24 14:37:56 -07:00
|
|
|
@Override
|
2017-07-25 13:58:09 -07:00
|
|
|
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
2017-07-24 14:37:56 -07:00
|
|
|
if(toClass == this.getClass()) return this;
|
2017-07-28 11:38:22 -07:00
|
|
|
else if(toClass == PreciseNumber.class){
|
|
|
|
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-25 13:58:09 -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
|
|
|
|