in reply to Re^3: Largest integer in 64-bit perl
in thread Largest integer in 64-bit perl

[Upd: This pertains to when using addition.] An int, yes. A float, no. Just like in C. Ints are promoted to floats, but floats aren't promoted to integers. You need to cast to int to get int behaviour. Just like in C.

use v5.14; use warnings; my $x = 2**53; say sprintf "%d", $x; say sprintf "%d", $x + 1; say sprintf "%d", int( $x ) + 1;
#include <inttypes.h> #include <stdint.h> #include <stdio.h> int main( void ) { double x = 9007199254740992; printf( "%"PRIu64"\n", (uint64_t)( x ) ); printf( "%"PRIu64"\n", (uint64_t)( x + 1 ) ); printf( "%"PRIu64"\n", (uint64_t)( (uint64_t)x + 1 ) ); }
9007199254740992 9007199254740992 9007199254740993

Other differences between floats and ints is the existence of inifinity, NaN and -0.

Replies are listed 'Best First'.
Re^5: Largest integer in 64-bit perl
by syphilis (Archbishop) on May 27, 2025 at 14:24 UTC
    .... but floats aren't promoted to integers.

    It can get a bit moot:
    D:\>perl -MDevel::Peek -le "$x = (2 ** 53) - 1; Dump $x; $x++; Dump $x +; $x++; Dump $x; $x++; Dump $x;" SV = NV(0x28e88c5a2f8) at 0x28e88c5a310 REFCNT = 1 FLAGS = (NOK,pNOK) NV = 9007199254740991 SV = PVNV(0x28e88c24e10) at 0x28e88c5a310 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 9007199254740992 NV = 9007199254740991 PV = 0 SV = PVNV(0x28e88c24e10) at 0x28e88c5a310 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 9007199254740993 NV = 9007199254740991 PV = 0 SV = PVNV(0x28e88c24e10) at 0x28e88c5a310 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 9007199254740994 NV = 9007199254740991 PV = 0
    The fact that the flags change from (NOK,pNOK) to (IOK,pIOK) suggests to me that the NV has been promoted to an IV.
    Or does the fact that both the IV and NV slots have been filled suggest that it's neither an IV nor an NV. Or maybe that suggests that it's both ? ... I don't know.

    Anyway, I'm not looking for an argument. It's just one of those things that I find a little mysterious.

    Other differences between floats and ints is the existence of inifinity, NaN and -0

    One of my beefs with Math::BigInt is that it has a NaN and an inf:
    D:\>perl -MMath::BigInt -le "$x = Math::BigInt->new(2.3); print $x; pr +int ref($x);" NaN Math::BigInt D:\>perl -MMath::BigInt -le "$x = Math::BigInt->new(1) / 0; print $x; +print ref($x);" inf Math::BigInt
    I don't regard it as all that important, but it probably shouldn't do that.
    I couldn't quickly get it to provide a "-0", but I did get the following weirdness:
    D:\>perl -MMath::BigInt -le "$x = Math::BigInt->new(1) / inf; print $x +; print ref($x);" 0 Math::BigInt D:\>perl -MMath::BigInt -le "$x = Math::BigInt->new(1) / -inf; print $ +x; print ref($x);" -1 Math::BigInt
    Cheers,
    Rob

      When the pre-increment operator is provided an integer that less than its max, it just adds one to it. Otherwise, it calls crazy, 150-lines long sv_inc. And yeah, sv_inc can promote to integer. Woops.

      Out of pure curiosity: What are you all working on that requires integers bigger than 64 bit? So far, the only cases i could think of are things like black holes and possibly simulating nuclear explosions down to subatomic particles.

      (I'm working mostly with boring financial data. If 64 bit is ever insufficient, something inflationary has probably gone very, very wrong. Or my software is used in Zimbabwe. Same thing, really...)

      PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
      Also check out my sisters artwork and my weekly webcomics

      I couldn't quickly get it to provide a "-0"

      $ perl -e'$_ = -1; $_ /= 10 while $_ < 0; printf "%g\n", $_;' -0