1

I’ve encountered an error, probably a bug in bc. I encountered it when trying to compare two numbers on a script in bash. It turns out that when I do:

echo "1.1E-2<1.1E-1" | bc -l

It returns 1 (as expected).

But when I do this for exmaple:

echo "2.1E-2<1.1E-1" | bc -l

It returns 0, when it should return 1 (because 0.021 is smaller than 0.11).

I’m using bc version 1.06.95 with kubuntu 14.04

Has anyone found a similar result? Can anyone think of a workaround?

Giacomo1968
  • 58,727

2 Answers2

3

bc doesn't understand exponential notation.

It probably interprets "E" as a hex digit, but in longer numbers, it seems to translate it to 9 (bug?), and similarly for other hex digits:

bc -l <<< E-4    # 14 - 4
10

bc -l <<< 1E-1   # 19 - 1
18

bc -l <<< F.4E2
9.492
choroba
  • 20,299
1

Your workaround

    echo "2.1*10^-2<1.1*10^-1" | bc -l

If the case you want to transform the output of another file you can do something similar to

    echo "2.1E-2<1.1E-1" | sed 's/E/*10^/g'  | bc -l 

where with sed you go to substitute E with *10^ that is accepted by bc

Hastur
  • 19,483
  • 9
  • 55
  • 99