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

Is there a standard way of handling imaginary and/or complex numbers in Perl?

I am writing a little utility (for fun - that's the kind of fun guy I am) to extract the roots of quartic equations and I have found that the square root of any negative number is returned as 2^31, which at least stops the script crashing but is otherwise not very helpful.

Is this OS-dependent?

Replies are listed 'Best First'.
Re: Imaginary / Complex numbers
by Ionitor (Scribe) on Jul 22, 2002 at 13:43 UTC
    I think you want Math::Complex from CPAN. This is probably the easiest and most standard way of handling it.
Re: Imaginary / Complex numbers
by Abigail-II (Bishop) on Jul 22, 2002 at 13:46 UTC
    Math::Complex is a standard module that comes with perl 5.8.0. You also might want to look into the PDL module. It has a lot of math functionality - though I'm not sure whether it actually has complex numbers.

    Abigail

      In 2002, there was basic support using PDL::Complex (which didn't have working matrix multiplication, and had an extra exposed dimension for real/imaginary, which caused problems). That still (as of 2.080) exists, but is now deprecated in favour of the "native complex" (C99) number functionality (cfloat, cdouble, cldouble types).
Re: Imaginary / Complex numbers
by dimmesdale (Friar) on Jul 22, 2002 at 13:45 UTC