Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-04-2008, 06:00 PM
|
#1
|
|
Member
Registered: Jan 2008
Distribution: Ubuntu
Posts: 31
Rep:
|
Multiply floats in bash script
I have a bash script with the following line where $timestep is a decimal number.
t=$timestep*$i
echo $t gives the value "0.125*2" for example instead of "0.250".
How do I change this?!?
|
|
|
|
02-04-2008, 06:25 PM
|
#2
|
|
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908
|
Try t=`expr $timestep*$i`.
Also, see the Advanced Bash Scripting Guide, chapter 15 on arthmetic expansion.
|
|
|
|
02-04-2008, 06:28 PM
|
#3
|
|
LQ Guru
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,422
|
Quote:
Originally Posted by mkrems
I have a bash script with the following line where $timestep is a decimal number.
t=$timestep*$i
echo $t gives the value "0.125*2" for example instead of "0.250".
How do I change this?!?
|
bash does not have floating point arithmetic. Any variable with a decimal point in it is treated as a string. So bash processes t=$timestep*$i as the concatenation of three strings. The strings are:
$timestep=0.125
*
$i=2
You are not going to get bash to use 0.125 as a number in an arithmetic expression.
--------------------
Steve Stites
|
|
|
|
02-04-2008, 06:33 PM
|
#4
|
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
To do floating point calculations in a shell script you can invoke awk, e.g.
Code:
timestep=0.125
i=2
t=$(echo $timestep $i | awk '{printf "%4.3f\n",$1*$2}'
or you can use bc calculations (see man bc for details).
|
|
|
|
02-04-2008, 06:51 PM
|
#5
|
|
Member
Registered: Jan 2008
Distribution: Ubuntu
Posts: 31
Original Poster
Rep:
|
hey guys, thanks for the help but actually i figured it out to be:
t=$(expr $timestep*$i | bc)
i have not tried the awk method, but the other ones did not work for me.
|
|
|
|
02-04-2008, 07:10 PM
|
#6
|
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
It should be
Code:
t=$(echo $timestep*$i | bc)
expr is a command who evaluates arithmetic expressions, while you have to simply echo the arithmetic expression to the bc calculator.
|
|
|
|
All times are GMT -5. The time now is 04:52 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|