PDA

View Full Version : Limit on Numbers



achuth
December 18th, 2011, 06:45 AM
Is there any limit for the size of the number that I can use in an arithmetic operation in a shell script.
The following multiplication operation on very large numbers gives a negative sign also.


Enter two numbers
3216549876545216
65478987931354666

Product is -3449254379524228224

I am using ubuntu 11.10.
If there exists a limit on the numbers, then what is it?

Bachstelze
December 18th, 2011, 06:54 AM
Depends on the shell. Bash uses 64-bit integers (yes, even on 32-bit systems), so the range of values is -2^63 to 2^63-1.

EDIT: POSIX mandates that the precision of integers in sh be at least that of a C long, but allows higher precision.

MadCow108
December 18th, 2011, 05:56 PM
for arbitrary precision math use e.g. the bc program

res="`echo "3216549876545216*65478987931354666" | bc`"
echo $res
210616430546904539318302881577856

the output of this is a string so you do not have the integer limit problem (but also can't use it in bash internal arithmetic anymore)