kingbol
kingbol

Reputation: 67

Comparing 0 with Double min value

Why is the below returning false?

int i = 0;
if ((double) i > Double.MIN_VALUE)
    System.out.print("true");
else
    System.out.print("false");

Upvotes: 1

Views: 812

Answers (2)

Fatih Aktaş
Fatih Aktaş

Reputation: 1564

Okay, let's see what we get from Double.MIN_VALUE. When we say,

System.out.println(Double.MIN_VALUE);

It prints out that the minimum double value is 4.9E-324, which is POSITIVE and NONZERO.

In your code you compare it to 0. Even though how small 4.9E-324 is, it is still greater than 0.

If you are trying to find the smallest negative double that you can get, then you are looking for,

System.out.println(-Double.MIN_VALUE);

This will return -4.9E-324, which is the smallest and negative number that you can get with Double.

Upvotes: 4

Shahid
Shahid

Reputation: 2330

Because Double.MIN_VALUE is positive and nonzero. According to Oracle doc:

MIN_VALUE: A constant holding the smallest positive nonzero value of type double, 2-1074. It is equal to the hexadecimal floating-point literal 0x0.0000000000001P-1022 and also equal to Double.longBitsToDouble(0x1L).

Upvotes: 4

Related Questions