2017-07-24 11:13:18 -07:00
|
|
|
package org.nwapw.abacus.number;
|
|
|
|
|
|
|
|
public class NaiveNumber implements Number {
|
|
|
|
|
|
|
|
private double value;
|
|
|
|
|
|
|
|
public NaiveNumber(double value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
2017-07-25 11:09:23 -07:00
|
|
|
static final NaiveNumber ZERO = new NaiveNumber(0);
|
|
|
|
static final NaiveNumber ONE = new NaiveNumber(1);
|
|
|
|
|
2017-07-24 13:43:51 -07:00
|
|
|
@Override
|
|
|
|
public int precision() {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2017-07-24 11:13:18 -07:00
|
|
|
@Override
|
|
|
|
public Number multiply(Number multiplier) {
|
|
|
|
return new NaiveNumber(value * ((NaiveNumber)multiplier).value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Number divide(Number 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
|
|
|
|
public Number add(Number 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
|
|
|
|
public Number subtract(Number 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
|
|
|
|
public Number negate() {
|
|
|
|
return new NaiveNumber(-value);
|
|
|
|
}
|
2017-07-24 13:57:14 -07:00
|
|
|
|
2017-07-25 11:46:15 -07:00
|
|
|
@Override
|
|
|
|
public Number intPow(int exponent) {
|
|
|
|
Number power = this;
|
|
|
|
for(int currentExponent = 1; currentExponent <= exponent; currentExponent++){
|
|
|
|
power = power.multiply(this);
|
|
|
|
}
|
|
|
|
return power;
|
|
|
|
}
|
|
|
|
|
2017-07-24 13:57:14 -07:00
|
|
|
@Override
|
2017-07-25 11:09:23 -07:00
|
|
|
public int compareTo(Number number) {
|
|
|
|
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
|
|
|
|
public Number promoteTo(Class<? extends Number> toClass) {
|
|
|
|
if(toClass == this.getClass()) return this;
|
|
|
|
return null;
|
|
|
|
}
|
2017-07-25 11:09:23 -07:00
|
|
|
|
2017-07-24 11:13:18 -07:00
|
|
|
}
|
2017-07-24 14:25:59 -07:00
|
|
|
|