From 4146e1a439d812d97055bb5967f05fb12210e871 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 5 Aug 2017 18:22:43 -0700 Subject: [PATCH] Revert "Remove the NumberInterface::intPow method." 0c16bb4e9be6d6b51dd87e9c6cc8d6040cdf679f --- .../org/nwapw/abacus/number/NaiveNumber.java | 17 +++++++++++++++ .../nwapw/abacus/number/NumberInterface.java | 21 +++++++++++++++++++ .../nwapw/abacus/number/PreciseNumber.java | 17 +++++++++++++++ .../nwapw/abacus/plugin/StandardPlugin.java | 6 ++---- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/number/NaiveNumber.java b/src/main/java/org/nwapw/abacus/number/NaiveNumber.java index 5326eb4..48beb1a 100755 --- a/src/main/java/org/nwapw/abacus/number/NaiveNumber.java +++ b/src/main/java/org/nwapw/abacus/number/NaiveNumber.java @@ -66,6 +66,23 @@ public class NaiveNumber extends NumberInterface { return new NaiveNumber(-value); } + @Override + public NumberInterface intPowInternal(int exponent) { + if (exponent == 0) { + return NaiveNumber.ONE; + } + boolean takeReciprocal = exponent < 0; + exponent = Math.abs(exponent); + NumberInterface power = this; + for (int currentExponent = 1; currentExponent < exponent; currentExponent++) { + power = power.multiply(this); + } + if (takeReciprocal) { + power = NaiveNumber.ONE.divide(power); + } + return power; + } + @Override public int compareTo(NumberInterface number) { NaiveNumber num = (NaiveNumber) number; diff --git a/src/main/java/org/nwapw/abacus/number/NumberInterface.java b/src/main/java/org/nwapw/abacus/number/NumberInterface.java index f5f1f72..7af5901 100755 --- a/src/main/java/org/nwapw/abacus/number/NumberInterface.java +++ b/src/main/java/org/nwapw/abacus/number/NumberInterface.java @@ -134,6 +134,27 @@ public abstract class NumberInterface { return negateInternal(); } + /** + * Raises this number to an integer power. + * + * @param exponent the exponent to which to take the number. + * @return the resulting value. + */ + protected abstract NumberInterface intPowInternal(int exponent); + + /** + * Raises this number to an integer power. Also, checks if the + * thread has been interrupted, and if so, throws + * an exception. + * + * @param exponent the exponent to which to take the number. + * @return the resulting value. + */ + public final NumberInterface intPow(int exponent){ + checkInterrupted(); + return intPowInternal(exponent); + } + /** * Compares this number to another. * diff --git a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java index 2b4a8e1..abc657f 100755 --- a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java +++ b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java @@ -71,6 +71,23 @@ public class PreciseNumber extends NumberInterface { return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value)); } + @Override + public NumberInterface intPowInternal(int exponent) { + if (exponent == 0) { + return PreciseNumber.ONE; + } + boolean takeReciprocal = exponent < 0; + exponent = Math.abs(exponent); + NumberInterface power = this; + for (int currentExponent = 1; currentExponent < exponent; currentExponent++) { + power = power.multiply(this); + } + if (takeReciprocal) { + power = PreciseNumber.ONE.divide(power); + } + return power; + } + @Override public int compareTo(NumberInterface number) { return value.compareTo(((PreciseNumber) number).value); diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index f926c20..c123caa 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -311,9 +311,7 @@ public class StandardPlugin extends Plugin { //right and left refer to lhs and rhs in the above inequality. NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass()); NumberInterface nextNumerator = params[0]; - NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass())); - left = intPow(left, left.getClass(), new NaiveNumber(params[0].ceiling().intValue()).promoteTo(left.getClass())); - NumberInterface right = maxError; + NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling().intValue())), right = maxError; do { sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n + 1))); n++; @@ -472,7 +470,7 @@ public class StandardPlugin extends Plugin { * @return the maximum error. */ private static NumberInterface getMaxError(NumberInterface number) { - return intPow(new NaiveNumber(10).promoteTo(number.getClass()), number.getClass(), new NaiveNumber(-number.getMaxPrecision()).promoteTo(number.getClass())); + return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision()); } /**