What is MAXINT, or when to use bigint?

B

bwooster47

On my machine, perl -V:ivsize returns
ivsize='4';

This I thought meant that 4-byte integers are supported, so max
integer is probably 2billion or 4billion (approximately).

But - I seem to be able to add much larger numbers, and stay in
integer space - such as 51 billion.

The exponentiation operator does internally use "double", but it still
seems to return very large integers, only breaking at around 10^15, as
this output shows:

2**50 = 1.12589990684262e+015
2**49 = 562949953421312
2**32 = 4294967296
with
use bigint;
2**50 = 1125899906842624

If I know the maximum integer I may encounter in a program, is there a
programmatic way to determine whether to use bigint? Doing a "use
bigint" slows down the whole thing by 40 times, in the example I am
running, and a few tests I ran show same results with and without
bigint.
But - how to know for sure that I don't need bigint - if I know that
all calculations to 2**49 are safe, that would make it sure. But not
sure what is so magic about 2**49!
 
S

Sisyphus

..
..
If I know the maximum integer I may encounter in a program, is there a
programmatic way to determine whether to use bigint? Doing a "use
bigint" slows down the whole thing by 40 times, in the example I am
running, and a few tests I ran show same results with and without
bigint.
But - how to know for sure that I don't need bigint - if I know that
all calculations to 2**49 are safe, that would make it sure. But not
sure what is so magic about 2**49!

I think you'll find that integers up to 2 ** 53 are handled correctly by
perl (as the "double" assigns 53 bits to its integer portion).
Unfortunately print() is not capable of reflecting that degree of
reliability. If you use printf() and the "%f" format, you should be able to
see that numbers up to (and including) 2 ** 53 are being handled correctly:

use warnings;
use strict;
my $x = (2 ** 53) - 1;
my $y = 2 ** 53;
my $z = (2 ** 53) + 1;
printf "%f\n%f\n%f\n", $x, $y, $z;

For me that outputs:
9007199254740991.000000
9007199254740992.000000
9007199254740992.000000

Cheers,
Rob
 
M

Mumia W.

[...]
2**50 = 1.12589990684262e+015
2**49 = 562949953421312
2**32 = 4294967296
with
use bigint;
2**50 = 1125899906842624

If I know the maximum integer I may encounter in a program, is there a
programmatic way to determine whether to use bigint? Doing a "use
bigint" slows down the whole thing by 40 times, [...]

I don't have a direct answer for your question, but I can suggest that
you look into the GMP module; if you've installed Math::BigInt::GMP, you
can do this at the top of your program:

use bigint qw/lib GMP/;

Another option would be to use the Math::BigInt module directly and only
for those values that need the larger sizes.

And yet another option would be to recompile Perl with 64-bit integer
support and not use bigint at all.
 
J

Joe Smith

But - how to know for sure that I don't need bigint - if I know that
all calculations to 2**49 are safe, that would make it sure. But not
sure what is so magic about 2**49!

http://en.wikipedia.org/wiki/Floating_point#A_few_nice_properties

Any integer less than or equal to 2**24 can be exactly represented
in the single precision format, and any integer less than or equal
to 2**53 can be exactly represented in the double precision format.
Furthermore, any reasonable power of 2 times such a number can be
represented. This property is sometimes used in purely integer
applications, to get 53-bit integers on platforms that have double
precision floats but only 32-bit integers.
 
B

Bart Lateur

Sisyphus said:
I think you'll find that integers up to 2 ** 53 are handled correctly by
perl (as the "double" assigns 53 bits to its integer portion).

Perl uses IEEE double (8 bytes) format for the floating points (at
least, most perl ports do) which uses 52 bits for the fraction (AKA
significand, occasionally also called "mantissa"), while I an extra bit
with value 1 is implied at the front. (Its value is always assumed 1,
bar a few exceptions, hence it needn't be stored).

See http://en.wikipedia.org/wiki/IEEE_754 for the technical details.

Thus, to answer the OP's question: you can safely use native perl
numbers for integers up to 2**53. Beyond that range, itll only be able
to store even numbers, up to 2**54, and losing more bits at the low end
for even higher values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top