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-08-05 13:54:06 -07:00
|
|
|
public class NaiveNumber extends 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-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.
|
|
|
|
*/
|
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-08-05 13:54:06 -07:00
|
|
|
public NumberInterface multiplyInternal(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-08-05 13:54:06 -07:00
|
|
|
public NumberInterface divideInternal(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-08-05 13:54:06 -07:00
|
|
|
public NumberInterface addInternal(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-08-05 13:54:06 -07:00
|
|
|
public NumberInterface subtractInternal(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-08-05 13:54:06 -07:00
|
|
|
public NumberInterface negateInternal() {
|
2017-07-24 11:13:18 -07:00
|
|
|
return new NaiveNumber(-value);
|
|
|
|
}
|
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-05 13:54:06 -07:00
|
|
|
public NumberInterface ceilingInternal() {
|
2017-08-02 12:00:56 -07:00
|
|
|
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-05 13:54:06 -07:00
|
|
|
public NumberInterface floorInternal() {
|
2017-08-02 12:00:56 -07:00
|
|
|
return new NaiveNumber(Math.floor(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-05 13:54:06 -07:00
|
|
|
public NumberInterface fractionalPartInternal() {
|
2017-08-02 12:00:56 -07:00
|
|
|
return new NaiveNumber(value - Math.floor(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int intValue() {
|
2017-08-04 13:20:57 -07:00
|
|
|
return (int) value;
|
2017-08-01 15:36:54 -07:00
|
|
|
}
|
|
|
|
|
2017-07-24 14:37:56 -07:00
|
|
|
@Override
|
2017-08-05 13:54:06 -07:00
|
|
|
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) {
|
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
|
|
|
|