1

Why is the below returning false?

int i = 0;
if ((double) i > Double.MIN_VALUE)
    System.out.print("true");
else
    System.out.print("false");
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
kingbol
  • 67
  • 6

2 Answers2

4

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).

Shahid
  • 2,288
  • 1
  • 14
  • 24
4

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.

Fatih Aktaş
  • 1,446
  • 13
  • 25