How can I compare two floating point numbers in Bash?

I am trying hard to compare two floating point numbers within a Bash script. I have two variables, e.g.

let num1=3.17648e-22
let num2=1.5

Now, I just want do a simple comparison of these two numbers:

st=`echo "$num1 < $num2" | bc`
if [ $st -eq 1]; then
  echo -e "$num1 < $num2"
else
  echo -e "$num1 >= $num2"
fi

Unfortunately, I have some problems with the right treatment of the num1 which can be of the “e-format”.

21 Answers
21

Leave a Comment