Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Floating point factorials

by John M. Dlugosz (Monsignor)
on May 14, 2011 at 15:59 UTC ( [id://904841]=note: print w/replies, xml ) Need Help??


in reply to Re: Floating point factorials
in thread Floating point factorials

You can use "memoize" to improve that, or make a new calculation build on the highest one it already knows

Your code will give inaccurate results once the number of non-zero leading digits exceeds that of the floating point precision. Try using BigInt in your code and see if the results near the top of your range are any different, and notice that you can keep going!

170! = 7.25741561530799e+306

170! = 7257415615 3079989673 9672821112 9263114716 9916812964 5137654357 7798900561 8434017061 5785235074 9242617459 5114909912 3783852077 666602256544 275302532890 0773207510 9024004302 8005829560 3966612599 6582571043 9855829425 7568966313 439612262571094946806711205568880457193340212661452800000000000000000000000000000000000000000

So, I guess 170! is indeed accurate to 15 decimal digits when using floating point.

But 200! = 7886578673647905035523632139321850622951359 7768717326329474253324435944996340334292030428 4011984623904177212138919638830257642790242637 105061926624952829931113462857270763 317237396988943922445621451 66424025403329186413122742829485327 752424240757390324032125740557956 866022603190417032406235170085879 6178922222789623703897374720000000000000000000000000000000000000000000000000

Years ago, I got around the overflow by using logs. It's interesting to play with.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://904841]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found