Search code examples
bashfloating-pointdivide

bash divide result compared with float


THis is part of my bash code;

        b=`cat 101127_2_aa_1.fastq|head -$a|tail -1|sed 's/\(.\)B*$/\1/g'|wc -c`
        d=`cat 101127_2_aa_1.fastq|head -$a|tail -1|wc -c`
        if (($b%$d>=0.7))
        then

HOwever I got problems like:

line 13: ((: 26%100>=0.7: syntax error: invalid arithmetic operator (error token is ".7")

WHat's the problem? thx

edit: Two if loops in my script:

if (($a%4==0))
if (( 10*$b/$d>= 7 ))

Seems for first one, only "%" works

And for the second one, only "/" works

I'm confused


Solution

  • The division operator is /, not %.

    Also bash does not have floats. The workaround is to do something like

    if (( 10 * $b / $d >= 7 ))
    

    or

    if (( 10 * $b >= 7 * $d ))