This has cost me almost an hour of my precious study time. The factorial function will give wrong results from exactly 13 on. I.e. factorial (12) = 12*11*10*9*8*7*6*5*4*3*2*1 = 479001600 That is just fine. But from 13 on, the result seems to pass a limit or whatver and gives back a wrong result... 13*12*11*10*9*8*7*6*5*4*3*2*1 =6227020800 (the same what Wolfram Alpha would return for 13! ) but, using the factorial function factorial(13) =1932053504 Which is wrong. Factorial(14) will be wrong as well... Reproducible: Always Steps to Reproduce: 1. Start kAlgebra 2. In the Input box type "12*11*10*9*8*7*6*5*4*3*2*1", hit Enter 3. in the input box type "ans*13", hit enter 4. Now input "factorial(12)" in the input box, hit enter 5. Now input "factorial(13)" in the input box, hit enter 6. Compare results. Actual Results: Factorial function is not the same as equivalent when value is 12!. Expected Results: 13! or "factorial(13)" gives the correct value Severity: a calculator that does surprisingly give back wrong results when you dont expect it is worthless. I had to debug a long equation step by step to find out the part with factorial(13) will give the wrong result. Here the kAlgebra script -------------------- save as factorial.kal and load in kAlgebra for convenience -------------- 1 factorial(1) 2*1 factorial(2) 3*2*1 factorial(3) 4*3*2*1 factorial(4) 5*4*3*2*1 factorial(5) 6*5*4*3*2*1 factorial(6) 7*6*5*4*3*2*1 factorial(7) 8*7*6*5*4*3*2*1 factorial(8) 9*8*7*6*5*4*3*2*1 factorial(9) 10*9*8*7*6*5*4*3*2*1 factorial(10) 11*10*9*8*7*6*5*4*3*2*1 factorial(11) 12*11*10*9*8*7*6*5*4*3*2*1 factorial(12) 13*12*11*10*9*8*7*6*5*4*3*2*1 factorial(13)
Created attachment 98709 [details] Screenshot for easy understanding... Screenshot for easy understanding...
Created attachment 98710 [details] KAlgebra script for your convenience KAlgebra script for your convenience
Git commit 5c4ff0778bead3e80299b58dae43c32b7b1f6d8c by Aleix Pol. Committed on 01/05/2016 at 22:51. Pushed by apol into branch 'master'. Use cmath implementation for factorial M +3 -8 analitza/operations.cpp M +3 -0 analitza/tests/analitzatest.cpp http://commits.kde.org/analitza/5c4ff0778bead3e80299b58dae43c32b7b1f6d8c
That's a technology limitation. You're hitting the precision limit c++ double has. Maybe we should adopt a multi precision library. What's your use case?
(In reply to Aleix Pol from comment #4) > What's your use case? My usecase? permutations and probabilities... you can't avoid factorials of big numbers. Just calculate the possible combinations of e.g. lottery examples.