Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Number too big to fit in integer

by syphilis (Archbishop)
on Dec 31, 2022 at 13:05 UTC ( [id://11149220]=note: print w/replies, xml ) Need Help??


in reply to Re: Number too big to fit in integer
in thread Number too big to fit in integer

That's tricky: for a number that doesn't fit in UV (integer), Perl automatically stores it in NV (double) which can result in an immediate loss of precision. For my system build (5.26.1 for Ubuntu) the first integer that gets stored as a double distinguishable from ~0 is 18446744073709554600 (which has actually been stored in NV as 2^64 + 2^12):
% perl -E 'say "ok" if 18446744073709554600 > ~0' ok % perl -E 'say "ok" if 18446744073709554599 > ~0' %
This is a bit baffling.
With perl-5.36.0, ivsize == 8, and nvtype eq 'double', it's looking to me that the "first integer that gets stored as a double distinguishable from ~0 is" 18446744073709553665:
>perl -E "say 'ok' if 18446744073709553665 > ~0" ok >perl -E "say 'ok' if 18446744073709553664 > ~0" >
Am I overlooking a bug in perl-5.26.1 ? Or is there something else going on that I've missed ?
For me, 18446744073709554599 is correctly evaluated as being greater than ~0 :
>perl -E "say 'ok' if 18446744073709554599 > ~0" ok >
Cheers,
Rob

Replies are listed 'Best First'.
Re^3: Number too big to fit in integer
by hv (Prior) on Dec 31, 2022 at 21:57 UTC

    I get the same with 5.36.0 as I did with 5.26.1. Here's what I originally used to home in on the tipping point:

    % /opt/v5.36.0/bin/perl -MMath::BigInt -wle '$z0 = Math::BigInt->new(2 +)**64; for my $i (0..12) { $zi = $z0 + 2**$i; $ni = "$zi"; $d = ($ni +== ~0) ? "same" : "differ"; print "$i: $d" }' 0: same 1: same 2: same 3: same 4: same 5: same 6: same 7: same 8: same 9: same 10: same 11: same 12: differ %

    Using eval "$zi == ~0" to make it more like direct use gives me the same result.

      I get the same with 5.36.0 as I did with 5.26.1.

      Oh, I see. You were adding powers of 2 to the original value of 2**64.
      I get the same as you when I run the code you provided.

      I was incrementing by one:
      D:\>perl -MMath::BigInt -wle "$x = Math::BigInt->new(~0); $x++; while( +\"$x\" == ~0){$x++}; print $x;" 18446744073709553665
      Of course, this particular perl configuration sees 18446744073709553665 and 18446744073709554600 as the same value, anyway:
      D:\>perl -le "print 'ok' if 18446744073709553665 == 184467440737095546 +00;" ok
      However, that your 5.26.1 regards 18446744073709554600 and 18446744073709554599 as different values is a bug in 5.26.1. (I see the same bug in my Windows build of 5.26.0.)
      I believe that perl should regard those two values as equivalent for all IV and NV configurations.
      D:\>perl -wle "print 'ok' if 18446744073709554600 == 18446744073709554 +599;" ok
      For me, that fails to output 'ok' on 5.26.0. (If 5.26.0 has been built with -Duselongdouble, then 5.26.0 does get it right and outputs 'ok'.)

      Cheers,
      Rob

        Ok, there has been a change somewhere along the line: if I compare "n more than ~0" against "~0" for integer n, up to 5.28 it first compares different for n = 2985 (ie with 18446744073709554600); with 5.30 and later it first compares different for n = 2050 (ie with 18446744073709553665):

        % /opt/v5.28.1/bin/perl -MMath::BigInt -we '$z0 = Math::BigInt->new(~0 +); for (1..4096) { $z1 = $z0 + $_; $n1 = "$z1"; die $_ if $n1 != ~0 } +' 2985 at -e line 1. % /opt/v5.30.0-d/bin/perl -MMath::BigInt -we '$z0 = Math::BigInt->new( +~0); for (1..4096) { $z1 = $z0 + $_; $n1 = "$z1"; die $_ if $n1 != ~0 + }' 2050 at -e line 1. %

        (From the naming I know that's a v5.30.0 built with debugging, but that shouldn't make a difference here.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149220]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found