Comparing doubles in Java gives odd results

前端 未结 5 777
误落风尘
误落风尘 2021-02-05 16:54

I really can\'get my head around why the following happens:

Double d = 0.0;
System.out.println(d == 0); // is true
System.out.println(d.equals(0)); // is false ?         


        
5条回答
  •  太阳男子
    2021-02-05 17:49

    It's probably worth noting that you should compare floating point numbers like this:

    |x - y| < ε, ε very small
    

提交回复
热议问题