Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java returning wrong boolean value when comparing integer with Double.MIN_VALUE

public class Test {
    public static void main(String[] args) {
        boolean v1 = -25 <= Double.MIN_VALUE;
        boolean v2 = -25 <= -100.0;
        System.out.println(v1); //true
        System.out.println(v2); //false
    }
}

Here -25 <= Double.MIN_VALUE should result in false, but it shows true! Again, when comparing -25 <= -100.0, it outputs the correct value.
What am I missing here? I think this might be due to comparing int and double, but still this doesn’t make sense, since the second test is also a similar one but returns correct value.
Edit: I should’ve checked against Double.NEGATIVE_INIFINITY, since this is the one that returns a negative value. Double.MIN_VALUE returns the smallest positive value, not the smallest negative value. This is not to confuse with Integer.MIN_VALUE, which really is in fact the smallest negative integer.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     **
     * A constant holding the smallest positive nonzero value of type
     * {@code double}, 2<sup>-1074</sup>. It is equal to the
     * hexadecimal floating-point literal
     * {@code 0x0.0000000000001P-1022} and also equal to
     * {@code Double.longBitsToDouble(0x1L)}.
     */
    public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324

Check in your JDK what double.MIN_VALUE represents.

It says the smallest positive value!

Your negative value -25 will always be smaller than any positive number.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading