General
cancel
Showing results for 
Search instead for 
Did you mean: 

adding 2 large integer numbers on ia64 gives negative results

SOLVED
Go to solution
moonchild
Regular Advisor

adding 2 large integer numbers on ia64 gives negative results

Hi All,

While adding two large integer numbers using expr command we discovered that the outcome becomes negative. (Small numbers are fine).

This happened on ALL our HP-UX ia64 servers.

Any idea?

Thank you in advance
4 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: adding 2 large integer numbers on ia64 gives negative results

HI:

This is due to 32-bit arithmetic. Use 'bc' or 'adb' or 'awk' or 'perl' to perform your arithmetic. The 'ksh' shell at 11.23 will do 64-bit arithmetic too.

Y=2147483647
# X=$(expr $Y + 1);echo $Xecho
-2147483648

# echo $Y|awk '{printf "%10.0f\n",$0+1}'
2147483648

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: adding 2 large integer numbers on ia64 gives negative results

> This is due to 32-bit arithmetic. [...]

No, this is due to _signed_ arithmetic. The
number of bits merely determines what "large"
is.

When I was young, learning about computers
involved learning how computers count. (That
was back when computers were used mostly for
computation. It's been a while.) I gather
that this is less common now.
Armin Kunaschik
Esteemed Contributor

Re: adding 2 large integer numbers on ia64 gives negative results

For big numbers use external commands like bc:

$ echo $((17179869184+17179869184))
0
$ echo "17179869184+17179869184"|bc
34359738368

My 2 cents,
Armin

PS: Please assign points if you find answers useful!
And now for something completely different...
moonchild
Regular Advisor

Re: adding 2 large integer numbers on ia64 gives negative results

see above