+ 3

How do I find factorial of decimal numbers ?

Hello there . Although I've been using SoloLearn for the past 1.5 years, I'm a bit new to the Q&A community, so this question might have been already asked. To find the factorial of any number (real or integer), there's a mathematical function[The usual functions like f(x) in maths.] known as gamma function. I've been trying to imitate this in Java for some time now but always go wrong with the decimal part. So, after many tries, I'm putting it to the community wall. I want someone who can explain me the gamma function in detail & give me a code (in Java of course !) to find the factorials of decimal numbers that works correctly.

1st Jun 2019, 12:10 PM
P Jain
P Jain - avatar
4 odpowiedzi
+ 9
You can refer this site https://www.quora.com/How-do-I-calculate-decimal-factorials-like-1-7 I used gospers Approximation here to get approximate value https://code.sololearn.com/cBuCn5lNHsFh/?ref=app
1st Jun 2019, 4:14 PM
Rstar
Rstar - avatar
+ 2
The gamma function is too difficult math for me, but building on the suggestion of Rstar I made java version for the Gosper's approximation. I used type overloading so a different formula is used when the parameter is int or double. My take-away from this was, that in Java it is important to use double constants inside the formula, like 2.0*n instead of 2*n, otherwise the jvm tends to apply some unwanted implicit type conversions that can seriously impact the result. https://code.sololearn.com/c53f6WaK5R5m/?ref=app
2nd Jun 2019, 3:58 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Thanks so much to both Rstar & Tibor Santa for helping me out. I know you have not imitated the Gamma Function but Gosper's approximation is also OK till the answer comes out as right.
2nd Jun 2019, 4:08 AM
P Jain
P Jain - avatar
0
Clarification of my own comment: In java, 1/3 = 0 1.0/3 = 0.33333333 because in the first case we do integer divisions. This was the part I had first trouble with, causing the formula to arrive at wrong results.
2nd Jun 2019, 4:07 AM
Tibor Santa
Tibor Santa - avatar