Reputation: 757
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");
}
Upvotes: 4
Views: 3386
Reputation: 59305
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
.
Upvotes: 3
Reputation: 511
If you are looking to compare a value to the smallest number (the negative value with the greatest magnitude) you might want to look at Double.NEGATIVE_INFINITY
which the Java documentation states is:
"A constant holding the negative infinity of type double. It is equal to the value returned by Double.longBitsToDouble(0xfff0000000000000L)."
Upvotes: 0
Reputation: 1461
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
Upvotes: 4
Reputation: 6784
Double.MIN_VALUE
is equal to the hexadecimal floating-point literal 0x0.0000000000001P-1022
and also equal to Double.longBitsToDouble(0x1L).
So your condition fails!
Upvotes: 0
Reputation: 37023
You are doing decimal number comparison. If you say try something like:
System.out.println(0.000000000000001d == 0.);//print false
You will get false. If i read java docs it says:
/**
* A constant holding the smallest positive nonzero value of type
* {@code double}, 2<sup>-1074</sup>. It is equal to the
* hexadecimal floating-point literal
* {@code 0x0.0000000000001P-1022} and also equal to
* {@code Double.longBitsToDouble(0x1L)}.
*/
So its near to zero but not really 0.
Upvotes: 2