Re: integer limit



On Thu, 13 Dec 2007 03:05:42 -0800, trbosjek wrote:

$ if [ 139586437119 -ge 0 ];then echo true;else echo false;fi true
$
$
$ if [ 139586437120 -ge 0 ];then echo true;else echo false;fi false

I found this limit by manual experimet. Is there a propper way to learn
of the limits such as this for a particular system? In this case it is
HP-UX.
And googling for 139586437119 produced zero results. Can anyone explain?

I think I can explain.

Consider the number 1395864371 (the first 10 digits of your numbers).
To convert the first 11 digits, we take the result of converting the
first 10, multiply by 10 (as this is a decimal number) and add the value
of the 11th, i.e. 13958643711 is 1395864371*10+1.

If we do this in 32 bit registers, and do not worry about overflow, we
get 1073741823, as 13958643710 is more than 2^32.

We then calculate 10 times this, and add 9, to get 2147483647, again
because
it overflows the 32 bit register.

Converting 139586437120 in the same way yields 2147483648.

These days it is reasonable to assume that CPUs will use a 2's complement
representation for numbers (there are other ways, but almost nothing uses
them). The way that this works is that if the most significant bit is set
then the number is considered negative, otherwise it is considered non-
negative. Looking at the bit patterns for the numbers
2147483647=01111111111111111111111111111111
2147483648=10000000000000000000000000000000

so in a twos complement machine 2147483648 is negative, but 2147483647
is positive, which explains your results.

I am not surprised that Google turns up nothing for 139586437119. I bet
it will turn up lots of results for 2147483647 and 2147483648.

In C you can look at MAX_INT from limits.h.

.



Relevant Pages

  • Re: Saturation
    ... (with DATAREG_X beeing 32bit data registers). ... Compute the absolute value between DATAREG_B and DATAREG_C ... sense here since in my option an overflow can never happen. ... But NEG of ffff_ffffh will produce a +1. ...
    (alt.lang.asm)
  • Re: Integer arithmetic, multiplication overflow
    ... If the result register of an integer multiplication instruction is twice as wide as the factor registers then overflow will never occur. ... The second operand D2represents the second factor, a 32-bit signed integer in main storage. ...
    (sci.math)
  • Exploiting new IE Object Type Overflow
    ... Object Type) (ESP doesn't seem to be overwritten, but EDI is). ... "This allows us to take control of key registers so as to run code that we ... which will be available at the EDX register. ... is different from a regular stack overflow as placing the address of a JMP ...
    (Vuln-Dev)
  • Re: problem with very small numbers - g95 versus gfortran
    ... I have worried about writing a replacement for missing intrinsics ... overflow even though the final result should be a model number. ... overflow for the single precision version because the single ... computation is more likely to be carried out in SSE registers ...
    (comp.lang.fortran)
  • Re: problem with very small numbers - g95 versus gfortran
    ... I have worried about writing a replacement for missing intrinsics ... overflow even though the final result should be a model number. ... overflow for the single precision version because the single ... computation is more likely to be carried out in SSE registers ...
    (comp.lang.fortran)