Microcebus has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

In a script, I use factorials to calculate propabilities. I calculate my propabilities with

$limit=1000; @factorials=(1); foreach(1..$limit) { $factorial=$factorial*$_; push(@factorials,$factorial); }
Of course this works fine with integers, but how can I calculate foctorials of floating point numbers?

Replies are listed 'Best First'.
Re: Floating point factorials
by ikegami (Patriarch) on May 13, 2011 at 20:55 UTC
    The factorial function is only defined for non-negative integers.
Re: Floating point factorials
by BrowserUk (Patriarch) on May 13, 2011 at 21:08 UTC

    Note also:

    The definition of the factorial function can also be extended to non-integer arguments, ...

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Floating point factorials
by ww (Archbishop) on May 14, 2011 at 00:33 UTC
    BUK's reference notwithstanding, I don't think you want to get in bed with using floating point numbers in calculation of probabilities (would you mind going back and editing your spelling, so future readers can find your nodes?) because of some inherent properties of using FP in binary-based computation.

    See, for example, What Every Computer Scientist Should Know About Floating-Point... and similarly-titled articles directing themselves specifically to "Programmers." See also, using Google or Super Search, the many SOPW nodes posted by programmers who've been bitten by the FP bug.

      I don't see how that issue with floating point (that all programmers should be familiar with) sensibly leads to a conclusion that floating point is just inappropriate for computing things, including probabilities.

      - tye        

Re: Floating point factorials
by Khen1950fx (Canon) on May 14, 2011 at 02:20 UTC
    A better way to get your factorials:
    #!/usr/bin/perl use strict; use warnings; sub fact { my $n = shift; my $result = 1; foreach my $i (1 .. $n) { $result *= $i; } $result; } foreach my $i (1 .. 170) { print "$i! = ", fact($i), "\n"; }
    Note: After 171>, the script just says "inf", so I cut it off at 170.
      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.