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 | |
by ikegami (Patriarch) on May 27, 2025 at 20:04 UTC | |
by cavac (Prior) on Jun 02, 2025 at 14:49 UTC | |
by ikegami (Patriarch) on Jun 02, 2025 at 15:44 UTC |