remove limits from fact in favour of estimation. Fixes #1065

This commit is contained in:
gnembon committed 2021-08-19 09:59:35 -03:00
1 parent 6222c97375
commit 61052ea06d
2 files changed
+14 -2

No files matched your search

+3 -2
View File
@@ -6,11 +6,12 @@ There is bunch of them - they require a number and spit out a number, doing what
### `fact(n)` ### `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)` ### `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)` ### `abs(n)`
@@ -20,12 +20,23 @@ public class Arithmetic {
expression.addUnaryFunction("fact", (v) -> expression.addUnaryFunction("fact", (v) ->
{ {
long number = NumericValue.asNumber(v).getLong(); long number = NumericValue.asNumber(v).getLong();
if (number < 21)
{
long factorial = 1; long factorial = 1;
for (int i = 1; i <= number; i++) for (int i = 1; i <= number; i++)
{ {
factorial = factorial * i; factorial = factorial * i;
} }
return new NumericValue(factorial); 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("sin", (d) -> Math.sin(Math.toRadians(d)));
expression.addMathematicalUnaryFunction("cos", (d) -> Math.cos(Math.toRadians(d))); expression.addMathematicalUnaryFunction("cos", (d) -> Math.cos(Math.toRadians(d)));