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

116 lines
2.8 KiB
Java
Raw Normal View History

package org.nwapw.abacus.number;
2017-07-25 22:47:48 -07:00
/**
* An implementation of NumberInterface using a double.
*/
public class NaiveNumber extends NumberInterface {
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.
*/
private double value;
/**
* Creates a new NaiveNumber with the given string.
2017-07-30 21:11:32 -07:00
*
* @param value the value, which will be parsed as a double.
*/
public NaiveNumber(String value) {
this(Double.parseDouble(value));
}
2017-08-04 13:20:57 -07:00
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.
*/
public NaiveNumber(double value) {
this.value = value;
}
2017-07-24 13:43:51 -07:00
@Override
public int getMaxPrecision() {
return 18;
2017-07-24 13:43:51 -07:00
}
@Override
public NumberInterface multiplyInternal(NumberInterface multiplier) {
2017-07-30 21:11:32 -07:00
return new NaiveNumber(value * ((NaiveNumber) multiplier).value);
}
@Override
public NumberInterface divideInternal(NumberInterface divisor) {
2017-07-30 21:11:32 -07:00
return new NaiveNumber(value / ((NaiveNumber) divisor).value);
}
@Override
public NumberInterface addInternal(NumberInterface summand) {
2017-07-30 21:11:32 -07:00
return new NaiveNumber(value + ((NaiveNumber) summand).value);
}
@Override
public NumberInterface subtractInternal(NumberInterface subtrahend) {
2017-07-30 21:11:32 -07:00
return new NaiveNumber(value - ((NaiveNumber) subtrahend).value);
}
@Override
public NumberInterface negateInternal() {
return new NaiveNumber(-value);
}
@Override
public int compareTo(NumberInterface number) {
NaiveNumber num = (NaiveNumber) number;
return Double.compare(value, num.value);
}
@Override
public int signum() {
return this.compareTo(ZERO);
}
@Override
public NumberInterface ceilingInternal() {
return new NaiveNumber(Math.ceil(value));
}
@Override
public NumberInterface floorInternal() {
return new NaiveNumber(Math.floor(value));
}
@Override
public NumberInterface fractionalPartInternal() {
return new NaiveNumber(value - Math.floor(value));
}
@Override
public int intValue() {
2017-08-04 13:20:57 -07:00
return (int) value;
}
2017-07-24 14:37:56 -07:00
@Override
public NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
2017-07-30 21:11:32 -07:00
if (toClass == this.getClass()) return this;
else if (toClass == PreciseNumber.class) {
return new PreciseNumber(Double.toString(value));
}
2017-07-24 14:37:56 -07:00
return null;
}
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);
}
}