Value range of Float in Java

xiaoxiao2021-03-06  67

Specification

The fact that the floating point number in Java IEEE Standard 754 Floating Point NumBers standard, the standard specification can be referenced

http://blog.9cbs.net/treeroot/articles/94752.aspx.

Float occupies 4 bytes, and int is the same, that is, 32bit.

The first bit represents the symbol, 0 represents a positive number, 1 indicates a negative number, this is very well understood, no multi-tube.

The second-9 Bit represents an index, a total of 8 is (which can represent 0-255), the base of the base is 2, in order to simultaneously represent the positive and negative numbers, the amount of 127 is subtracted. In this way is (- 127 to 128), additional 0 and all 1 as special treatment, so direct representation -126 to 127.

The remaining 23 bits represent the fractional part, and 23 points represent 24-bit numbers because there is a default leading 1 (only binary has this characteristic).

The final result is: (- 1) ^ (SIGN) * 1.F * 2 ^ (Exponent)

Here: SIGN is a symbol bit, F is a score of 23 bit, and Exponent is an index section, and finally the scope is (because the number of positive and negative is symmetrical, here only care positive)

2 ^ (- 126) ~~ 2 (1-2 ^ (- 24)) * 2 ^ 127

This is not the range of Float, because the standard is also specified in the standard, and there are some special provisions.

Non-specificity:

When the index portion is all 0 and the decimalized floating point is indicated when the decimal portion is 0, because the default is not a preamble 1, but 0.

The value is 0.F * 2 ^ (- 126), indicating the range of 2 ^ (- 149) ~~ (1-2 ^ (- 23)) * 2 ^ (- 126) This is not considered symbol. Why is it here? -126 instead of -127? If it is -127, then the biggest representation is

2 ^ (- 127) -2 ^ (- 149), it is obvious that 2 ^ (- 127) ~~ 2 ^ (- 126) cannot be expressed.

Other Special Representation 1. When the index portion and the decimal portion are 0, 0 value, 0 and -0 (symbolic bit decision), 0x00000000 indicate that the positive 0, 0x80000000 represents the negative 0.

2. The index part is all 1, and the decimal part is 0, indicating infinity, is infinite and negative, 0x7f800000 means that is endless, 0xff800000 indicates endless.

3. The index portion is all 1, when the fraction is 0, it means NAN, which is divided into QNAN and SNAN, and Java is NAN.

in conclusion:

It can be seen that the value range of floating point numbers is: 2 ^ (- 149) ~~ (2-2 ^ (- 23)) * 2 ^ 127, that is, float.min_value and float.max_Value.

转载请注明原文地址:https://www.9cbs.com/read-120088.html

New Post(0)