-1
public class test {
    public static void main(String[] args) {

        double num = -Double.MAX_VALUE;
        double num1 = Double.MIN_VALUE;

        if (num < num1)
            System.out.println("num");
        else
            System.out.println("num1");
        
    }
}

I wrote this code to check which one is smaller and it return num, I was wondering why is it like that with double but not with integers?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Galz
  • 31
  • 4
  • 5
    `Double.MAX_VALUE` is a very large positive number. `Double.MIN_VALUE` is a very small positive number, not a very large negative number. – khelwood Oct 14 '21 at 12:21
  • 2
    To be precise [`Double.MIN_VALUE`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Double.html#MIN_VALUE) represents "the smallest positive nonzero value of type double" – Federico klez Culloca Oct 14 '21 at 12:23
  • then why is it not the same in integers, -Integer.MAX_VALUE equals Integer.MIN_VALUE – Galz Oct 14 '21 at 12:23
  • @Galz *"-Integer.MAX_VALUE equals Integer.MIN_VALUE"* it doesn't. `(-Integer.MAX_VALUE) == Integer.MIN_VALUE` returns false on my machine. – Federico klez Culloca Oct 14 '21 at 12:29
  • `-Integer.MIN_VALUE == Integer.MIN_VALUE`, however. `-Integer.MAX_VALUE == Integer.MIN_VALUE + 1`. – Andy Turner Oct 14 '21 at 12:30
  • 1
    @Galz EPSILON is the name of the constant for the smallest double value in .NET. I believe this is just a bad naming decision on part of Java, you will find a lot of them and they are still in the language for backwards compatibility purposes. – corlaez Oct 14 '21 at 12:30

2 Answers2

1

See https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html

Double.MAX_VALUE is the biggest finite number that double can hold. (A very large positive number.)

Double.MIN_VALUE is the smallest positive number that double can hold. (A very small positive number.)

So -Double.MAX_VALUE is a very large negative number. That's a lower number than a very small positive number.

Contrarily, Integer.MIN_VALUE is the most negative number that an int can hold. It has a different meaning from the similarly named constant in Double.

khelwood
  • 55,782
  • 14
  • 81
  • 108
1

As stated in the Javadoc of Double:

  • MAX_VALUE is "A constant holding the largest positive finite value of type double, (2-2-52)·21023.", that is, a large positive number.
  • MIN_VALUE is "A constant holding the smallest positive nonzero value of type double, 2-1074.", that is, a small positive number.

So, -MAX_VALUE is a large negative number, which is less that zero, which is less than a small positive number.small positive number.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243