Operating System - HP-UX
1835166 Members
2380 Online
110077 Solutions
New Discussion

Re: how to work with long int expressions in shell scripts

 
SOLVED
Go to solution
madhavabk
Regular Advisor

how to work with long int expressions in shell scripts

Hi All,

I am adding two large integer numbers and i am getting -ve output as expr will use 32 bit representation.

but do we have any command or tool that will evaluate expressions that contain larget ints.

Thanks,
Madhava
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: how to work with long int expressions in shell scripts

You can use bc to do calculations both integet and floating-point of unlimited precision.

VAL=$(echo "(300 * 8) + 5" | bc)
echo "Result = ${VAL}"

Man bc for details.
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: how to work with long int expressions in shell scripts

Within ksh or posix-sh you can use the typeset command to specify a integer type (long) and then use the "let" command to do arithmetic.

HTH

-- Rod Hills
There be dragons...
Peter Nikitka
Honored Contributor

Re: how to work with long int expressions in shell scripts

Hi,

if mean, how to enable 64bit integer arithmetic in the shell: you can't (as far as I know).

You'll have to use bc, dc or perl to do your computation.
If you set a shell variable to such a value, the result would be a string - so you can compare for equalness.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
A. Clay Stephenson
Acclaimed Contributor

Re: how to work with long int expressions in shell scripts

It would be better to do all the comparisons in bc as well since actual numeric comparisons can be done (integer or floating-point) without having to pad values as one would have to do in the shell.
If it ain't broke, I can fix that.
madhavabk
Regular Advisor

Re: how to work with long int expressions in shell scripts

Hi All,
Thanks for participation in my problem.
The problem is getting solved with bc command.

Thanks,
Madhava