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

Revert "Remove the NumberInterface::intPow method."

0c16bb4e9b
This commit is contained in:
Danila Fedorin 2017-08-05 18:22:43 -07:00
parent 0c16bb4e9b
commit 4146e1a439
4 changed files with 57 additions and 4 deletions

View File

@ -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;

View File

@ -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.
*

View File

@ -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);

View File

@ -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());
}
/**