2

We have been told again and again that we should be very careful when we do equal (==) comparison with floating point numbers in C language because floating point numbers are represented approximately.

However, we do not see many warnings when it comes to greater than or equal to comparison (>=) while in theory we can have the same issue.

Can anyone help me to understand why we do not care that much about >= comparison, or > comparison?

Thanks.

  • 2
    The issues are the same, since `>=` includes checking for equality. It's not mentioned explicitly because it's so obvious. – Barmar Sep 06 '19 at 10:50
  • Because in general the possibility of error for `>` is small. If a number is greater than another, even if it is approximate, `>` will return true. If you're scared of using `==`, use `abs(x - y) <= margin_of_error`. – S.S. Anne Sep 06 '19 at 11:08
  • 2
    Please note that there is nothing wrong with the floating point `==`. It does exactly what it is supposed to. The real culprit is usually conversions between decimal format (typically number as string) and binary floating point with limited precision, or careless assignments between floating point types of different precisions. – user694733 Sep 06 '19 at 11:22

2 Answers2

1

Floating point math is imprecise because of the challenges of storing such values in a binary representation. Therefore, the use of the equality == and inequality != operators on float or double values is almost always an error.

Instead the best course is to avoid floating point comparisons altogether (or at least be really carefull with it and choose wisely what you are doing).

Have a look here for more details.

Mike
  • 4,041
  • 6
  • 20
  • 37
  • I think you can use some comparisons, such as `abs(x - y) <= margin_of_error`, as I mentioned before. – S.S. Anne Sep 06 '19 at 11:09
  • 2
    *"best course is to avoid floating point comparisons altogether."* I don't think that is good advice. Article you linked has a better one: *"Know what you’re doing: There is no silver bullet. You have to choose wisely."* – user694733 Sep 06 '19 at 11:28
1

One way of thinking about floating-point numbers is that they're never exactly equal. (This isn't completely true, of course, but it's not completely false, either.)

And if you think about it that way, then > is the same as >=, and < is the same as <=. So there's not much wrong with using >= and <=, except that the = part might be unnecessary.

That is, for a given piece of code, if the "right" code would have involved >, but you write >= instead, the code probably won't fail, it will probably work just fine.

But this has been a rather general, handwavey answer. This whole answer was predicated on "If you think of floating-point numbers as never being equal", which as I said, isn't really correct. In particular, since floating-point numbers certainly can be exactly equal, there are certainly plenty of cases where a >= comparison would work, and a > comparison would give different results and would fail. So, in reality, you're usually going to have to think about each case more carefully.

But the bottom line is, while it's almost always wrong to use == and != with floating-point numbers, there's not nearly as big a prohibition against >= and <=. (But they might be wrong, or poor, so they're still worth thinking about.)

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 3
    Comment for the downvoter(s): This has been an attempt to answer the question as stated, "Why do we not hear prohibitions against `<=` and `>=` as often as we hear prohibitions against `==` and `!=`? This is not intended to be an answer to the question, "Is it wrong to use `>=` and `<=`? – Steve Summit Sep 06 '19 at 11:44
  • Thanks Steve Summit, you are addressing exactly my question. – Andrew Smith Sep 10 '19 at 15:15