Languages and Scripting
Showing results for
Do you mean

## arithmetic operations problem

SOLVED
Respected Contributor

# 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
10 REPLIES
Honored Contributor [Founder]

## 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.
Respected Contributor

## 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
Valued Contributor [Founder]

## 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.
Honored Contributor [Founder]

## Re: arithmetic operations problem

Paul,

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

Regards,
Eric
Valued Contributor [Founder]

## 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

Honored Contributor [Founder]

## 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.
Respected Contributor

## 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]
-1914384464

Regards,
Paul
Honored Contributor

## Re: arithmetic operations problem

You probably meant

(i.e. lose the last pipe)

Works for me!

Honored Contributor

## Re: arithmetic operations problem

(well apart from the 32-bit integer rounding bit!)
Highlighted
Respected Contributor

## 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.