1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-15 19:35:20 +00:00

Merge branch 'stoppable-alternate'

This commit is contained in:
2017-08-05 17:58:26 -07:00
11 changed files with 268 additions and 192 deletions

View File

@@ -0,0 +1,16 @@
package org.nwapw.abacus.number;
/**
* Exception thrown when the computation is interrupted by
* the user.
*/
public class ComputationInterruptedException extends RuntimeException {
/**
* Creates a new exception of this type.
*/
public ComputationInterruptedException(){
super("Computation interrupted by user.");
}
}

View File

@@ -3,7 +3,7 @@ package org.nwapw.abacus.number;
/**
* An implementation of NumberInterface using a double.
*/
public class NaiveNumber implements NumberInterface {
public class NaiveNumber extends NumberInterface {
/**
* The number zero.
@@ -42,32 +42,32 @@ public class NaiveNumber implements NumberInterface {
}
@Override
public NumberInterface multiply(NumberInterface multiplier) {
public NumberInterface multiplyInternal(NumberInterface multiplier) {
return new NaiveNumber(value * ((NaiveNumber) multiplier).value);
}
@Override
public NumberInterface divide(NumberInterface divisor) {
public NumberInterface divideInternal(NumberInterface divisor) {
return new NaiveNumber(value / ((NaiveNumber) divisor).value);
}
@Override
public NumberInterface add(NumberInterface summand) {
public NumberInterface addInternal(NumberInterface summand) {
return new NaiveNumber(value + ((NaiveNumber) summand).value);
}
@Override
public NumberInterface subtract(NumberInterface subtrahend) {
public NumberInterface subtractInternal(NumberInterface subtrahend) {
return new NaiveNumber(value - ((NaiveNumber) subtrahend).value);
}
@Override
public NumberInterface negate() {
public NumberInterface negateInternal() {
return new NaiveNumber(-value);
}
@Override
public NumberInterface intPow(int exponent) {
public NumberInterface intPowInternal(int exponent) {
if (exponent == 0) {
return NaiveNumber.ONE;
}
@@ -95,17 +95,17 @@ public class NaiveNumber implements NumberInterface {
}
@Override
public NumberInterface ceiling() {
public NumberInterface ceilingInternal() {
return new NaiveNumber(Math.ceil(value));
}
@Override
public NumberInterface floor() {
public NumberInterface floorInternal() {
return new NaiveNumber(Math.floor(value));
}
@Override
public NumberInterface fractionalPart() {
public NumberInterface fractionalPartInternal() {
return new NaiveNumber(value - Math.floor(value));
}
@@ -115,7 +115,7 @@ public class NaiveNumber implements NumberInterface {
}
@Override
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
public NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
if (toClass == this.getClass()) return this;
else if (toClass == PreciseNumber.class) {
return new PreciseNumber(Double.toString(value));

View File

@@ -3,14 +3,22 @@ package org.nwapw.abacus.number;
/**
* An interface used to represent a number.
*/
public interface NumberInterface {
public abstract class NumberInterface {
/**
* Check if the thread was interrupted and
* throw an exception to end the computation.
*/
private static void checkInterrupted(){
if(Thread.currentThread().isInterrupted())
throw new ComputationInterruptedException();
}
/**
* The maximum precision to which this number operates.
*
* @return the precision.
*/
int getMaxPrecision();
public abstract int getMaxPrecision();
/**
* Multiplies this number by another, returning
@@ -19,7 +27,21 @@ public interface NumberInterface {
* @param multiplier the multiplier
* @return the result of the multiplication.
*/
NumberInterface multiply(NumberInterface multiplier);
protected abstract NumberInterface multiplyInternal(NumberInterface multiplier);
/**
* Multiplies this number by another, returning
* a new number instance. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param multiplier the multiplier
* @return the result of the multiplication.
*/
public final NumberInterface multiply(NumberInterface multiplier){
checkInterrupted();
return multiplyInternal(multiplier);
}
/**
* Divides this number by another, returning
@@ -28,7 +50,21 @@ public interface NumberInterface {
* @param divisor the divisor
* @return the result of the division.
*/
NumberInterface divide(NumberInterface divisor);
protected abstract NumberInterface divideInternal(NumberInterface divisor);
/**
* Divides this number by another, returning
* a new number instance. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param divisor the divisor
* @return the result of the division.
*/
public final NumberInterface divide(NumberInterface divisor){
checkInterrupted();
return divideInternal(divisor);
}
/**
* Adds this number to another, returning
@@ -37,7 +73,21 @@ public interface NumberInterface {
* @param summand the summand
* @return the result of the summation.
*/
NumberInterface add(NumberInterface summand);
protected abstract NumberInterface addInternal(NumberInterface summand);
/**
* Adds this number to another, returning
* a new number instance. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param summand the summand
* @return the result of the summation.
*/
public final NumberInterface add(NumberInterface summand){
checkInterrupted();
return addInternal(summand);
}
/**
* Subtracts another number from this number,
@@ -46,7 +96,21 @@ public interface NumberInterface {
* @param subtrahend the subtrahend.
* @return the result of the subtraction.
*/
NumberInterface subtract(NumberInterface subtrahend);
protected abstract NumberInterface subtractInternal(NumberInterface subtrahend);
/**
* Subtracts another number from this number,
* a new number instance. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param subtrahend the subtrahend.
* @return the result of the subtraction.
*/
public final NumberInterface subtract(NumberInterface subtrahend){
checkInterrupted();
return subtractInternal(subtrahend);
}
/**
* Returns a new instance of this number with
@@ -54,7 +118,21 @@ public interface NumberInterface {
*
* @return the new instance.
*/
NumberInterface negate();
protected abstract NumberInterface negateInternal();
/**
* Returns a new instance of this number with
* the sign flipped. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @return the new instance.
*/
public final NumberInterface negate(){
checkInterrupted();
return negateInternal();
}
/**
* Raises this number to an integer power.
@@ -62,7 +140,20 @@ public interface NumberInterface {
* @param exponent the exponent to which to take the number.
* @return the resulting value.
*/
NumberInterface intPow(int exponent);
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.
@@ -70,35 +161,70 @@ public interface NumberInterface {
* @param number the number to compare to.
* @return same as Integer.compare();
*/
int compareTo(NumberInterface number);
public abstract int compareTo(NumberInterface number);
/**
* Same as Math.signum().
*
* @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0.
*/
int signum();
public abstract 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.
*/
protected abstract NumberInterface ceilingInternal();
/**
* Returns the least integer greater than or equal to the number.
* Also, checks if the thread has been interrupted, and if so, throws
* an exception.
*
* @return the least integer bigger or equal to the number, if int can hold the value.
*/
NumberInterface ceiling();
public final NumberInterface ceiling(){
checkInterrupted();
return ceilingInternal();
}
/**
* 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.
*/
NumberInterface floor();
protected abstract NumberInterface floorInternal();
/**
* Return the greatest integer less than or equal to the number.
* Also, checks if the thread has been interrupted, and if so, throws
* an exception.
*
* @return the greatest int >= the number, if int can hold the value.
*/
public final NumberInterface floor(){
checkInterrupted();
return floorInternal();
}
/**
* Returns the fractional part of the number.
*
* @return the fractional part of the number.
*/
NumberInterface fractionalPart();
protected abstract NumberInterface fractionalPartInternal();
/**
* Returns the fractional part of the number.
* Also, checks if the thread has been interrupted,
* and if so, throws an exception.
* @return the fractional part of the number.
*/
public final NumberInterface fractionalPart(){
checkInterrupted();
return fractionalPartInternal();
}
/**
* Returns the integer representation of this number, discarding any fractional part,
@@ -106,7 +232,7 @@ public interface NumberInterface {
*
* @return the integer value of this number.
*/
int intValue();
public abstract int intValue();
/**
* Promotes this class to another number class.
@@ -114,6 +240,19 @@ public interface NumberInterface {
* @param toClass the class to promote to.
* @return the resulting new instance.
*/
NumberInterface promoteTo(Class<? extends NumberInterface> toClass);
protected abstract NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass);
/**
* Promotes this class to another number class. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param toClass the class to promote to.
* @return the resulting new instance.
*/
public final NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
checkInterrupted();
return promoteToInternal(toClass);
}
}

View File

@@ -7,7 +7,7 @@ import java.math.RoundingMode;
* A number that uses a BigDecimal to store its value,
* leading to infinite possible precision.
*/
public class PreciseNumber implements NumberInterface {
public class PreciseNumber extends NumberInterface {
/**
* The number one.
@@ -52,27 +52,27 @@ public class PreciseNumber implements NumberInterface {
}
@Override
public NumberInterface multiply(NumberInterface multiplier) {
public NumberInterface multiplyInternal(NumberInterface multiplier) {
return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value));
}
@Override
public NumberInterface divide(NumberInterface divisor) {
public NumberInterface divideInternal(NumberInterface divisor) {
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.getMaxPrecision(), RoundingMode.HALF_UP));
}
@Override
public NumberInterface add(NumberInterface summand) {
public NumberInterface addInternal(NumberInterface summand) {
return new PreciseNumber(value.add(((PreciseNumber) summand).value));
}
@Override
public NumberInterface subtract(NumberInterface subtrahend) {
public NumberInterface subtractInternal(NumberInterface subtrahend) {
return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value));
}
@Override
public NumberInterface intPow(int exponent) {
public NumberInterface intPowInternal(int exponent) {
if (exponent == 0) {
return PreciseNumber.ONE;
}
@@ -99,7 +99,7 @@ public class PreciseNumber implements NumberInterface {
}
@Override
public NumberInterface ceiling() {
public NumberInterface ceilingInternal() {
String str = value.toPlainString();
int decimalIndex = str.indexOf('.');
if (decimalIndex != -1) {
@@ -109,7 +109,7 @@ public class PreciseNumber implements NumberInterface {
}
@Override
public NumberInterface floor() {
public NumberInterface floorInternal() {
String str = value.toPlainString();
int decimalIndex = str.indexOf('.');
if (decimalIndex != -1) {
@@ -119,7 +119,7 @@ public class PreciseNumber implements NumberInterface {
}
@Override
public NumberInterface fractionalPart() {
public NumberInterface fractionalPartInternal() {
String str = value.toPlainString();
int decimalIndex = str.indexOf('.');
if (decimalIndex != -1) {
@@ -134,12 +134,12 @@ public class PreciseNumber implements NumberInterface {
}
@Override
public NumberInterface negate() {
public NumberInterface negateInternal() {
return new PreciseNumber(value.negate());
}
@Override
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
public NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
if (toClass == this.getClass()) {
return this;
}