Re: integer limit
- From: Icarus Sparry <usenet@xxxxxxxxxxxxxxxxx>
- Date: 13 Dec 2007 17:42:58 GMT
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.
.
- References:
- integer limit
- From: trbosjek
- integer limit
- Prev by Date: Re: How to deal with binary file?
- Next by Date: Re: Shell equivalent of continue|break ???
- Previous by thread: Re: integer limit
- Next by thread: Re: integer limit
- Index(es):
Relevant Pages
|