开发者

Difference between Float.MIN_VALUE and Float.MIN_NORMAL? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Difference among Double.MIN_NORMAL and Double.MIN_VALUE

1) Can some one pls explain to me what the difference is between MIN_NORMAL and MIN_VALUE?

开发者_开发百科System.out.println(Float.MIN_NORMAL);
System.out.println(Float.MIN_VALUE);

2) Also, why does still still print 1.0?

float f = Float.MIN_NORMAL + 1.0f;
System.out.println(f);

double d = Float.MIN_NORMAL + 1.0f;
System.out.println(d);

Output:

1.17549435E-38
1.4E-45
1.0
1.0


The answer for the first question is in the duplicate.

The answer for the second question is:

Both Floats and Doubles do not have infinite precision. You can conveniently think they have around 16 digits of precision. Anything past that and you are going to have rounding errors and truncation.

So, 1.0e0 + 1e-38 is going to lack precision to do anything except for end with 1.0e0 due to truncating the extra precision.

Really, like the rest of the answer, it requires an understanding of how floating point numbers in IEEE format are actually added in binary. The basic idea is that the non-sign-and-exponent portion of the binary, floating point number is shifted in the IEEE-754 unit on the CPU (80 bits wide on Intel, which means there is always truncation at the end of the calculation) to represent its real number. In decimal this would look like this:

Digit: 1 234567890123456
Value: 1.0000000000000000000000000000...0000
Value: 0.0000000000000000000000000000...0001

After the add is processed, it is practically:

Digit: 1 234567890123456
Value: 1.0000000000000000000000000000...0001

So, with it in mind that the value truncates around the 16 digit mark (in decimal, it's exactly 22 binary digits in a 32-bit float, and 51 binary digits in a 64-bit double, ignoring the very important fact that the leading 1 is shifted (with respect to the exponent) and assumed (effectively compressing 23 binary digits into 22 for 32-bit and 52 to 51 for 64-bit); this is a very interesting point, but one you should read more detailed examples, such as here for those details).

Truncated:

Digit: 1 234567890123456
Value: 1.00000000000000000000

Note the really small decimal portion is truncated, thus leaving 1.

Here is a good page that I use whenever I have issues thinking about the actual representation in memory: Decimal to 32-bit IEEE-754 format. In the site, there are links for 64-bit as well and going in reverse.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