Operating System - HP-UX
1829576 Members
2948 Online
109992 Solutions
New Discussion

Re: computing limitation error - long and double

 
deCG
Advisor

computing limitation error - long and double

I am writing a shell script to compute really long numbers. But due to limitation, it is giving out wrong numbers.

I tried 'double' and 'long' as variable type but none of them are valid type.

This is output.

2847744950/3234344960 = 5295336 (huh??)

Does anybody have any clue???

Thanks a bunch.

Yong
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: computing limitation error - long and double

Hi:

The shell limits you to 32-bit arithmetic. Use something like Perl or 'bc'. For example:

# perl -le 'print +(2**52)'
4503599627370496

# echo 2^52|bc
4503599627370496

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: computing limitation error - long and double

>JRF: The shell limits you to 32-bit arithmetic.

ksh was changed in 11.23 to use 64 bit ints. Unfortunately the posix shell wasn't changed.

You can use awk to do calculations in double.
And gdb to do calculations in long double.
(gdb) p (long double)2847744950/3234344960
$2 = 0.88047038433401983194767202568275215
deCG
Advisor

Re: computing limitation error - long and double

You guys are great... I will get back to you when I try at work...

Thanks
deCG
Advisor

Re: computing limitation error - long and double

.... ....
deCG
Advisor

Re: computing limitation error - long and double

thanks. it works now...