in reply to Highest scalar = ???

The highest integer is easy to get:

$ perl -e 'print ~0' 4294967295

Perl can count higher by using floating point, but that's the last exact integer you'll be able to represent without Math::BigInt.

-sam

Replies are listed 'Best First'.
Re^2: Highest scalar = ???
by ikegami (Patriarch) on Oct 01, 2007 at 18:37 UTC

    That's not true. Floating points can represent a large range of integers exactly, and that range for a double is bigger than the range of an unsigned 32 bit int.

    Type Min Max -------------------------------- -------- ----------- signed n-bit int -2^(n-1) (2^(n-1))-1 unsigned n-bit int 0 (2^n)-1 IEEE float with n-bit mantissa -2^(n+1) 2^(n+1)

    Double has n=52, so 4294967295 isn't the maximum that can be held without Math::BigInt.

    Type Min Max -------------------------------- ----------------- ---------------- signed 32-bit integer -2147483648 2147483647 unsigned 32-bit integer 0 4294967295 IEEE float with 52-bit mantissa -9007199254740992 9007199254740992

    Demo:

    $x = 2**(52+1); # For doubles, format "%.17e" will tell you if # the number is stored with no loss of precision # because log10(2**(n+1)) < 17. printf("%.17e\n\n", $x); $x -= 5; for (1..10) { printf("%f\n", $x); $x++; }
    9.00719925474099200e+015 <-- That's exact. 9007199254740987.000000 9007199254740988.000000 9007199254740989.000000 9007199254740990.000000 9007199254740991.000000 9007199254740992.000000 9007199254740992.000000 <-- Least significant 9007199254740992.000000 bit dropped. 9007199254740992.000000 9007199254740992.000000
      I suppose that depends on your definition of "integer". I was never much of a math student, so I use the comp. sci definition. So does Perl, sometimes:

      $x = 2**(52+1); for (1 .. $x) { # not gonna get here! }

      Produces:

         Range iterator outside integer range at foo.pl line 2.

      Guess that's not an integer after all!

      -sam

        I was using your own definition. You said ~0 was the highest integer that can be represented precisely without using Math::BigInt. That's wrong, and I explained why.

        Your original post if still wrong by your new definition. If you want to the highest integer that can be used in a range, both your answer of ~0 and your suggestion to use Math::BigInt for larger integers than ~0 are wrong!!

        I have already mentioned in this thread that ranges require signed integers, so ~0 >> 1 (2147483647) would be the answer.

        Update: I originalyl had ~-(~0+1) instead of ~0 >> 1, but that won't work if the system's floats have a smaller range than the integers.