The range of float type in Java and its conversion to hexadecimal

Float occupies 4 bytes, which is the same as int, that is, 32bit. The first bit represents a symbol, 0 represents a positive number, and 1 represents a negative number. This is easy to understand. There is no need for multiple tubes. The 2nd-9th bit represents the index, and a total of 8 is (can represent 0-255). The base here is 2. In order to represent both positive and negative numbers, the offset of 127 should be subtracted. In this case, the range is (- 127 to 128). In addition, all 0 and all 1 are treated as special, so it directly represents - 126 to 127. The remaining 23 digits represent the decimal part, where 23 digits represent 24 digits, because there is a default leading 1 (only binary has this feature). The final result is: (- 1) ^ (sign) * 1. F * 2 ^ (exponent): here, sign is the sign bit, f is the fractional part of 23bit, exponent is the exponential part, and finally the range is (because the positive and negative numbers are symmetrical, only the positive numbers are concerned here) 2 ^ (- 126) ~ ~ 2 (1-2 ^ (- 24)) * 2 ^ 127 is not the value range of float, because the non standardized representation is also specified in the standard, and there are some special provisions. Denormalized representation: when the exponent part is all 0 and the decimal part is not all 0, it represents a denormalized floating-point number, because there is no leading 1 by default, but 0. Value bit 0. F * 2 ^ (- 126), indicating range bits 2 ^ (- 149) ~ (1-2 ^ (- 23)) * 2 ^ (- 126) symbols are not considered here. Why is this - 126 instead of - 127? If it is - 127, the maximum is 2 ^ (- 127) - 2 ^ (- 149). Obviously, 2 ^ (- 127) ~ ~ 2 ^ (- 126) cannot be represented.

Other special representations 1. When the exponential part and decimal part are all 0, it represents the value of 0, which can be divided into + 0 and - 0 (determined by sign bit), 0x00000000 represents positive 0 and 0x80000000 represents negative 0. 2。 When the exponential part is all 1 and the decimal part is all 0, it means infinity, with positive infinity and negative infinity, 0x7f800000 means positive infinity and 0xff800000 means negative infinity. 3。 When the index part is all 1 and the decimal part is not all 0, it indicates Nan, which is divided into qnan and snan. Both are Nan in Java. Conclusion: it can be seen that the value range of floating point number is 2 ^ (- 149) ~ (2-2 ^ (- 23)) * 2 ^ 127, that is, float. MIN_ Value and float. MAX_ VALUE。

PS: float to hexadecimal, hexadecimal to float

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>