http://www.perlmonks.org?node_id=906612

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

Dear Monks,

I wonder why the following code produces NaN for $number. The problem seems to be the bignum module, since without it the script works fine. But I need this module because I have to handle BIG numbers (~10**2500).

use bignum; $number=330978816.3082480433473810957254665863937; $number=(int(((log($number)/log(10))*10)+0.5))/10; print"$number\n";
Thanks for your help

Replies are listed 'Best First'.
Re: What causes NaN when using bignum
by BrowserUk (Patriarch) on May 25, 2011 at 09:12 UTC

    Your code produces 8.5 on my 64-bit/5.10 install.


    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.
      This is curious. On my 64bit with Perl 5.12.1 it produces NaN?!?

        What output do you get from this on your system?

        #! perl -slw use bignum; $number = 330978816.3082480433473810957254665863937; $number =( int( ( ( log( $number ) / log( 10 ) ) * 10 ) + 0.5 ) ) / 10 +; print"$number\n"; print "$k -> $v" while ( $k, $v ) = each %INC; __END__ c:\test>junk 8.5 XSLoader.pm -> C:/Perl64/lib/XSLoader.pm re.pm -> C:/Perl64/lib/re.pm Math/BigInt/FastCalc.pm -> C:/Perl64/lib/Math/BigInt/FastCalc.pm warnings/register.pm -> C:/Perl64/lib/warnings/register.pm bigint.pm -> C:/Perl64/lib/bigint.pm C:/Perl64/site/lib/sitecustomize.pl -> C:/Perl64/site/lib/sitecustomiz +e.pl ActiveState/Path.pm -> C:/Perl64/lib/ActiveState/Path.pm Cwd.pm -> C:/Perl64/site/lib/Cwd.pm warnings.pm -> C:/Perl64/lib/warnings.pm Config_git.pl -> C:/Perl64/lib/Config_git.pl File/Basename.pm -> C:/Perl64/lib/File/Basename.pm Config.pm -> C:/Perl64/lib/Config.pm integer.pm -> C:/Perl64/lib/integer.pm ActivePerl/Config.pm -> C:/Perl64/lib/ActivePerl/Config.pm Math/BigFloat.pm -> C:/Perl64/lib/Math/BigFloat.pm Carp.pm -> C:/Perl64/lib/Carp.pm Math/BigInt.pm -> C:/Perl64/lib/Math/BigInt.pm Exporter/Heavy.pm -> C:/Perl64/lib/Exporter/Heavy.pm bignum.pm -> C:/Perl64/lib/bignum.pm Exporter.pm -> C:/Perl64/lib/Exporter.pm strict.pm -> C:/Perl64/lib/strict.pm vars.pm -> C:/Perl64/lib/vars.pm Math/BigInt/Calc.pm -> C:/Perl64/lib/Math/BigInt/Calc.pm constant.pm -> C:/Perl64/lib/constant.pm Config_heavy.pl -> C:/Perl64/lib/Config_heavy.pl overload.pm -> C:/Perl64/lib/overload.pm AutoLoader.pm -> C:/Perl64/lib/AutoLoader.pm DynaLoader.pm -> C:/Perl64/lib/DynaLoader.pm

        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: What causes NaN when using bignum
by GrandFather (Saint) on May 25, 2011 at 09:19 UTC

    Your code produces 8.5 on my 5.10.1 32 bit ActiveState install under Windows XP.

    True laziness is hard work
