mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-23 16:00:09 -08:00
Change precision to getMaxPrecision, as precision can be configured.
This commit is contained in:
parent
a0bc85a899
commit
90f04a20ad
|
@ -10,6 +10,13 @@ public class NaiveNumber implements NumberInterface {
|
||||||
*/
|
*/
|
||||||
private double value;
|
private double value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new NaiveNumber with the given string.
|
||||||
|
* @param value the value, which will be parsed as a double.
|
||||||
|
*/
|
||||||
|
public NaiveNumber(String value) {
|
||||||
|
this(Double.parseDouble(value));
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Creates a new NaiveNumber with the given value.
|
* Creates a new NaiveNumber with the given value.
|
||||||
* @param value the value to use.
|
* @param value the value to use.
|
||||||
|
@ -28,7 +35,7 @@ public class NaiveNumber implements NumberInterface {
|
||||||
public static final NaiveNumber ONE = new NaiveNumber(1);
|
public static final NaiveNumber ONE = new NaiveNumber(1);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int precision() {
|
public int getMaxPrecision() {
|
||||||
return 18;
|
return 18;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,10 @@ package org.nwapw.abacus.number;
|
||||||
public interface NumberInterface {
|
public interface NumberInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The precision to which this number operates.
|
* The maximum precision to which this number operates.
|
||||||
* @return the precision.
|
* @return the precision.
|
||||||
*/
|
*/
|
||||||
int precision();
|
int getMaxPrecision();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Multiplies this number by another, returning
|
* Multiplies this number by another, returning
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package org.nwapw.abacus.number;
|
package org.nwapw.abacus.number;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.math.MathContext;
|
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
public class PreciseNumber implements NumberInterface{
|
public class PreciseNumber implements NumberInterface{
|
||||||
|
@ -43,7 +41,7 @@ public class PreciseNumber implements NumberInterface{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int precision() {
|
public int getMaxPrecision() {
|
||||||
return 54;
|
return 54;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +52,7 @@ public class PreciseNumber implements NumberInterface{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface divide(NumberInterface divisor) {
|
public NumberInterface divide(NumberInterface divisor) {
|
||||||
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.precision(), RoundingMode.HALF_UP));
|
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.getMaxPrecision(), RoundingMode.HALF_UP));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -109,7 +107,7 @@ public class PreciseNumber implements NumberInterface{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
BigDecimal rounded = value.setScale(precision() - 4, RoundingMode.HALF_UP);
|
BigDecimal rounded = value.setScale(getMaxPrecision() - 4, RoundingMode.HALF_UP);
|
||||||
return rounded.stripTrailingZeros().toPlainString();
|
return rounded.stripTrailingZeros().toPlainString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -292,7 +292,7 @@ public class StandardPlugin extends Plugin {
|
||||||
* @return the maximum error.
|
* @return the maximum error.
|
||||||
*/
|
*/
|
||||||
private NumberInterface getMaxError(NumberInterface number){
|
private NumberInterface getMaxError(NumberInterface number){
|
||||||
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.precision());
|
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user