LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-06-2009, 08:07 AM   #1
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Rep: Reputation: 30
bash math...can't divide!!


I try to make a division like so:

VAR1=$(( 28331 / 33009));
echo "Result is: $VAR1";


It should come out with "Result is 6.05"....but no, it comes with:

$ Result is 0

When I do it with smaller numbers like 100 / 3 it works perfectly. What is the problem?
 
Old 07-06-2009, 08:17 AM   #2
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by vous View Post
I try to make a division like so:

VAR1=$(( 28331 / 33009));
echo "Result is: $VAR1";


It should come out with "Result is 6.05"....but no, it comes with:

$ Result is 0

When I do it with smaller numbers like 100 / 3 it works perfectly. What is the problem?
The Result isn't 6.5 at all. You're dividing a number by a number greater than itself so it will be 0.something.

The reason you're getting 0 is that the answer is given as a truncated integer.
 
Old 07-06-2009, 08:22 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
use awk
Code:
awk 'BEGIN{print 28331 / 33009}'
 
Old 07-06-2009, 08:32 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Code:
mherring@Ath:~$ echo $((100/3))
33
Hmmmm----not quite perfect........

There is a utility for bash that does real decimal math, but I don't remember what it is.

This is easier:
Code:
python
>>> 100./3
33.33333333
>>> 2/3
0
>>>2./3
0.66666666
>>>2/3.
0.66666666
Note how easy it tis to tell it you want integer or decimal math.....
 
Old 07-06-2009, 08:33 AM   #5
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Interesting. I didn't know awk could do math. I'll have to remember that one myself.
 
Old 07-06-2009, 08:38 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by GazL View Post
Interesting. I didn't know awk could do math. I'll
have to remember that one myself.
please see my sig on link on learning Gawk.
 
Old 07-06-2009, 08:58 AM   #7
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by ghostdog74 View Post
please see my sig on link on learning Gawk.
Thanks ghost. I'll check those out sometime. I've always tended towards sed rather than awk, so never really gave it much more than a cursory glance.
 
Old 07-06-2009, 10:44 AM   #8
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 492

Rep: Reputation: 50
Maybe easier than gawk, you can invoke "bc" to do floating point arithmetic
 
Old 07-06-2009, 08:46 PM   #9
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,105

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Code:
echo "4 k 100 3 / p" | dc
or, if you like
Code:
answer=$(echo "4 k 100 3 / p" | dc)
yields: 33.3333

You guessed it, "4 k" determines the number of decimal places.

jlinkels
 
Old 07-07-2009, 03:22 AM   #10
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Original Poster
Rep: Reputation: 30
This worked actually, thanks!

echo "scale=5; (54321 / 12345)" | bc
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script Q : how do I force bash to perform the math? kevinyeandel Linux - Newbie 4 02-20-2009 02:35 AM
LXer: Floating Point Math in Bash LXer Syndicated Linux News 0 07-31-2008 03:50 AM
bash simple math (division) question babag Programming 3 04-03-2008 05:24 PM
stuck again (noob bash math question) babag Programming 6 04-25-2005 01:27 AM
bash and math division problem bennethos Programming 5 10-17-2004 01:51 PM


All times are GMT -5. The time now is 09:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration