1

I know you can't compare something like 1.0 == 1 safely.

But could you compare something like 0.0 == 0?

Since floating points can represent the idea of zero perfectly, this shouldn't be a problem right?

EDIT


Another possibly helpful link to future visitors
Which is the first integer that an IEEE 754 float is incapable of representing exactly?

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • Floating points can also represent the idea of 1.0 perfectly. What is the difference in your understanding to 0.0? – Yunnosch Jul 29 '18 at 20:24
  • @Yunnosch I didn't know that either. Could you please provide a citation? – AlanSTACK Jul 29 '18 at 20:24
  • ["A widening primitive conversion does not lose information about the overall magnitude of a numeric value in the following cases, **where the numeric value is preserved exactly**: \[...\] • from `int` to `double`"](https://docs.oracle.com/javase/specs/jls/se10/html/jls-5.html#jls-5.1.2) The notion of "safely" here seems kind of vague to me, though. Whether a particular floating-point comparison is safe depends on what you're actually attempting to do. – Radiodef Jul 29 '18 at 20:26
  • I have marked your question as a duplicate of another that answers it as asked. When a floating-point value is compared to an integer zero, the zero is converted to floating-point. This conversion is exact (the result is exactly zero), and the comparison is exact (it returns true if and only if the floating-point value is exactly zero). I suspect you may have some idea that a floating-point zero might “represent” some number that is approximately zero. That would be a subject for another question, as it does not affect the comparison.… – Eric Postpischil Jul 29 '18 at 20:37
  • … Each floating-point number represents one number exactly. It does not represent an interval or an approximation. If you have a floating-point number that has been produced as the result of some sequence of computations, and you are concerned that it might be zero even though a mathematically perfect computation would have produced a small non-zero number, then you have a different problem than the one you asked about. We can see that because, in such a situation, if you compared the computed result to a floating-point zero instead of an integer zero, it would still produce true. – Eric Postpischil Jul 29 '18 at 20:38
  • @EricPostpischil Yeah, I just wanted to run my thoughts by other people for confirmation. In any case, thanks. – AlanSTACK Jul 29 '18 at 20:38
  • @EricPostpischil... What do you mean? Maybe I wasn't clear with my previous comment. What I meant is that storage of ints, chars, bytes... isn't comparable with float, double. Correct me, if I'm wrong. – zlakad Jul 29 '18 at 20:40
  • @zlakad A comparison in Java does not compare the bits that represent values. It compares the values. If an `int` is compared to a `float`, the `int` is converted to `float`, and then the values are compared. How they are encoded as bits is not relevant. – Eric Postpischil Jul 29 '18 at 22:08

0 Answers0