Bash Floating Number Variables Comparision


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash Floating Number Variables Comparision
# 1  
Old 07-08-2013
Bash Floating Number Variables Comparision

I am trying to compare the floating number variables but i am receiving an error, can you please help what is wrong. Thank you.

Code:
#!/bin/bash

var1=100.25
var2=100.25

if (( $var1 == $var2 )); then
        echo "Matching"
else
        echo "Not Matching"
fi

Error:
Code:
./number.sh: line 6: ((: 100.25 == 100.25 : syntax error: invalid arithmetic operator (error token is ".25 == 100.25 ")
Not Matching

# 2  
Old 07-08-2013
Bash does not support floating point arithmetic. Use ksh93 instead, if possible.
# 3  
Old 07-08-2013
Interesting so there is no other way in bash shell to achieve this other than using korn shell ?
# 4  
Old 07-08-2013
There are many awkward ways to do so. For instance:

Code:
if awk -v VAR1="$VAR1" -v VAR2="$VAR2" 'BEGIN { exit(!(VAR1 == VAR2)); }'
then
...
fi

But ksh93 supports it directly, without external utilities.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 07-08-2013
Code:
export var1="100.25" && export var2="100.25"

[ $var1 == $var2 ] && echo "match" || echo "no match"
match


export var1="100.55" && export var2="100.25"

[ $var1 == $var2 ] && echo "match" || echo "no match"
no match

This User Gave Thanks to in2nix4life For This Post:
# 6  
Old 07-08-2013
Or you can use bc - An arbitrary precision calculator language:
Code:
if [ $( echo "$var1 == $var2" | bc -l ) -eq 1 ]
then
        echo "Matching"
else
        echo "Not Matching"
fi

This User Gave Thanks to Yoda For This Post:
# 7  
Old 07-08-2013
Quote:
Originally Posted by in2nix4life
Code:
export var1="100.25" && export var2="100.25"

[ $var1 == $var2 ] && echo "match" || echo "no match"
match


export var1="100.55" && export var2="100.25"

[ $var1 == $var2 ] && echo "match" || echo "no match"
no match

This will not work when comparing 100.25 to 100.250
This User Gave Thanks to Corona688 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to print the smallest floating point number in a row that is not 0

Hello, I have often found bash to be difficult when it comes to floating point numbers. I have data with rows of tab delimited floating point numbers. I need to find the smallest number in each row that is not 0.0. Numbers can be negative and they do not come in any particular order for a given... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

2. Shell Programming and Scripting

Bash script accepting variables in valid number format

Hi Experts I would like to ask if there is a way to validate if the variable passed is in this kind of sample format "06-10" or "10-01". It was really a challenge to me on how to start and echnically the "6-10" stands for "June 10" and "10-01" stands as "October 1", overall it needs to have ... (3 Replies)
Discussion started by: ersan-poguita
3 Replies

3. Shell Programming and Scripting

Convert floating point to a number

Hello Guys, I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value #!/bin/bash #... (9 Replies)
Discussion started by: skatpally
9 Replies

4. Shell Programming and Scripting

How to compare floating variables , integer value expected?

I am running some commands and I am trying to get an output into a variable. I am having problem when I try to put that value in while loop, it says integer value expected. What's the best way to accomplish this remaining=$(symclone -sid XXX -f Clone_test query | grep MB | awk '{print... (1 Reply)
Discussion started by: rajsan
1 Replies

5. Shell Programming and Scripting

[BASH] Regex for floating point number

Hey again, I have a basic regex that tests if a number is a float. Thank you. (5 Replies)
Discussion started by: whyte_rhyno
5 Replies

6. Shell Programming and Scripting

floating point variables korn shell

Hi I'm not using Korn93 but want to use floating point variable. Is there any solution to do that ? thx for help. ---------- Post updated at 02:28 PM ---------- Previous update was at 12:38 PM ---------- I have the following peace of code: for n in `cat log.January.1.array` do ... (3 Replies)
Discussion started by: presul
3 Replies

7. Shell Programming and Scripting

Defining Dynamic Number of Variables in a Bash Script

Code: $ cat test.bash #!/bin/bash job=$1 steps=$2 num=$(echo "$@" | wc -w) Example Submission: $ ./test.bash BS01 3 1 2 3 What: (2 Replies)
Discussion started by: mkastin
2 Replies

8. Shell Programming and Scripting

HELP: compare floating point variables??

Hi All, I got this script that pulls the Amps value from our RPC's , I basiclly want to compare the valued with a "limit" value -- if the numbers match or are greater than the definded value ...do something. My problem is I cant seem to figure out how to compare floating points... here is... (1 Reply)
Discussion started by: zeekblack
1 Replies

9. Shell Programming and Scripting

using bc with floating point number in files

Hi, I' using bash and I would like to use "bc" to compute the ratio of of two numbers and assign the ratio to a variable. The numbers are in a file, e.g. 196.304492 615.348986 Any idea how to do it? N.B. I cannot change the file to have 196.304492 / 615.348986 as the file is produced by... (14 Replies)
Discussion started by: f_o_555
14 Replies

10. UNIX for Dummies Questions & Answers

Number comparision in AWK

Hi, I have a file like this. "2006","10",25,"U","1129","32","C",0,0,0,0,0,0,0,0,0,0,0,0,352,16,4,0,0,0,0,0,"80",,1 "2006","11",25,"U","1148","32","C",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"80",,2 "2006","14",25,"U","1149","10","C",0,0,0,0,0,0,0,0,0,0,0,0,560,12,0,0,0,0,0,0,"80",,3... (1 Reply)
Discussion started by: vskr72
1 Replies
Login or Register to Ask a Question