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

Respected Monks,

I have noticed that infinity is expressed differently depending on platform:

Are there any resources on how infinity (and possibly other special numbers, e.g. NaN) are handled by Perl on different platforms?

Update Fri Jan 18 16:55:20 CET 2008: This many faces of infinity is documented in Math::Complex:

Note that the stringified form of infinity varies between platforms: it can be for example any of inf infinity INF 1.#INF or it can be something else.
--
Andreas

Replies are listed 'Best First'.
Re: Infinity and platforms
by GrandFather (Saint) on Jan 18, 2008 at 06:35 UTC

    The issue is not so much the platform, as the compiler (more particularly, the maths runtime library) that was used to create the Perl interpreter.


    Perl is environmentally friendly - it saves trees
      Is there an overview somewhere of the possible variants of infinity? I want to test functions that are expected to return infinity, independently of architectures, compilers and libraries.
      --
      Andreas
        SUSv3 says "infinity shall be converted in one of the styles "[-]inf" or "[-]infinity" ; which style is implementation-defined." Unfortunately, MS can't change their broken, made up garbage without breaking their compatibility with folk who have coded to it. I'm rather surprised that Solaris is broken, though.

        You could check the standards document for C, and cross-check the IEEE arithmetic standard, which I think is IEEE 754.

        This could work, especially if you can be reasonably sure that the C-compiler being used is actually standards-compliant, and the standard doesn't say something like "it's up to the compiler builder," which it almost certainly does, so the likely short answer to your question is "No." You may (probably will) have to take this up with members of the numerical analysis community, where they deal with this sort of issue all the time.


        emc

        Information about American English usage here and here.

        Any Northeastern US area jobs? I'm currently unemployed.

Re: Infinity and platforms
by BrowserUk (Patriarch) on Jan 18, 2008 at 07:24 UTC
Re: Infinity and platforms
by rgiskard (Hermit) on Jan 18, 2008 at 13:21 UTC
Re: Infinity and platforms
by starbolin (Hermit) on Jan 18, 2008 at 17:56 UTC

    Checking the return values against every possible permutation is not the way to write portable code. The proper procedure is to compare the return value against the system's own definition of Infinity. In C we'd compare the return value against the appropriate constant in float.h. Under ISO C99 we call isint() to do the check transparently. Perl being an interpreter we can ask Perl what it thinks the value is:

    #!/usr/bin/perl -w use Math::BigFloat; my $inf = Math::BigFloat->binf(); # $inf contains: "inf" print $inf,"\n";

    Though if you use any of the Math modules in the core distribution you can import transparent checks for infinity and if you divide by zero in vanilla perl you get an exception. I guess I'm not sure what problem you are trying to solve.

    UPDATE: I said "they contain transparent". Changed it to "you can import"


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

      andreas1234567 points out that using Math::BigFloat to recover the moniker for infinity may not jive with the perl internal for infinity. This was indeed an error on my part. I should have realized that Math::BigFloat might not have been compiled against the same library as Perl. Here is the demonstration from andreas1234567:


      s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: Infinity and platforms
by swampyankee (Parson) on Jan 18, 2008 at 19:24 UTC

    There are also platforms,such as IBM mainframes, which don't define an "infinity." Perl uses the math library of the compiler with which it's built on a given platform. I would suspect that most Linux platforms use the gcc compiler, most commercial Unix-oid systems (such as Solaris or AIX) use their vendor's proprietary compiler, Windows usually uses Microsoft's compiler, etc.

    If you're getting "infinities" as the result of a computational process, I would suggest that some rather careful code revisions are required. Numerical programming is rather an arcane speciality, and poking around Netlib, NA-Net, and NCSA may be profitable. Some CS programs are less than thorough in this area, which is something I find quite disturbing.


    emc

    Information about American English usage here and here.

    Any Northeastern US area jobs? I'm currently unemployed.