The defintion of Double.MIN_VALUE
gives the answer. This is the smallest positive value that a double
can hold, equal to 2-1074, which is about 4.94 * 10-324. If you put 4.9E-324
in your program, it's close enough to MIN_VALUE
that the compiler will round up and use this value. But then when you divide by 2, the result is too small for a double
, so it is rounded down to 0.
The reason 2-1074 is the smallest value has to do with the format of 64-bit floating-point numbers specified by IEEE 754. This is a standard format, and most processors have built-in hardware to perform calculations on numbers in that format. (By contrast, BigDecimal
is represented as a Java array of unlimited length, so it's able to handle numbers with much greater precision; however, arithmetic on these numbers is done in software, making it much slower than calculations on double
s.) A 64-bit double
consists of a sign bit, an 11-bit exponent field, and a 52-bit mantissa. For a "normal" value, the exponent field is in the range 1 to 2046, and the actual exponent (power of 2) is offset from this so that the exponent ranges from -1022 to 1023. The mantissa is 1.xxxxx...xxx (in binary), where the x's are the 52 bits of the mantissa. The value represented by this format is 1.xxxxx...xxx * 2exponent. So the smallest normal value is 1 * 2-1022, or Double.MIN_NORMAL
. If the exponent field is 0, and the mantissa bits are not all zero, then the floating-point value is a "denormal" number whose value is 2-1022 * 0.xxxxx...xxx, where the x's are the 52-bit mantissa field. (Denormal numbers can get smaller than normal ones, but they don't have 52 bits of precision like normal doubles do.) The smallest possible value is thus 2-1022 * 0.00000...001; the last part of this expression is 2-52 since the mantissa field is 52 bits long, so that makes the smallest value 2-(1022+52) = 2-1074.