mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-23 07:50:09 -08:00
Rename Number to NumberInterface. Fix factorial function. Add toString override to NaiveNumber.
This commit is contained in:
parent
55c7f939d8
commit
4962aae083
10
src/org/nwapw/abacus/number/Function.java
Normal file → Executable file
10
src/org/nwapw/abacus/number/Function.java
Normal file → Executable file
|
@ -4,15 +4,15 @@ import java.util.HashMap;
|
|||
|
||||
public abstract class Function {
|
||||
|
||||
private static final HashMap<Class<? extends Number>, Integer> priorityMap =
|
||||
new HashMap<Class<? extends Number>, Integer>() {{
|
||||
private static final HashMap<Class<? extends NumberInterface>, Integer> priorityMap =
|
||||
new HashMap<Class<? extends NumberInterface>, Integer>() {{
|
||||
put(NaiveNumber.class, 0);
|
||||
}};
|
||||
|
||||
protected abstract boolean matchesParams(Number[] params);
|
||||
protected abstract Number applyInternal(Number[] params);
|
||||
protected abstract boolean matchesParams(NumberInterface[] params);
|
||||
protected abstract NumberInterface applyInternal(NumberInterface[] params);
|
||||
|
||||
public Number apply(Number...params) {
|
||||
public NumberInterface apply(NumberInterface...params) {
|
||||
if(!matchesParams(params)) return null;
|
||||
return applyInternal(params);
|
||||
}
|
||||
|
|
|
@ -9,13 +9,13 @@ public class FunctionDatabase {
|
|||
private void registerDefault(){
|
||||
functions.put("+", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(Number[] params) {
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length >= 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Number applyInternal(Number[] params) {
|
||||
Number sum = params[0];
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface sum = params[0];
|
||||
for(int i = 1; i < params.length; i++){
|
||||
sum = sum.add(params[i]);
|
||||
}
|
||||
|
@ -25,25 +25,25 @@ public class FunctionDatabase {
|
|||
|
||||
functions.put("-", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(Number[] params) {
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Number applyInternal(Number[] params) {
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
return params[0].subtract(params[1]);
|
||||
}
|
||||
});
|
||||
|
||||
functions.put("*", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(Number[] params) {
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length >= 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Number applyInternal(Number[] params) {
|
||||
Number product = params[1];
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface product = params[1];
|
||||
for(int i = 1; i < params.length; i++){
|
||||
product = product.multiply(params[i]);
|
||||
}
|
||||
|
@ -53,29 +53,28 @@ public class FunctionDatabase {
|
|||
|
||||
functions.put("/", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(Number[] params) {
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Number applyInternal(Number[] params) {
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
return params[0].divide(params[1]);
|
||||
}
|
||||
});
|
||||
|
||||
functions.put("!", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(Number[] params) {
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Number applyInternal(Number[] params) {
|
||||
Number factorial = params[0];
|
||||
Number multiplier = params[0];
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface factorial = params[0];
|
||||
NumberInterface multiplier = params[0];
|
||||
//It is necessary to later prevent calls of factorial on anything but non-negative integers.
|
||||
while(multiplier.signum() == 1){
|
||||
multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()));
|
||||
while((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1){
|
||||
factorial = factorial.multiply(multiplier);
|
||||
}
|
||||
return factorial;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.nwapw.abacus.number;
|
||||
|
||||
public class NaiveNumber implements Number {
|
||||
public class NaiveNumber implements NumberInterface {
|
||||
|
||||
private double value;
|
||||
|
||||
|
@ -8,8 +8,8 @@ public class NaiveNumber implements Number {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
static final NaiveNumber ZERO = new NaiveNumber(0);
|
||||
static final NaiveNumber ONE = new NaiveNumber(1);
|
||||
public static final NaiveNumber ZERO = new NaiveNumber(0);
|
||||
public static final NaiveNumber ONE = new NaiveNumber(1);
|
||||
|
||||
@Override
|
||||
public int precision() {
|
||||
|
@ -17,33 +17,33 @@ public class NaiveNumber implements Number {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Number multiply(Number multiplier) {
|
||||
public NumberInterface multiply(NumberInterface multiplier) {
|
||||
return new NaiveNumber(value * ((NaiveNumber)multiplier).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number divide(Number divisor) {
|
||||
public NumberInterface divide(NumberInterface divisor) {
|
||||
return new NaiveNumber(value / ((NaiveNumber)divisor).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number add(Number summand) {
|
||||
public NumberInterface add(NumberInterface summand) {
|
||||
return new NaiveNumber(value + ((NaiveNumber)summand).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number subtract(Number subtrahend) {
|
||||
public NumberInterface subtract(NumberInterface subtrahend) {
|
||||
return new NaiveNumber(value - ((NaiveNumber)subtrahend).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number negate() {
|
||||
public NumberInterface negate() {
|
||||
return new NaiveNumber(-value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number intPow(int exponent) {
|
||||
Number power = this;
|
||||
public NumberInterface intPow(int exponent) {
|
||||
NumberInterface power = this;
|
||||
for(int currentExponent = 1; currentExponent <= exponent; currentExponent++){
|
||||
power = power.multiply(this);
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ public class NaiveNumber implements Number {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Number number) {
|
||||
public int compareTo(NumberInterface number) {
|
||||
NaiveNumber num = (NaiveNumber) number;
|
||||
return Double.compare(value, num.value);
|
||||
}
|
||||
|
@ -62,10 +62,14 @@ public class NaiveNumber implements Number {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Number promoteTo(Class<? extends Number> toClass) {
|
||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||
if(toClass == this.getClass()) return this;
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return Double.toString(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
package org.nwapw.abacus.number;
|
||||
|
||||
public interface Number {
|
||||
|
||||
int precision();
|
||||
Number multiply(Number multiplier);
|
||||
Number divide(Number divisor);
|
||||
Number add(Number summand);
|
||||
Number subtract(Number subtrahend);
|
||||
Number negate();
|
||||
Number intPow(int exponent);
|
||||
int compareTo(Number number);
|
||||
int signum();
|
||||
|
||||
Number promoteTo(Class<? extends Number> toClass);
|
||||
|
||||
}
|
17
src/org/nwapw/abacus/number/NumberInterface.java
Executable file
17
src/org/nwapw/abacus/number/NumberInterface.java
Executable file
|
@ -0,0 +1,17 @@
|
|||
package org.nwapw.abacus.number;
|
||||
|
||||
public interface NumberInterface {
|
||||
|
||||
int precision();
|
||||
NumberInterface multiply(NumberInterface multiplier);
|
||||
NumberInterface divide(NumberInterface divisor);
|
||||
NumberInterface add(NumberInterface summand);
|
||||
NumberInterface subtract(NumberInterface subtrahend);
|
||||
NumberInterface negate();
|
||||
NumberInterface intPow(int exponent);
|
||||
int compareTo(NumberInterface number);
|
||||
int signum();
|
||||
|
||||
NumberInterface promoteTo(Class<? extends NumberInterface> toClass);
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user