8

In the program below, the result is that 0.0 is considered less than Double.MIN_VALUE. Why?

We have a solution (work with Doubles only and use compareTo) and I want to understand why unboxing is failing here.

import java.util.Date;
import java.util.Calendar;
import java.math.BigDecimal;

public class Test {

  public static void main(String[] args) {
    double max = 99999.9999;
    double min = Double.MIN_VALUE;
    Double test = 0.0;

    System.out.println(max > test); // expect true; is true
    System.out.println(test > min); // expect true; is false
  }
}
1
  • Going to add a link to a previous SO question that has an excellent answer from @aioobe. IMO Sun should have named this constant something more intuitive but that ship has sailed. stackoverflow.com/questions/3884793/…
    – Perception
    Jul 23, 2011 at 14:00

4 Answers 4

11

According to the Javadocs :

MIN_VALUE

A constant holding the smallest positive nonzero value of type double, 2-1074.

In other words, it is bigger than 0.

5

You should read the Double.MIN_VALUE specification. It's a minimum possible but positive Double value which means it's larger than 0.0.

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

Double.MIN_VALUE = 4.9E-324 which is still a positive number. I think you are looking for min = - Double.MAX_VALUE

2

According to me autoboxing has no problems. Perhaps you simply need to use something like Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY that should work well with the < and > operators. For example note that

-Double.MAX_VALUE > Double.NEGATIVE_INFINITY
is true!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.