Factorial
7othifaPS opened this issue · 3 comments
Suggestion
Adding Factorial function to get the factorial of numbers would be nice!
Why?
because it will help lots of people and make some code lines shorter
Other
No response
Agreement
- I have read the guidelines above and affirm I am following them with this suggestion.
This was previously proposed and not added due to limited use and the ease of writing your own function for it. It's two lines with recursion and 4 without.
This has been suggested before, there were multiple reasons we didn't add it at the time.
I couldn't find the original post so I thought I'd detail it here again in case anybody wonders for the future.
The most relevant reason is that the set of numbers you can actually perform factorial on is tiny: by factorial 13 you have overflowed the integer limit twice, after factorial 20 you have exceeded the long limit (the biggest whole number that can be represented in Skript). At that size you are already losing double precision in the units and tens column, so a lot of math functions will start to return innaccurate results.
Realistically, there are only ~20 safe inputs for the function, I've pasted them here for you.
n | n! |
---|---|
0 | 1 |
1 | 1 |
2 | 2 |
3 | 6 |
4 | 24 |
5 | 120 |
6 | 720 |
7 | 5040 |
8 | 40320 |
9 | 362880 |
10 | 3628800 |
11 | 39916800 |
12 | 479001600 |
13 | 6227020800 |
14 | 87178291200 |
15 | 1307674368000 |
16 | 20922789888000 |
17 | 355687428096000 |
18 | 6402373705728000 |
19 | 121645100408832000 |
20 | 2432902008176640000 |
because it will help lots of people
Will it?
Factorial looks like it's a common function, there will be a button for it on most scientific calculators.
In reality, it's almost never seen outside calculating permutations without repetition (e.g. if I have 1000 episodes of One Piece to watch, how many non-distinct arrangements of 10 episodes could I watch them in? -> 9.55860613*10^29
), which has a lot less use than you might expect.
Permutation has a sister function called combination (you might have learned "N-choose-K" at school) which is generally the more useful of the two, because it relates more to real-world probability applications.
The thing is, these binomial coefficients can be calculated without a factorial (in a few ways) and they're pretty much all more efficient (since they don't have to do factorial's recessive multiplication multiple times). Cool side note: combination also gives you the lower-half permutations.
Now I would be totally happy with both permutation and combination functions going into Skript because these are two really useful things (you can do some really cool probability things with binomials) and I feel like a (safe) permutations function wouldn't be a bad thing, but I feel like factorial itself is a lot of unsafe number risk (and potentially confusion for people who don't know what it's doing) for very little actual benefit.