Search code examples
javadouble

Why is -(Double.MAX_VALUE) smaller than Double.MIN_VALUE?


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?


Solution

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