Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Double.MIN_VALUE and 0

Why this code does't work? Actually,this is just the small part of a big program. It has to compare Double.MIN_VALUE to different values, it works with all values except 0. ,why? Thanks!

double d = Double.MIN_VALUE;
if (0. > d) {
    System.out.println("OK");
}
like image 810
Andre Liberty Avatar asked Sep 30 '25 09:09

Andre Liberty


2 Answers

The Double.MIN_VALUE is 4.9E-324. And this is not less than 0. But it is not actually 0.

If you print

System.out.println(4.9E-324d > 0.);//this is true

In this sense,

0.0000000000...0001 != 0. But it tends to 0

The same way 4.9E-324d != 0 but tends to 0

like image 60
E Do Avatar answered Oct 02 '25 04:10

E Do


Double.MIN_VALUE is actually a bad name for the constant in Java. It does not fit to int.MIN_VALUE. In C# it is called Double.Epsilon, which IMHO fits better.

So, Double.MIN_VALUE is not the largest negative double value that exists. IMHO there's no such constant for the largest discrete Double number. Otherwise, the largest negative number is Double.NEGATIVE_INFINITY.

like image 34
Thomas Weller Avatar answered Oct 02 '25 06:10

Thomas Weller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!