How to compare two floating point numbers in Bash?

前端 未结 17 2155
無奈伤痛
無奈伤痛 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 02:57

    This script may help where I'm checking if installed grails version is greater than minimum required. Hope it helps.

    #!/bin/bash                                                                                         
    
    min=1.4                                                                                             
    current=`echo $(grails --version | head -n 2 | awk '{print $NF}' | cut -c 1-3)`                         
    
    if [ 1 -eq `echo "${current} < ${min}" | bc` ]                                                          
    then                                                                                                
        echo "yo, you have older version of grails."                                                   
    else                                                                                                                                                                                                                       
        echo "Hurray, you have the latest version" 
    fi
    
    0 讨论(0)
  • 2020-11-22 02:59

    I was posting this as an answer to https://stackoverflow.com/a/56415379/1745001 when it got closed as a dup of this question so here it is as it applies here too:

    For simplicity and clarity just use awk for the calculations as it's a standard UNIX tool and so just as likely to be present as bc and much easier to work with syntactically.

    For this question:

    $ cat tst.sh
    #!/bin/bash
    
    num1=3.17648e-22
    num2=1.5
    
    awk -v num1="$num1" -v num2="$num2" '
    BEGIN {
        print "num1", (num1 < num2 ? "<" : ">="), "num2"
    }
    '
    
    $ ./tst.sh
    num1 < num2
    

    and for that other question that was closed as a dup of this one:

    $ cat tst.sh
    #!/bin/bash
    
    read -p "Operator: " operator
    read -p "First number: " ch1
    read -p "Second number: " ch2
    
    awk -v ch1="$ch1" -v ch2="$ch2" -v op="$operator" '
    BEGIN {
        if ( ( op == "/" ) && ( ch2 == 0 ) ) {
            print "Nope..."
        }
        else {
            print ch1 '"$operator"' ch2
        }
    }
    '
    
    $ ./tst.sh
    Operator: /
    First number: 4.5
    Second number: 2
    2.25
    
    $ ./tst.sh
    Operator: /
    First number: 4.5
    Second number: 0
    Nope...
    
    0 讨论(0)
  • 2020-11-22 03:00

    How about this? =D

    VAL_TO_CHECK="1.00001"
    if [ $(awk '{printf($1 >= $2) ? 1 : 0}' <<<" $VAL_TO_CHECK 1 ") -eq 1 ] ; then
        echo "$VAL_TO_CHECK >= 1"
    else
        echo "$VAL_TO_CHECK < 1"
    fi
    
    0 讨论(0)
  • 2020-11-22 03:01

    please check the below edited code:-

    #!/bin/bash
    
    export num1=(3.17648*e-22)
    export num2=1.5
    
    st=$((`echo "$num1 < $num2"| bc`))
    if [ $st -eq 1 ]
      then
        echo -e "$num1 < $num2"
      else
        echo -e "$num1 >= $num2"
    fi
    

    this works well.

    0 讨论(0)
  • 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<b) ? -1 : 0);}
    
    #!bashj
    num1=3.17648e-22
    num2=1.5
    comp=j.doubleCompare($num1,$num2)
    if [ $comp == 0 ] ; then echo "Equal" ; fi
    if [ $comp == 1 ] ; then echo "$num1 > $num2" ; fi
    if [ $comp == -1 ] ; then echo "$num2 > $num1" ; fi
    

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

    0 讨论(0)
提交回复
热议问题