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

Dear wise Monks,

I found that useful script to calculate factorials of rational numbers:

# Pragmas. use strict; # Modules. use bignum; use Math::Complex; # Global variables. use vars qw($g $lanczos_coef); $g = 7; $lanczos_coef = [ 0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7 ]; sub gamma_lanczos { my $z = shift; $z = Math::Complex->make($z); if (Re($z) < 0.5) { return pi / (sin(pi * $z) * gamma(1 - $z)); } else { $z -= 1; my $x = $lanczos_coef->[0]; foreach my $i (1 .. $g + 2) { $x += $lanczos_coef->[$i] / ($z + $i); } my $t = $z + $g + 0.5; return sqrt(2 * pi) * $t ** ($z + 0.5) * exp(-$t) * $x +; } } print gamma_lanczos(25.78), "\n"; system("pause");
But using it together with the bignum module returns the error message: Can't locate object method "_cartesian" via package "Math::BigInt". The Math::BigInt module is up to date. All I want to do is calculating factorials of rational numbers up to 999.9. Can someone help?

Replies are listed 'Best First'.
Re: Factorials of rational numbers up to 999.9
by Corion (Patriarch) on May 16, 2011 at 08:08 UTC

    You are mixing Math::Compex and bignum. I'm not really sure that that's well-supported. Have you looked at removing the use of one or the other? I would assume that using only Math::Complex should be sufficient.

    A call to ->_cartesian() is issued from within Math::Complex, so my guess is that somewhere somehow, methods from Math::Complex are issued against Math::BigInt objects that may or may not come from bignum. This may be a bug in Math::Complex, but I don't know.

      without using bignum the script works fine, but the factorials become #INF at 143!

        Most likely this is because not all elements in your calculations are Math::Complex objects, especially the constants. I would try to convert all elements to Math::Complex objects, or remove all Math::Complex calculations in favor of Math::BigInt.

        I'm not sure what part Math::Complex plays in your calculations. Maybe you can eliminate Math::Complex in favor of number pairs, (Math::BigInt, Math::BigInt), to denote the real and imaginary parts.

Re: Factorials of rational numbers up to 999.9
by anonymized user 468275 (Curate) on May 16, 2011 at 12:09 UTC
    On AIX, Perl v5.8.2, the message is clearer:

    Math::Complex::make: Cannot take real part of Math::BigFloat.

    And as I expected, it goes away, producing 7.61401993629603e+24, when the use bignum is removed.

    Math::Complex has no idea that you have invoked bignum and has incompatible methods for this type of data. Some obvious choices are:

    1) override and rewrite all the required Complex methods to be bignum-aware (check if bignum is loaded in a new "new" method) or

    2) settle for no bignum

    Update: or rewrite the gamma function from scratch using bignum, for reference: http://mathworld.wolfram.com/GammaFunction.html

    One world, one people