Anyone who doesn't know about this really shouldn't be speaking on what computers are supposedly good at
(This is not a bug. This is documented and expected behavior in pretty much every widely used programming language today. See: IEEE 754)
Discussion
Anyone who doesn't know about this really shouldn't be speaking on what computers are supposedly good at
(This is not a bug. This is documented and expected behavior in pretty much every widely used programming language today. See: IEEE 754)
@ferret yeah, but that's not great either, right? because it diverges from the behavior of standard floating point numbers that's established nearly everywhere else
@ferret actually, it's much worse
bash doesn't do floating point at all. I didn't know until today
the bad thing here is that it fails silently without a warning or error
@aesthr @ferret it's quite difficult to get bash to do arithmetic inside [[ ]]; i had to resort to $(( ))
$ (set -x; [[ 0.1 + 0.2 == 42 ]]; echo $?)
bash: unexpected token `+', conditional binary operator expected
bash: syntax error near `+'
$ (set -x; [[ (0.1 + 0.2) == 42 ]]; echo $?)
bash: unexpected token `+', conditional binary operator expected
bash: expected `)'
bash: syntax error near `+'
(cont'd...)
$ (set -x; [[ ( 0.1 + 0.2 ) == 42 ]]; echo $?)
bash: unexpected token `+', conditional binary operator expected
bash: expected `)'
bash: syntax error near `+'
but finally
$ (set -x; [[ $(( 0.1 + 0.2 )) == 42 ]]; echo $?)
bash: 0.1 + 0.2 : arithmetic syntax error: invalid arithmetic operator (error token is ".1 + 0.2 ")
and
$ (set -x; [[ $(( 1 + 2 )) == 42 ]]; echo $?)
+ [[ 3 == 42 ]]
+ echo 1
1
(but that last [[ 3 == 42 ]] is still a string comparison!)
@aesthr @ferret this is one of the many reasons why I tend to tell beginners "the moment something starts to look complicated or difficult to do in shell, the moment you are tempted to reach for _any bash extension at all_, that is the moment you should stop and rewrite the entire script in _any other language_, I don't care which one, as long as it isn't Tcl or C shell, you'll be having a better time than with Bourne shell".
@zwol a C compiler will at least yell at you a lot instead of just quietly doing the wrong thing much of the time :)
@ferret it still fails silently even with `set -e` in place. awesome
And if you're a programmer and just learning about this now ... I'm so sorry, but someone had to tell you the truth eventually.