Re: What causes NaN when using bignum
by choroba (Cardinal) on May 25, 2011 at 12:36 UTC
    Simplified:
    $ perl -e 'use bigint;print log(10),"\n"' NaN $ perl --version This is perl, v5.10.1 (*) built for i486-linux-gnu-thread-multi
    Update: Using Ubuntu 10.04.2 LTS

      Which strongly suggests that the cause is one of the many possible binary modules (Math::BigInt::BitVect, Math::BigInt::Pari and Math::BigInt::GMP.) that can sit behind use bignum doesn't properly handle log.

      If you could dump your %INC, maybe we could track down which one(s).


      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.
        Math/BigInt.pm -> /ha/work/projects/perl_repo/Ubuntu/10.04/i686/ +share/perl/5.10/Math/BigInt.pm warnings/register.pm -> /usr/share/perl/5.10/warnings/register.pm Exporter/Heavy.pm -> /usr/share/perl/5.10/Exporter/Heavy.pm bigint.pm -> /ha/work/projects/perl_repo/Ubuntu/10.04/i686/ +share/perl/5.10/bigint.pm vars.pm -> /usr/share/perl/5.10/vars.pm strict.pm -> /usr/share/perl/5.10/strict.pm Exporter.pm -> /usr/share/perl/5.10/Exporter.pm constant.pm -> /ha/work/projects/perl_repo/Ubuntu/10.04/i686/ +share/perl/5.10/constant.pm Math/BigInt/Calc.pm -> /ha/work/projects/perl_repo/Ubuntu/10.04/i686/ +share/perl/5.10/Math/BigInt/Calc.pm warnings.pm -> /usr/share/perl/5.10/warnings.pm overload.pm -> /usr/share/perl/5.10/overload.pm integer.pm -> /usr/share/perl/5.10/integer.pm
        On the other hand, works well on perl 5.12.1 on openSUSE 11.3.
        Update: paths added. I checked everything in the perl_repo, it's latest CPAN version available.
      log(10) works fine on my 5.8.5 rhel linux 4 box... (produces... "2.303585092994045684017991454684364207601") I also get 8.5 from the original question.
Re: What causes NaN when using bignum
by Anonymous Monk on May 25, 2011 at 09:27 UTC
    $ perl use bignum; $number=330978816.3082480433473810957254665863937; $number=(int(((log($number)/log(10))*10)+0.5))/10; print"$number\n"; __END__ NaN $ perldoc -V Perldoc v3.15_02, under perl v5.012002 for MSWin32
      use bignum; $number=330978816.3082480433473810957254665863937; $number=(int(((log($number)/log(10))*10)+0.5))/10; print"$number\n"; print "$_\n" for %INC; __END__ NaN Math/BigInt.pm C:/perl/5.12.2/lib/Math/BigInt.pm warnings/register.pm C:/perl/5.12.2/lib/warnings/register.pm Exporter/Heavy.pm C:/perl/5.12.2/lib/Exporter/Heavy.pm bigint.pm C:/perl/5.12.2/lib/bigint.pm vars.pm C:/perl/5.12.2/lib/vars.pm strict.pm C:/perl/5.12.2/lib/strict.pm Exporter.pm C:/perl/5.12.2/lib/Exporter.pm bignum.pm C:/perl/5.12.2/lib/bignum.pm constant.pm C:/perl/5.12.2/lib/constant.pm Math/BigInt/Calc.pm C:/perl/5.12.2/lib/Math/BigInt/Calc.pm warnings.pm C:/perl/5.12.2/lib/warnings.pm overload.pm C:/perl/5.12.2/lib/overload.pm integer.pm C:/perl/5.12.2/lib/integer.pm Math/BigFloat.pm C:/perl/5.12.2/lib/Math/BigFloat.pm
Re: What causes NaN when using bignum
by tune (Curate) on May 25, 2011 at 12:19 UTC
    $ perl -v|grep "This is perl" This is perl, v5.10.1 (*) built for i686-linux-thread-multi $ uname -i -o -r 2.6.9-89.0.2.EL.msdw.1largesmp x86_64 GNU/Linux $ perl -Mbignum -e '$number=330978816.3082480433473810957254665863937; +$number=(int(((log($number)/log(10))*10)+0.5))/10;print"$number\n";' 8.5

    --
    tune

Re: What causes NaN when using bignum
by wind (Priest) on May 25, 2011 at 16:19 UTC
    >perl -Mbignum -e "print log(10)" NaN >perl -v This is perl 5, version 12, subversion 1 (v5.12.1) built for MSWin32-x +86-multi-thread
    Works fine of course without bignum, producing 8.5 for the original question.
Re: What causes NaN when using bignum
by davido (Cardinal) on May 26, 2011 at 06:59 UTC

    Interesting quirk. I get 8.5 on my 32 bit install of Perl 5.10.1 with Ubuntu Linux 11. But with 64 bit Strawberry 5.12.2 on Windows Vista 64 I get NaN.

    I saw you posted the @INC. I'm going to dig in, though I suspect someone more proficient with XS is going to catch the issue first.


    Dave