How to compare two floating point numbers in Bash?

前端 未结 17 2165
無奈伤痛
無奈伤痛 2020-11-22 02:17

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

let num1=3.17648e-22
let num2=1.5

No

17条回答
  •  误落风尘
    2020-11-22 03:02

    Using bashj (https://sourceforge.net/projects/bashj/ ), a bash mutant with java support, you just write (and it IS easy to read):

    #!/usr/bin/bashj
    
    #!java
    static int doubleCompare(double a,double b) {return((a>b) ? 1 : (a $num2" ; fi
    if [ $comp == -1 ] ; then echo "$num2 > $num1" ; fi
    

    Of course bashj bash/java hybridation offers much more...

提交回复
热议问题