From afb57cf69b8f9b7203272c3d4ee33ef12de09727 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Mon, 31 Jul 2017 13:25:23 -0700 Subject: [PATCH] Add ceiling to NumberInterface and the two numbers that implement it. --- src/main/java/org/nwapw/abacus/number/NaiveNumber.java | 5 +++++ .../java/org/nwapw/abacus/number/NumberInterface.java | 6 ++++++ src/main/java/org/nwapw/abacus/number/PreciseNumber.java | 8 +++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/nwapw/abacus/number/NaiveNumber.java b/src/main/java/org/nwapw/abacus/number/NaiveNumber.java index 4b502b3..8c23b6b 100755 --- a/src/main/java/org/nwapw/abacus/number/NaiveNumber.java +++ b/src/main/java/org/nwapw/abacus/number/NaiveNumber.java @@ -93,6 +93,11 @@ public class NaiveNumber implements NumberInterface { return this.compareTo(ZERO); } + @Override + public int ceiling() { + return (int) Math.ceil(value); + } + @Override public NumberInterface promoteTo(Class toClass) { if (toClass == this.getClass()) return this; diff --git a/src/main/java/org/nwapw/abacus/number/NumberInterface.java b/src/main/java/org/nwapw/abacus/number/NumberInterface.java index 74b4d4c..34d1cfd 100755 --- a/src/main/java/org/nwapw/abacus/number/NumberInterface.java +++ b/src/main/java/org/nwapw/abacus/number/NumberInterface.java @@ -79,6 +79,12 @@ public interface NumberInterface { */ int signum(); + /** + * 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(); + /** * 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 172a1c4..ebf14d5 100755 --- a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java +++ b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java @@ -1,6 +1,7 @@ package org.nwapw.abacus.number; import java.math.BigDecimal; +import java.math.MathContext; import java.math.RoundingMode; public class PreciseNumber implements NumberInterface { @@ -49,7 +50,7 @@ public class PreciseNumber implements NumberInterface { @Override public NumberInterface multiply(NumberInterface multiplier) { - return new PreciseNumber(value.multiply(((PreciseNumber) multiplier).value)); + return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value)); } @Override @@ -94,6 +95,11 @@ public class PreciseNumber implements NumberInterface { return value.signum(); } + @Override + public int ceiling() { + return (int) Math.ceil(value.doubleValue()); + } + @Override public NumberInterface negate() { return new PreciseNumber(value.negate());