Re: arithmetic operations problem (104 Views)
Reply
Respected Contributor
Paul_481
Posts: 253
Registered: ‎09-08-2004
Message 1 of 11 (104 Views)
Accepted Solution

arithmetic operations problem

Hi,

This is using POSIX shell.

I.

#((x=1000000000+1));echo $x
1000000001

II.

#((x=10000000000+1));echo $x
1410065409

I notice that POSIX is not capable of computing more that 10 digit. Is there a way around this? Any other alternative?

Regards,
Paul
Please use plain text.
Honored Contributor
Eric Antunes
Posts: 1,931
Registered: ‎06-15-2003
Message 2 of 11 (104 Views)

Re: arithmetic operations problem

Hi Paul,

Try this:

((x=500000000+1));((y=500000000));echo $x;echo $y;((z=$y+$x));echo $z

Best Regards,

Eric Antunes
Each and every day is a good day to learn.
Please use plain text.
Respected Contributor
Paul_481
Posts: 253
Registered: ‎09-08-2004
Message 3 of 11 (104 Views)

Re: arithmetic operations problem

Hi Eric,

((x=500000000+1));((y=500000000));echo $x;echo $y;((z=$y+$x));echo $z

From you example the value of x is still less than 10.

What I need is for a way to compute 11 digit integer.

Regards,
Paul
Please use plain text.
Valued Contributor
CAS_2
Posts: 186
Registered: ‎04-07-2005
Message 4 of 11 (104 Views)

Re: arithmetic operations problem

Arithmetic operations are 32 bit wide.

integers range are: from -2^31 (-2147483648) to +2^31-1 (+2147483647)

Following commands show only low 32-bit are used:

(( x=2147483647)); (( x=x+1 )); echo $x
-2147483648

I should use "bc" command (or "dc" utility) instead.
Please use plain text.
Honored Contributor
erics_1
Posts: 445
Registered: ‎02-22-2001
Message 5 of 11 (104 Views)

Re: arithmetic operations problem

Paul,

Take a look at Knowledge Base Doc. ID 4000099480 . This explains workarounds to 32 bit integer limitations.

Regards,
Eric
Please use plain text.
Valued Contributor
CAS_2
Posts: 186
Registered: ‎04-07-2005
Message 6 of 11 (104 Views)

Re: arithmetic operations problem

Using bc command:

echo 10000000000+1 | bc | read x
echo $x

Note that the 32-bit arithmetic is also applicable to INTEGER shell variables defined by means of 'typeset -i' (or 'integer' alias). Therefore, the shell variables used to handle this big values HAVE TO BE NON-INTEGER.

The following examples:

typeset -i x
echo 10000000000+1 | bc | read x
echo $x

typeset +i x
echo 10000000000+1 | bc | read x
echo $x


wil print:

1410065409
10000000001

Please use plain text.
Honored Contributor
Eric Antunes
Posts: 1,931
Registered: ‎06-15-2003
Message 7 of 11 (104 Views)

Re: arithmetic operations problem

Sorry Paul, I missed one zero...

I also have this 32 bits maximum:

2^0+2^1+...+2^31=2^32-2^0=4294967296-1=4294967295 but since we also need to use negative numbers, our limit is as CAS said...

Best Regards,

Eric Antunes
Each and every day is a good day to learn.
Please use plain text.
Respected Contributor
Paul_481
Posts: 253
Registered: ‎09-08-2004
Message 8 of 11 (104 Views)

Re: arithmetic operations problem

Hi all,

I have problem storing the output of bc into variable. I need to get to correct results and process it further.

cscpcc@idmp005 [/home/cscpcc]
#print "2124582832+256000000"|bc
2380582832

cscpcc@idmp005 [/home/cscpcc]
#print "2124582832+256000000"|bc|read x|echo $x
-1914384464


Regards,
Paul
Please use plain text.
Honored Contributor
Stephen Keane
Posts: 1,155
Registered: ‎11-24-2004
Message 9 of 11 (104 Views)

Re: arithmetic operations problem

You probably meant

# print "2124582832+256000000"|bc|read x;echo $x

(i.e. lose the last pipe)

Works for me!




Please use plain text.
Honored Contributor
Stephen Keane
Posts: 1,155
Registered: ‎11-24-2004
Message 10 of 11 (104 Views)

Re: arithmetic operations problem

(well apart from the 32-bit integer rounding bit!)
Please use plain text.
Respected Contributor
Paul_481
Posts: 253
Registered: ‎09-08-2004
Message 11 of 11 (104 Views)

Re: arithmetic operations problem

Hello All,

Well I finally able to make my script work. Actually the script is for disk utilization computation from bdf output. My script is working ok until we upgrade our disk to EVA when we add a couple of Terbytes disk.

Any way, I used "bc" as suggested and it works.

Thanks everyone for the reply.

Regards,
Paul
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation