From 90f04a20add93e6b69640130a2b5e93175dd072e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 28 Jul 2017 20:04:13 -0700 Subject: [PATCH] Change precision to getMaxPrecision, as precision can be configured. --- src/org/nwapw/abacus/number/NaiveNumber.java | 9 ++++++++- src/org/nwapw/abacus/number/NumberInterface.java | 4 ++-- src/org/nwapw/abacus/number/PreciseNumber.java | 8 +++----- src/org/nwapw/abacus/plugin/StandardPlugin.java | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/org/nwapw/abacus/number/NaiveNumber.java b/src/org/nwapw/abacus/number/NaiveNumber.java index 1a80cd1..96e42dd 100755 --- a/src/org/nwapw/abacus/number/NaiveNumber.java +++ b/src/org/nwapw/abacus/number/NaiveNumber.java @@ -10,6 +10,13 @@ public class NaiveNumber implements NumberInterface { */ 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. * @param value the value to use. @@ -28,7 +35,7 @@ public class NaiveNumber implements NumberInterface { public static final NaiveNumber ONE = new NaiveNumber(1); @Override - public int precision() { + public int getMaxPrecision() { return 18; } diff --git a/src/org/nwapw/abacus/number/NumberInterface.java b/src/org/nwapw/abacus/number/NumberInterface.java index f425a30..1f87d11 100755 --- a/src/org/nwapw/abacus/number/NumberInterface.java +++ b/src/org/nwapw/abacus/number/NumberInterface.java @@ -6,10 +6,10 @@ package org.nwapw.abacus.number; public interface NumberInterface { /** - * The precision to which this number operates. + * The maximum precision to which this number operates. * @return the precision. */ - int precision(); + int getMaxPrecision(); /** * Multiplies this number by another, returning diff --git a/src/org/nwapw/abacus/number/PreciseNumber.java b/src/org/nwapw/abacus/number/PreciseNumber.java index 1641cff..8b01622 100755 --- a/src/org/nwapw/abacus/number/PreciseNumber.java +++ b/src/org/nwapw/abacus/number/PreciseNumber.java @@ -1,8 +1,6 @@ package org.nwapw.abacus.number; import java.math.BigDecimal; -import java.math.BigInteger; -import java.math.MathContext; import java.math.RoundingMode; public class PreciseNumber implements NumberInterface{ @@ -43,7 +41,7 @@ public class PreciseNumber implements NumberInterface{ } @Override - public int precision() { + public int getMaxPrecision() { return 54; } @@ -54,7 +52,7 @@ public class PreciseNumber implements NumberInterface{ @Override 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 @@ -109,7 +107,7 @@ public class PreciseNumber implements NumberInterface{ @Override 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(); } } diff --git a/src/org/nwapw/abacus/plugin/StandardPlugin.java b/src/org/nwapw/abacus/plugin/StandardPlugin.java index 1ae0969..088c877 100755 --- a/src/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/org/nwapw/abacus/plugin/StandardPlugin.java @@ -292,7 +292,7 @@ public class StandardPlugin extends Plugin { * @return the maximum error. */ 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()); } }