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
 
LinkBack Search this Thread
Old 02-04-2008, 06: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, 06:25 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,697

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

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

Rep: Reputation: 134Reputation: 134
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, 06:33 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,366

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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, 06: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, 07:10 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,366

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 04:11 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM


All times are GMT -5. The time now is 11:57 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