Keep It Simple, Stupid | |
PerlMonks |
Re^2: Floating point factorialsby John M. Dlugosz (Monsignor) |
on May 14, 2011 at 15:59 UTC ( [id://904841]=note: print w/replies, xml ) | Need Help?? |
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.
In Section
Seekers of Perl Wisdom
|
|