1834742 Members
2987 Online
110070 Solutions
New Discussion

Shell script problem

 
bibith dathan
Advisor

Shell script problem

Hi All

can any one tell is their any limitation in lengh of numeric value that expr function can operate ?

means ,in shell script if i have set a value Y=100000000000( more than 10 digit) and Z =100000000000 ( more than 10 digit).
and do "expr $Y + $Z" i should get 200000000000, but i am getting some negative value . If i use 9 digit numbers for Y and Z i will get correct output .


Please tell me why it is happeing and how to over come the same

Bibith Dathan
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor

Re: Shell script problem

Hi:

The shell is using 32-bit arithmetic and you are seeing an overflow. See our discussion here:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1273761

Regards!

...JRF...
Laurent Menase
Honored Contributor

Re: Shell script problem

Hi Bibith,

If you want to use large numbers you need to use bc as coprocess.
i.e.:
a=$(echo "$Z+$Y" |bc)
or ksh coprecess

bc |& # start bc as coprocess
exec 3>&p 4<&p
echo "$Z+$Y" >&3
read result <&4
echo $result

echo ".+$Z" >&3
read result <&4
echo $result

echo ".*$Z" >&3
read result <&4
echo $result
exec 3<&- 4<&- # to exit the coprocess

bc is using unlimited arithmetic.
if yu want floating points or mathematical functions use "bc -l"