In current C ++ and Java, the double and float type: if (x == 0.0) is correct?

In my old knowledge : when we want to check if double or float is equal to 0.0,
we should not write like this:

double x = 0.0; if (x == 0.0) System.out.println("yes"); else System.out.println("no"); 

But a few minutes ago I tried again, in Java (1.7) and C ++ (Apple LLVM version 6.0) , there is no problem writing this! I tried using the double and float type under Java and C ++ respectively.

My question is:

  • I missed something, or we can really check for double or floating, as with current Java and C ++.
  • If possible, can we do this in an earlier version of Java and C ++?


Conclusion (based on all the tips):
We should not use "float == float or double == double" to check if two floats or two doubles are equal (if we want to get the right answer), the reasons are in all the answers and comments below .


Edition (first time):
Thank you so much for your help!
But a minute ago I’ll just try them under Java (1.7), everyone shows yes ,
it seems we can really do this in current Java!
 float x = 0.0f; if (x == 0) System.out.println("yes"); else System.out.println("no"); float y = 100.0f - 50.0f*2.0f + 45.0f*3 - 135.0f; if (y == 0.0f) System.out.println("yes"); else System.out.println("no"); if (100.0f - 50.0f*2.0f + 45.0f*3 - 135.0f == 0.0f) System.out.println("yes"); else System.out.println("no"); 


Edition (second time):
But I tried this, Java also shows yes , (in Java (1.7)).
I try to exclude the "pre-compute" compiler and break the calculation into several steps.
 float a = 100.0f; float b = 50.0f; float c = 2.0f; float bc = b * c; System.out.println("b*c = " + bc); float d = 45.0f; float e = 3.0f; float de = d * e; System.out.println("d*e = " + de); float f = 135.0f; float g = a - bc + de - f; float h = 0.0f; if (g == h) System.out.println("yes"); else System.out.println("no"); 


Edition (third time):
Thanks for @DiegoBasch counterexample (for float == float):
This time Java (1.7) shows " no ".
 float m = 0.37f - 0.36f; float n = 0.01f; if (m - n == 0.0f) System.out.println("yes"); else System.out.println("no"); 
+5
source share
2 answers

The code is legal. The problem is that when you perform calculations using floating point numbers, there will be rounding errors, and therefore, in many cases, checking for exactly 0 (or checking two numbers for equality) will not work.

This code is good in any language since you did nothing to cause rounding:

 double x = 0.0; if (x == 0.0) System.out.println("yes"); else System.out.println("no"); 

This code may not be quite right:

 double a1 = ...something...; double a2 = ...something...; double a3 = a1 / a2; double a4 = a3 * a2; double a5 = a4 - a1; if (a5 == 0.0) ... 

Although mathematically a5 should be 0, in practice the == operation can return false due to rounding.

This requires an understanding of how a computer is processed with a floating point; it has nothing to do with any language or version of the language. One link What every computer scientist needs to know about floating point .

+6
source

To complement ajb's answer, here is an example of a numerical error. You do not need a lot of calculations to make this happen.

  double a = 0.37 - 0.36; // 0.010000000000000009 double b = 0.01; System.out.println (a - b == 0.0); // false 
+2
source

Source: https://habr.com/ru/post/1204269/


All Articles