|
|
 |
 |
 |
 |
Perl Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
What is MAXINT, or when to use bigint?
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!
<bwooste @gmail.com> wrote in message news:1180497176.687453.79890@q69g2000hsb.googlegroups.com... . . > 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
On 05/29/2007 10:52 PM, bwooste@gmail.com wrote: > [...] > 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.
bwooste @gmail.com wrote: > 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.
On 29 May 2007 20:52:56 -0700, "bwooste@gmail.com" <bwooste @gmail.com> wrote: >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).
I don't know if the following recent thread @ PM is of interest to you, but it may: http://perlmonks.org/?node_id=617840 Michele -- {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB=' .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_, 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Sisyphus wrote: >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. -- Bart.
|
 |
 |
 |
 |
|