LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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



Reply
 
Search this Thread
Old 02-04-2008, 07:00 PM   #1
mkrems
Member
 
Registered: Jan 2008
Distribution: Ubuntu
Posts: 31

Rep: Reputation: 15
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?!?
 
Old 02-04-2008, 07:25 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,805

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
Try t=`expr $timestep*$i`.

Also, see the Advanced Bash Scripting Guide, chapter 15 on arthmetic expansion.
 
Old 02-04-2008, 07:28 PM   #3
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,590

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by mkrems View Post
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
 
Old 02-04-2008, 07:33 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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).
 
Old 02-04-2008, 07:51 PM   #5
mkrems
Member
 
Registered: Jan 2008
Distribution: Ubuntu
Posts: 31

Original Poster
Rep: Reputation: 15
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.
 
Old 02-04-2008, 08:10 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
Bash script to create bash script jag7720 Programming 10 09-10-2007 08:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 07:51 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM


All times are GMT -5. The time now is 02:50 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration