1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 16:00:09 -08:00

Separate power and factorial calculations to fix large precision loss in exp.

This commit is contained in:
Arthur Drobot 2017-07-31 22:56:55 -07:00
parent 87f98228d0
commit f68f184945

View File

@ -171,17 +171,19 @@ public class StandardPlugin extends Plugin {
//We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!. //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. //right and left refer to lhs and rhs in the above inequality.
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass()); NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
NumberInterface nextTerm = params[0]; NumberInterface nextNumerator = params[0];
NumberInterface left = params[0].multiply(new NaiveNumber(3).promoteTo(params[0].getClass()).intPow(params[0].ceiling())), right = maxError; NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling())), right = maxError;
do{ do{
sum = sum.add(nextTerm); sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1)));
n++; n++;
NumberInterface nextN = new NaiveNumber(n+1).promoteTo(params[0].getClass()); nextNumerator = nextNumerator.multiply(params[0]);
nextTerm = nextTerm.multiply(params[0]).divide(nextN);
left = left.multiply(params[0]); left = left.multiply(params[0]);
NumberInterface nextN = (new NaiveNumber(n+1)).promoteTo(params[0].getClass());
right = right.multiply(nextN); right = right.multiply(nextN);
//System.out.println(left + ", " + right);
} }
while(left.compareTo(right) > 0); while(left.compareTo(right) > 0);
System.out.println(n+1);
return sum; return sum;
} }
} }