in reply to Largest integer in 64-bit perl

Perl may internally keep your numbers as floating point values unless you tell it not to. Add use integer; at the top of your program and see what happens.

If you want to print integer values, use %d, not %f in your print format.

A hex value of all 'F's represents a negative value (2's complement notation). I altered your max value to be the largest 64 bit positive number.

use integer; my $MAXQUAD = 9223372036854775807; # equals 0x7fffffffffffffff for (my $i = 0; $i <= $MAXQUAD; $i++) { printf("\n %d", $i); } exit;
90% of every Perl application is already written.
dragonchild

Replies are listed 'Best First'.
Re^2: Largest integer in 64-bit perl
by harangzsolt33 (Deacon) on May 17, 2025 at 00:18 UTC
    Interestingly, the "use integer" line did absolutely nothing for me on 32-bit Windows XP TinyPerl 5.8. I tried to run it on Linux with 64-bit Perl 5.36, and to my surprise, it didn't even run! :-O It produced no error message and no output at all. The program does have a for loop in it, but it looked like it didn't even start the loop. I don't understand... Here's the program again:

    #!/usr/bin/perl use strict; use warnings; use integer; $| = 1; my $MAXQUAD = 18446744073709551615; for (my $i = 9007199254740000; $i <= $MAXQUAD; $i++) { printf("\n %.0f %d", $i, $i); }

    So, anyway, I did notice that if I remove the "use integer" line, then the program runs. In fact, if I started the loop at, let's say, 4611686018420000000, then it will keep counting on the 64-bit Perl in Linux. Interesting!

    However, when I stop the program by pressing CTRL+C, I notice something else. When printing the number $i as float, it sort of loses precision, but when printing it as %d number, it prints it precisely. In TinyPerl, I had the opposite experience with this where %f printed more precise values than %d. This is so weird... :P

    4611686018420022272 4611686018420022338 4611686018420022272 4611686018420022339 4611686018420022272 4611686018420022340 4611686018420022272 4611686018420022341 4611686018420022272 4611686018420022342 4611686018420022272 4611686018420022343 4611686018420022272 4611686018420022344 4611686018420022272 4611686018420022345 4611686018420022272 4611686018420022346 4611686018420022272 4611686018420022347 4611686018420022272 4611686018420022348 4611686018420022272 4611686018420022349 4611686018420022272 4611686018420022350

      Try printing $MAXQUAD. I bet you get '-1'. That's why your code looks like it doesn't run.

      Update: I tried this and it prints the set value. However, I wonder if perl is treating it as a signed value when comparing it to $i because the loop never executes.

      90% of every Perl application is already written.
      dragonchild