From aec37b6720df18d5e6dc6bba1a83064dd42735b1 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Thu, 27 Jul 2017 10:03:26 -0700 Subject: [PATCH] Add absolute value function to standard plugin. Modify getNTermsExp to work on negative exponents instead (and correctly). --- .../nwapw/abacus/plugin/StandardPlugin.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/org/nwapw/abacus/plugin/StandardPlugin.java b/src/org/nwapw/abacus/plugin/StandardPlugin.java index 9881ff2..ac4eb76 100755 --- a/src/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/org/nwapw/abacus/plugin/StandardPlugin.java @@ -75,6 +75,7 @@ public class StandardPlugin extends Plugin { }); registerFunction("!", new Function() { + //private ArrayLi @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 1; @@ -95,6 +96,18 @@ public class StandardPlugin extends Plugin { } }); + registerFunction("abs", new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + return params[0].multiply((new NaiveNumber(params[0].signum())).promoteTo(params[0].getClass())); + } + }); + registerFunction("exp", new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { @@ -126,14 +139,15 @@ public class StandardPlugin extends Plugin { * @return */ private int getNTermsExp(NumberInterface maxError, NumberInterface x){ - //We need n such that x^(n+2) <= (n+1)! * maxError + //We need n such that |x^(n+1)| <= (n+1)! * maxError //The variables LHS and RHS refer to the above inequality. int n = 0; - NumberInterface LHS = x.intPow(2), RHS = maxError; + x = this.getFunction("abs").apply(x); + NumberInterface LHS = x, RHS = maxError; while(LHS.compareTo(RHS) > 0){ n++; LHS = LHS.multiply(x); - RHS = RHS.multiply(new NaiveNumber(n).promoteTo(RHS.getClass())); + RHS = RHS.multiply(new NaiveNumber(n+1).promoteTo(RHS.getClass())); } return n; }