Results 1 to 3 of 3
Hi Im wondering how to compare floating point variables. I tried:
Code:
var1=1.1111
var2=1.0001
if [ $var1 -gt $var2 ]
then X=1
else X=0
fi
But bash thinks that the ...
- 10-14-2009 #1Just Joined!
- Join Date
- Oct 2009
- Posts
- 9
Compare Floating Point Variables
Hi Im wondering how to compare floating point variables. I tried:
But bash thinks that the numbers are equal. Any ideas?Code:var1=1.1111 var2=1.0001 if [ $var1 -gt $var2 ] then X=1 else X=0 fi
- 10-14-2009 #2
I think you'll need to use bc.
Floating Point Math in Bash | Linux Journal
- 10-14-2009 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Note that at the end of the comments after the article shells ksh and zsh are mentioned. They have such arithmetic builtin. If you are concerned about portability to bash and other Bourne shell family members or size (of your system), then bc is probably best. Notice also that other scripting languages are mentioned: if you have the time, space, knowledge, and a lot of arithmetic to do, e.g. awk, perl, etc.
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote
