From 44ad5b3a6c7e7ddfb5d74ec23c01ae63c84c0ac1 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Wed, 2 Aug 2017 12:00:56 -0700 Subject: [PATCH] Change ceiling and floor to return NumberInterface. Add fractional part function. Add intValue function. Change StandardPlugin correspondingly. --- .../org/nwapw/abacus/number/NaiveNumber.java | 18 +++++++--- .../nwapw/abacus/number/NumberInterface.java | 17 ++++++++-- .../nwapw/abacus/number/PreciseNumber.java | 33 ++++++++++++++++--- .../nwapw/abacus/plugin/StandardPlugin.java | 4 +-- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/number/NaiveNumber.java b/src/main/java/org/nwapw/abacus/number/NaiveNumber.java index 91b4435..b32e2fa 100755 --- a/src/main/java/org/nwapw/abacus/number/NaiveNumber.java +++ b/src/main/java/org/nwapw/abacus/number/NaiveNumber.java @@ -94,13 +94,23 @@ public class NaiveNumber implements NumberInterface { } @Override - public int ceiling() { - return (int) Math.ceil(value); + public NumberInterface ceiling() { + return new NaiveNumber(Math.ceil(value)); } @Override - public int floor() { - return (int) Math.floor(value); + public NumberInterface floor() { + return new NaiveNumber(Math.floor(value)); + } + + @Override + public NumberInterface fractionalPart() { + return new NaiveNumber(value - Math.floor(value)); + } + + @Override + public int intValue() { + return (int)value; } @Override diff --git a/src/main/java/org/nwapw/abacus/number/NumberInterface.java b/src/main/java/org/nwapw/abacus/number/NumberInterface.java index 1d16416..dad9274 100755 --- a/src/main/java/org/nwapw/abacus/number/NumberInterface.java +++ b/src/main/java/org/nwapw/abacus/number/NumberInterface.java @@ -83,13 +83,26 @@ public interface NumberInterface { * Returns the least integer greater than or equal to the number. * @return the least integer >= the number, if int can hold the value. */ - int ceiling(); + NumberInterface ceiling(); /** * Return the greatest integer less than or equal to the number. * @return the greatest int >= the number, if int can hold the value. */ - int floor(); + NumberInterface floor(); + + /** + * Returns the fractional part of the number. + * @return the fractional part of the number. + */ + NumberInterface fractionalPart(); + + /** + * Returns the integer representation of this number, discarding any fractional part, + * if int can hold the value. + * @return + */ + int intValue(); /** * Promotes this class to another number class. diff --git a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java index 19de42e..c648e7c 100755 --- a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java +++ b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java @@ -96,13 +96,38 @@ public class PreciseNumber implements NumberInterface { } @Override - public int ceiling() { - return (int) Math.ceil(value.doubleValue()); + public NumberInterface ceiling() { + String str = value.toPlainString(); + int decimalIndex = str.indexOf('.'); + if(decimalIndex != -1){ + return this.floor().add(ONE); + } + return this; } @Override - public int floor() { - return (int) Math.floor(value.doubleValue()); + public NumberInterface floor() { + String str = value.toPlainString(); + int decimalIndex = str.indexOf('.'); + if(decimalIndex != -1){ + return new PreciseNumber(str.substring(0, decimalIndex)); + } + return this; + } + + @Override + public NumberInterface fractionalPart() { + String str = value.toPlainString(); + int decimalIndex = str.indexOf('.'); + if(decimalIndex != -1){ + return new PreciseNumber(str.substring(decimalIndex + 1)); + } + return ZERO; + } + + @Override + public int intValue() { + return value.intValue(); } @Override diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index 9ce8c6d..0c79115 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -174,7 +174,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()).intPow(params[0].ceiling())), 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++; @@ -434,7 +434,7 @@ public class StandardPlugin extends Plugin { private static NumberInterface getSmallAngle(NumberInterface phi){ NumberInterface twoPi = getPi(phi.getClass()).multiply(new NaiveNumber("2").promoteTo(phi.getClass())); NumberInterface theta = FUNCTION_ABS.apply(phi).subtract(twoPi - .multiply(new NaiveNumber(FUNCTION_ABS.apply(phi).divide(twoPi).floor()).promoteTo(phi.getClass()))); //Now theta is in [0, 2pi). + .multiply(FUNCTION_ABS.apply(phi).divide(twoPi))); //Now theta is in [0, 2pi). if(phi.signum() < 0){ theta = twoPi.subtract(theta); }