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
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Chip McCormick
  • 744
  • 4
  • 17
  • 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. http://stackoverflow.com/questions/3884793/minimum-values-and-double-min-value-in-java – Perception Jul 23 '11 at 14:00

4 Answers4

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.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
Nico Huysamen
  • 10,217
  • 9
  • 62
  • 88
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). 
user802421
  • 7,465
  • 5
  • 40
  • 63
2

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

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
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!
Plap
  • 1,046
  • 1
  • 8
  • 14