Bug 362507 - Factorial(13) does not return correct results, factorial(12) is correct.
Summary: Factorial(13) does not return correct results, factorial(12) is correct.
Status: CONFIRMED
Alias: None
Product: kalgebra
Classification: Applications
Component: general (other bugs)
Version First Reported In: 0.11
Platform: Mint (Ubuntu based) Linux
: NOR grave
Target Milestone: ---
Assignee: Aleix Pol
URL: http://abload.de/img/factorial_13_is_...
Keywords:
Depends on:
Blocks:
 
Reported: 2016-04-30 16:50 UTC by Sebastian J.
Modified: 2016-05-02 07:24 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
Screenshot for easy understanding... (55.59 KB, image/png)
2016-04-30 16:51 UTC, Sebastian J.
Details
KAlgebra script for your convenience (365 bytes, text/plain)
2016-04-30 16:52 UTC, Sebastian J.
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian J. 2016-04-30 16:50:24 UTC
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)
Comment 1 Sebastian J. 2016-04-30 16:51:43 UTC
Created attachment 98709 [details]
Screenshot for easy understanding...

Screenshot for easy understanding...
Comment 2 Sebastian J. 2016-04-30 16:52:21 UTC
Created attachment 98710 [details]
KAlgebra script for your convenience

KAlgebra script for your convenience
Comment 3 Aleix Pol 2016-05-01 22:59:48 UTC
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
Comment 4 Aleix Pol 2016-05-01 23:04:06 UTC
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?
Comment 5 Sebastian J. 2016-05-02 07:24:50 UTC
(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.