LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-22-2008, 10:45 AM   #1
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
Quick and basic math question


I want to evaluate a simple math equation using bash, and would like to have returned a floating number with an accuracy to the tenth i.e 5.5

the code I have currently is:

myEXP200=$[(25*($reflength-11+1))/$reflength]

where $reflength are already stored values, I was looking at some code and saw that they piped the math equation to |bc -l , but thats not really what I want.
 
Old 09-22-2008, 11:09 AM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 69
bash doesn't support float-point operations. Only bc does, that's why usually all float-point operations are piped to bc.
 
Old 09-22-2008, 11:17 AM   #3
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
That could be why . So I looked over it some more and I read up on scale and length and believe I need to use scale but not sure how to do it in the form I want. I am guessing I could do the following:

myEXP200="scale=1;$[(25*($reflength-11+1))/$reflength]"|bc

Would that work?
 
Old 09-22-2008, 11:52 AM   #4
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
I just tried:

myEXP200=$("scale=1;(25*($reflength-11+1))/$reflength"|bc)

which didnt work. On the bc man page it says I can do scale ( expression ) but I have yet to find an example using this syntax. I believe that the scale=1 is an environmental element so I should take it out of my equation but what about the bc? Should it be inside or out of the $() ?

Best,

Weldon
 
Old 09-22-2008, 11:55 AM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by bioinformatics_guy View Post
I just tried:

myEXP200=$("scale=1;(25*($reflength-11+1))/$reflength"|bc)

which didnt work. On the bc man page it says I can do scale ( expression ) but I have yet to find an example using this syntax. I believe that the scale=1 is an environmental element so I should take it out of my equation but what about the bc? Should it be inside or out of the $() ?

Best,

Weldon
Try:
Code:
myEXP=$(echo "scale=5; (25*($reflength-11+1))/$reflength" | bc -l)
Scale is synonymous with number of decimal points. -l defines the standard maths library; may not be necessary, but I always use it, just in case.

Last edited by pwc101; 09-22-2008 at 11:56 AM. Reason: clarification
 
Old 09-22-2008, 12:03 PM   #6
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
Brilliant. It worked perfectly. I did look at the -l flag but when I used it stand alone with a math function, it just gave me a bunch of zeros after the decimal -- not sure what its set scale is.

Do I have to use the echo? And if so, why? Does it just basically tell the shell to evaulate the statement in " " and save the output as myEXP?

Best,

Weldon
 
Old 09-22-2008, 12:11 PM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by bioinformatics_guy View Post
Brilliant. It worked perfectly. I did look at the -l flag but when I used it stand alone with a math function, it just gave me a bunch of zeros after the decimal -- not sure what its set scale is.

Do I have to use the echo? And if so, why? Does it just basically tell the shell to evaulate the statement in " " and save the output as myEXP?

Best,

Weldon
bc is an interactive calculator (type bc on its own in a terminal and you'll get a command line). You can enter the statement in the $() construct, and you'll get an answer printed out on the screen
Code:
<your_normal_prompt> bc
<bc_command_prompt> scale=4
<bc_command_prompt> 2/3
.6666
<bc_command_prompt> 2/4
.5000
<bc_command_prompt> quit
The $() construct means "evaluate all the commands contained in the brackets". Here, we're assigning that result to the variable myEXP.

So, what we're doing here is sending the information we want evaluated to bc through standard input, but we can't just type it without the echo, or the shell will try and execute the command scale (since it's the first one in the $() construct). Echoing the commands means they get sent to bc for it to read as though they were bc commands.

Hope this helps

Last edited by pwc101; 09-22-2008 at 12:12 PM. Reason: more clarification
 
Old 09-23-2008, 06:09 AM   #8
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
Math Questions update

pwc1,

A most excellent description I understand know why you need the echo.

Here's a simple one for you

var1=2
var2=5

var3= var1 + var2

Why does,

var3=$($var1 + $var2)
or
var3=$(($var1 + $var2))
or
var3=$[$var1 + $var2]

not work? I know for bash if you want to evaluate a math function, you give it the form $[] with numbers in the brackets, but what if they are stored variables?
 
Old 09-23-2008, 06:14 AM   #9
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
I should also point out that var1 and var2 in my program are decimals, to which I am returned this error from this line:
myTOTEXP=$(($myEXP200 + $myEXP500))

error:
./command_assem.sh: line 68: 24.9 + .9: syntax error in expression (error token is ".9 + .9")

where $myEXP200 is 24.9 and $myEXP500 is .9
 
Old 09-23-2008, 06:18 AM   #10
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
Figured it out, just had to use the same syntax as above (pipe to bc and flag scale). I didn't realise that all floating point numbers had to go through bc
 
Old 09-23-2008, 06:35 AM   #11
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by bioinformatics_guy View Post
var1=2
var2=5

var3= var1 + var2

Why does,

var3=$($var1 + $var2)
This is trying to execute the value of the variables as commands. So what bash sees is:
Code:
var3=$(2 + 5)
Neither 2 nor 5 are commands you can type into a bash terminal, so it fails.
Quote:
Originally Posted by bioinformatics_guy View Post
or
var3=$(($var1 + $var2))
This will work; the syntax for bash's internal arithmetic is $((1+2)), which will give you 3 if you type that verbatim into a bash terminal.
Quote:
Originally Posted by bioinformatics_guy View Post
or
var3=$[$var1 + $var2]
This is synonymous with the example above.
Quote:
Originally Posted by bioinformatics_guy View Post
I know for bash if you want to evaluate a math function, you give it the form $[] with numbers in the brackets, but what if they are stored variables?
You can use either $(()) or $[] to evaluate simple arithmetic in bash, and both accept variables in the constructs. So:
Code:
prompt> var1=10
prompt> var2=50
prompt> var3=$((var2-var1))
prompt> echo $var3
40
prompt>
Quote:
Originally Posted by bioinformatics_guy
I should also point out that var1 and var2 in my program are decimals, to which I am returned this error from this line:
myTOTEXP=$(($myEXP200 + $myEXP500))

error:
./command_assem.sh: line 68: 24.9 + .9: syntax error in expression (error token is ".9 + .9")

where $myEXP200 is 24.9 and $myEXP500 is .9
The problem here is not that the values are stored in variables (bash has translated them from variables to the variable's content as you can see because the error knows these are numbers), the problem is they are decimals; bash cannot do floating point arithmetic - it can only handle integer values. That's why there's bc.

If you want some simple examples and explanations of this sort of stuff, see "Arithmetic evaluation": http://tldp.org/HOWTO/Bash-Prog-Intr...10.html#ss10.2

To see how bash is interpreting your script, it's useful to put "set -x" at the start of the script, after the shebang (#!/bin/bash), and then run your code as normal. You can turn it off by just commenting the set -x line.

Last edited by pwc101; 09-23-2008 at 06:38 AM.
 
  


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
math trick question xeon123 General 6 03-23-2007 07:54 AM
Help with math question nadroj General 28 01-20-2007 03:00 AM
quick basic compiler for linux bsengland Linux - General 1 05-17-2004 03:40 PM
quick noob visual basic question libster Programming 8 10-29-2003 09:52 AM
Quick Easy Basic Question RaheimSG Linux - Hardware 5 09-12-2002 08:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:10 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration