1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-16 07:33:09 -08:00
Abacus/src/org/nwapw/abacus/number/NaiveNumber.java

76 lines
1.8 KiB
Java
Raw Normal View History

package org.nwapw.abacus.number;
public class NaiveNumber implements NumberInterface {
private double value;
public NaiveNumber(double value) {
this.value = value;
}
public static final NaiveNumber ZERO = new NaiveNumber(0);
public static final NaiveNumber ONE = new NaiveNumber(1);
2017-07-24 13:43:51 -07:00
@Override
public int precision() {
return 4;
}
@Override
public NumberInterface multiply(NumberInterface multiplier) {
return new NaiveNumber(value * ((NaiveNumber)multiplier).value);
}
@Override
public NumberInterface divide(NumberInterface divisor) {
2017-07-24 13:44:09 -07:00
return new NaiveNumber(value / ((NaiveNumber)divisor).value);
}
@Override
public NumberInterface add(NumberInterface summand) {
2017-07-24 13:44:09 -07:00
return new NaiveNumber(value + ((NaiveNumber)summand).value);
}
@Override
public NumberInterface subtract(NumberInterface subtrahend) {
2017-07-24 13:44:09 -07:00
return new NaiveNumber(value - ((NaiveNumber)subtrahend).value);
}
@Override
public NumberInterface negate() {
return new NaiveNumber(-value);
}
@Override
public NumberInterface intPow(int exponent) {
NumberInterface power = this;
2017-07-25 14:18:00 -07:00
for(int currentExponent = 1; currentExponent < exponent; currentExponent++){
power = power.multiply(this);
}
return power;
}
@Override
public int compareTo(NumberInterface number) {
NaiveNumber num = (NaiveNumber) number;
return Double.compare(value, num.value);
}
@Override
public int signum() {
return this.compareTo(ZERO);
}
2017-07-24 14:37:56 -07:00
@Override
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
2017-07-24 14:37:56 -07:00
if(toClass == this.getClass()) return this;
return null;
}
public String toString(){
return Double.toString(value);
}
}