Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Negative answers for multiplication in K Shell.

Status
Not open for further replies.

Geek8

Programmer
Apr 23, 2003
31
US
Hello,
I am currently writing a script to print a report after collecting a bunch of information on our servers. The report is looking fine except for 1 thing. There are about 3 spots on the report that the number comes out negative. I tried running the command on the command line and I still get a negative anwer. This is the command in both the script and via command line that I am using:

expr 900000 \* 2575

the answer I am getting is this:

-1977467296

instead of: 2317500000

Can you help as to why I am getting this? How can I get it to be positive and the correct answer?

Thanks so much for helping me with this trivial question. I am sure I am missing something obvious.

Geek

P.s. This is on a Linux box. (not sure if that makes a difference)
 
works fine for me:
$ expr 900000 \* 2575
2317500000

don't know why you're seeing negatives.
try another way:
$ echo '900000 * 2575' | bc
2317500000



vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad,
Thanks for the support. I don't know why I am getting the negative with the first way, but when I try the alternative way you mentioned, it worked fine. What does it mean when you pipe it to &quot;bc&quot;. I understand it has to do with arithmetic conversions, but I don't fully understand the logic behind &quot;bc&quot;.

By the way, thanks so much for the help.

Geek.
 
Works fine for me in SUN:

$ expr 900000 \* 2575
2317500000

But NOT in Linux:

$ expr 900000 \* 2575
-1977467296

Could there be a maximum integer size???

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
For systems with a 32-bit word size, the maximum integer is 2147483647. If you exceed it, you overwrite the sign bit and get a negative result.
 
Geek, [sorry, nothing personal]
as others have explained you might be hitting the limits.

'bc' stands for 'Basic Calculator' - you feed it a 'math expression' and it does the calculation. Look into 'man bc' for the details.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top