mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 07:20:09 -08:00
Merge branch 'master' of https://github.com/DanilaFe/abacus
This commit is contained in:
commit
07acfefd0b
|
@ -7,7 +7,6 @@ public class FunctionDatabase {
|
||||||
private HashMap<String, Function> functions;
|
private HashMap<String, Function> functions;
|
||||||
|
|
||||||
private void registerDefault(){
|
private void registerDefault(){
|
||||||
|
|
||||||
functions.put("+", new Function() {
|
functions.put("+", new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(Number[] params) {
|
protected boolean matchesParams(Number[] params) {
|
||||||
|
@ -63,6 +62,25 @@ public class FunctionDatabase {
|
||||||
return params[0].divide(params[1]);
|
return params[0].divide(params[1]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
functions.put("!", new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(Number[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Number applyInternal(Number[] params) {
|
||||||
|
Number factorial = params[0];
|
||||||
|
Number 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()));
|
||||||
|
factorial = factorial.multiply(multiplier);
|
||||||
|
}
|
||||||
|
return factorial;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public FunctionDatabase(){
|
public FunctionDatabase(){
|
||||||
|
|
13
src/org/nwapw/abacus/number/NaiveNumber.java
Normal file → Executable file
13
src/org/nwapw/abacus/number/NaiveNumber.java
Normal file → Executable file
|
@ -8,6 +8,9 @@ public class NaiveNumber implements Number {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static final NaiveNumber ZERO = new NaiveNumber(0);
|
||||||
|
static final NaiveNumber ONE = new NaiveNumber(1);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int precision() {
|
public int precision() {
|
||||||
return 4;
|
return 4;
|
||||||
|
@ -39,13 +42,14 @@ public class NaiveNumber implements Number {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number zero() {
|
public int compareTo(Number number) {
|
||||||
return new NaiveNumber(0);
|
NaiveNumber num = (NaiveNumber) number;
|
||||||
|
return Double.compare(value, num.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number one() {
|
public int signum() {
|
||||||
return new NaiveNumber(1);
|
return this.compareTo(ZERO);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -53,5 +57,6 @@ public class NaiveNumber implements Number {
|
||||||
if(toClass == this.getClass()) return this;
|
if(toClass == this.getClass()) return this;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,8 @@ public interface Number {
|
||||||
Number add(Number summand);
|
Number add(Number summand);
|
||||||
Number subtract(Number subtrahend);
|
Number subtract(Number subtrahend);
|
||||||
Number negate();
|
Number negate();
|
||||||
|
int compareTo(Number number);
|
||||||
Number zero();
|
int signum();
|
||||||
Number one();
|
|
||||||
|
|
||||||
Number promoteTo(Class<? extends Number> toClass);
|
Number promoteTo(Class<? extends Number> toClass);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user