in reply to How to portably determine integer limits?

The question itself is actually questionable. As Perl is not a language with strong type, the upper or lower limits of integer themselves, do not really put much restriction on the things one "typically" (another typically ;-) does.

Again because Perl is not a strong type language, the plan to use ++/-- ONLY to hit the integer limits are not doable, unless you sneak some other stuffs in.

Integer limits produce much less worries in Perl, than they do in other languages. At least the worry is from a totally different sense.

  • Comment on Re: How to portably determine integer limits?

Replies are listed 'Best First'.
Re: Re: How to portably determine integer limits?
by bart (Canon) on Oct 30, 2003 at 02:40 UTC
    There are reasons to worry, most notable with regards to the bitwise operands, &, |, ^, ~ on one hand, << and >> on the other. And % also has a limit on the range for which it works reliable — very system dependent, I've had a perl 5.005 for which modulo calculations with a number > 2**32 always returned zero, but on my newer port, I can go up to 2**52 or 2**53 (I don't recall exactly), the mantissa part of an IEEE double float, of 64 bits.

    This meaningfulness does give a clue on how to efficiently test how large the range can be: I'm quite convinced the limit is always closely related to a power of 2 so one could shift a number to the left, until the result differs from the number times 2, in floating point calculation.

    my $i = 0; my $n = my $m = 1; while($n == $m) { $n <<= 1; $m *= 2; $i++; } print "different for $m (2**$i)\n";
    I get:
    different for 4294967296 (2**32)
    
    What precisely this implies, I leave as an exercise for the reader. :)
      Just in case you were not writing out your thoughts so the reader can learn...

      The limits are of a power of 2.. 'cause it's binary. For every extra bit you tack on, you are adding to a position representing 2^(N+1) position, after the most significant bit. Adding to a 1 bit number makes it 2 bits and a max of 3 (4-1). Adding another bit makes it a 3 bit number, and givig it a max of 7 (8-1), It's minus one just 'cause that's how binary works. 2^n would be 1000....

      Same w/ char's (2^8 in ascii), unicode, (2^8 * 2 == 2^16)..

      With signed numbers, it's a bit different.. since your most significant bit, teh bit with teh largest 2^n position would be, is just a bit saying if the number is negative or not. But for more reference on how that works, look up 2's compliment on google. :) I 'm going to bed.

      Play that funky music white boy..