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,740
  • 9
  • 33
  • 33
kingbol
  • 47
  • 6
  • You can see this https://stackoverflow.com/questions/3884793/why-is-double-min-value-in-not-negative – lucumt Sep 26 '18 at 05:37

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,258
  • 1
  • 13
  • 23
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,316
  • 10
  • 23