in reply to Integer overflow question

if you use bigint; you will have never have to worry.
#bad print 2**59, "\n"; #5.76460752303423e+017 print 2**59+1, "\n"; #5.76460752303423e+017
#good use bigint; print 2**59, "\n"; #576460752303423488 print 2**59+1, "\n"; #576460752303423489

Replies are listed 'Best First'.
Re^2: Integer overflow question
by waswas-fng (Curate) on Jan 03, 2005 at 00:23 UTC
    Another thing you may want to mention about bigint is that it is slower than incrementing a normal int. This may be a limiting factor in a counter that is used in a very tight loop (requirement that it support very large ints could mean tight loop or long run).


    -Waswas
Re^2: Integer overflow question
by ikegami (Patriarch) on Jan 02, 2005 at 22:56 UTC

    Someone looking at your example could think that use bigint alters how many digits print outputs, but that would be wrong. Here's a better example:

    { $a = 2**59; # Saved as a limited precision float. $b = 2**59 + 1; # Saved as a limited precision float. print($a, $/); # Prints "5.76460752303423e+017". print($b, $/); # Prints "5.76460752303423e+017". print($b-$a, $/); # Prints "0". } { use bigint; $a = 2**59; # Saved as an unlimited precision int. $b = 2**59 + 1; # Saved as an unlimited precision int. print($a, $/); # Prints "576460752303423488". print($b, $/); # Prints "576460752303423489". print($b-$a, $/); # Prints "1". }