Add absolute value function to standard plugin. Modify getNTermsExp to work on negative exponents instead (and correctly).

This commit is contained in:
Arthur Drobot 2017-07-27 10:03:26 -07:00
parent 4a8164631f
commit aec37b6720
1 changed files with 17 additions and 3 deletions

View File

@ -75,6 +75,7 @@ public class StandardPlugin extends Plugin {
}); });
registerFunction("!", new Function() { registerFunction("!", new Function() {
//private ArrayLi
@Override @Override
protected boolean matchesParams(NumberInterface[] params) { protected boolean matchesParams(NumberInterface[] params) {
return params.length == 1; 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() { registerFunction("exp", new Function() {
@Override @Override
protected boolean matchesParams(NumberInterface[] params) { protected boolean matchesParams(NumberInterface[] params) {
@ -126,14 +139,15 @@ public class StandardPlugin extends Plugin {
* @return * @return
*/ */
private int getNTermsExp(NumberInterface maxError, NumberInterface x){ 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. //The variables LHS and RHS refer to the above inequality.
int n = 0; 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){ while(LHS.compareTo(RHS) > 0){
n++; n++;
LHS = LHS.multiply(x); 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; return n;
} }