From f68f18494503a2a41bbd3708806e58fda672cf03 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Mon, 31 Jul 2017 22:56:55 -0700 Subject: [PATCH] Separate power and factorial calculations to fix large precision loss in exp. --- .../java/org/nwapw/abacus/plugin/StandardPlugin.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index aeb4fb5..24b3d2a 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -171,17 +171,19 @@ public class StandardPlugin extends Plugin { //We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!. //right and left refer to lhs and rhs in the above inequality. NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass()); - NumberInterface nextTerm = params[0]; - NumberInterface left = params[0].multiply(new NaiveNumber(3).promoteTo(params[0].getClass()).intPow(params[0].ceiling())), right = maxError; + NumberInterface nextNumerator = params[0]; + NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling())), right = maxError; do{ - sum = sum.add(nextTerm); + sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1))); n++; - NumberInterface nextN = new NaiveNumber(n+1).promoteTo(params[0].getClass()); - nextTerm = nextTerm.multiply(params[0]).divide(nextN); + nextNumerator = nextNumerator.multiply(params[0]); left = left.multiply(params[0]); + NumberInterface nextN = (new NaiveNumber(n+1)).promoteTo(params[0].getClass()); right = right.multiply(nextN); + //System.out.println(left + ", " + right); } while(left.compareTo(right) > 0); + System.out.println(n+1); return sum; } }