diff --git a/docs/scarpet/language/Math.md b/docs/scarpet/language/Math.md index 10cccaf0..afb89987 100644 --- a/docs/scarpet/language/Math.md +++ b/docs/scarpet/language/Math.md @@ -6,11 +6,12 @@ There is bunch of them - they require a number and spit out a number, doing what ### `fact(n)` -Factorial of a number, a.k.a `n!`, just not in `scarpet`. Gets big... quick... +Factorial of a number, a.k.a `n!`, just not in `scarpet`. Gets big... quick... Therefore, values larger +than `fact(20)` will not return the exact value, but a value with 'double-float' precision. ### `sqrt(n)` -Square root. For other fancy roots, use `^`, math and yo noggin. Imagine square roots on a tree... +Square root (not 'a squirt') of a number. For other fancy roots, use `^`, math and yo noggin. Imagine square roots on a tree... ### `abs(n)` diff --git a/src/main/java/carpet/script/language/Arithmetic.java b/src/main/java/carpet/script/language/Arithmetic.java index e5b543f9..46f308e6 100644 --- a/src/main/java/carpet/script/language/Arithmetic.java +++ b/src/main/java/carpet/script/language/Arithmetic.java @@ -20,12 +20,23 @@ public class Arithmetic { expression.addUnaryFunction("fact", (v) -> { long number = NumericValue.asNumber(v).getLong(); - long factorial = 1; + if (number < 21) + { + long factorial = 1; + for (int i = 1; i <= number; i++) + { + factorial = factorial * i; + } + return new NumericValue(factorial); + } + // values over 21 will exceed long limits + double factorial = 1.0; for (int i = 1; i <= number; i++) { factorial = factorial * i; } return new NumericValue(factorial); + }); expression.addMathematicalUnaryFunction("sin", (d) -> Math.sin(Math.toRadians(d))); expression.addMathematicalUnaryFunction("cos", (d) -> Math.cos(Math.toRadians(d)));