1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-26 16:45:21 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
Arthur Drobot
8a9df051cf Optimize pow for integer bases. 2017-08-07 13:43:12 -07:00
Arthur Drobot
4eda15b3fb Modify some functions in PreciseNumber for consistency. 2017-08-07 13:41:45 -07:00
Arthur Drobot
d0ccb8b625 Improve some comments of NumberInterface functions. 2017-08-07 10:54:27 -07:00
3 changed files with 17 additions and 11 deletions

View File

@@ -182,7 +182,7 @@ public abstract class NumberInterface {
* Also, checks if the thread has been interrupted, and if so, throws * Also, checks if the thread has been interrupted, and if so, throws
* an exception. * an exception.
* *
* @return the least integer bigger or equal to the number, if int can hold the value. * @return the least integer bigger or equal to the number.
*/ */
public final NumberInterface ceiling(){ public final NumberInterface ceiling(){
checkInterrupted(); checkInterrupted();
@@ -192,7 +192,7 @@ public abstract class NumberInterface {
/** /**
* Return the greatest integer less than or equal to the number. * Return the greatest integer less than or equal to the number.
* *
* @return the greatest integer smaller or equal the number, if int can hold the value. * @return the greatest integer smaller or equal the number.
*/ */
protected abstract NumberInterface floorInternal(); protected abstract NumberInterface floorInternal();
@@ -201,7 +201,7 @@ public abstract class NumberInterface {
* Also, checks if the thread has been interrupted, and if so, throws * Also, checks if the thread has been interrupted, and if so, throws
* an exception. * an exception.
* *
* @return the greatest int >= the number, if int can hold the value. * @return the greatest int greater than or equal to the number.
*/ */
public final NumberInterface floor(){ public final NumberInterface floor(){
checkInterrupted(); checkInterrupted();
@@ -216,7 +216,7 @@ public abstract class NumberInterface {
protected abstract NumberInterface fractionalPartInternal(); protected abstract NumberInterface fractionalPartInternal();
/** /**
* Returns the fractional part of the number. * Returns the fractional part of the number, specifically x - floor(x).
* Also, checks if the thread has been interrupted, * Also, checks if the thread has been interrupted,
* and if so, throws an exception. * and if so, throws an exception.
* @return the fractional part of the number. * @return the fractional part of the number.

View File

@@ -113,19 +113,18 @@ public class PreciseNumber extends NumberInterface {
String str = value.toPlainString(); String str = value.toPlainString();
int decimalIndex = str.indexOf('.'); int decimalIndex = str.indexOf('.');
if (decimalIndex != -1) { if (decimalIndex != -1) {
return new PreciseNumber(str.substring(0, decimalIndex)); NumberInterface floor = new PreciseNumber(str.substring(0, decimalIndex));
if(signum() == -1){
floor = floor.subtract(ONE);
}
return floor;
} }
return this; return this;
} }
@Override @Override
public NumberInterface fractionalPartInternal() { public NumberInterface fractionalPartInternal() {
String str = value.toPlainString(); return this.subtractInternal(floorInternal());
int decimalIndex = str.indexOf('.');
if (decimalIndex != -1) {
return new PreciseNumber(str.substring(decimalIndex + 1));
}
return ZERO;
} }
@Override @Override

View File

@@ -349,6 +349,13 @@ public class StandardPlugin extends Plugin {
return NaiveNumber.ZERO.promoteTo(params[0].getClass()); return NaiveNumber.ZERO.promoteTo(params[0].getClass());
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0) else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
return NaiveNumber.ONE.promoteTo(params[1].getClass()); return NaiveNumber.ONE.promoteTo(params[1].getClass());
//Detect integer bases:
if(params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
&& FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 0
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0){
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams));
}
return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1])); return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1]));
} }
}); });