float occupies 4 bytes, which is the same as int, which is 32bit.
The first bit represents the sign, 0 represents a positive number, and 1 represents a negative number. This is easy to understand, so don’t worry about it.
The 2nd to 9th bits represent the exponent, a total of 8 bits (can represent 0-255). The base here is 2. In order to represent both positive and negative numbers, an offset of 127 needs to be subtracted here. In this case, the range is (-127 to 128). In addition, all 0s and all 1s are treated as special processing, so -126 to 127 is directly expressed.
The remaining 23 bits represent the decimal part, where 23 bits represent a 24-bit number 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 decimal part of 23 bits, exponent is the exponent part, and the final range is (because positive and negative numbers are symmetrical, we only care about positive numbers here)
2^(-126) ~~ 2(1-2^(-24)) * 2^127
This is not the value range of float, because the standard also stipulates denormalized representation, and there are some special regulations.
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.
The value bit is 0. f * 2^(-126), indicating the range bits 2^(-149)~~ (1-2^(-23)) * 2^(-126) The sign is not considered here. Why is it -126 instead of -127? If it is -127, then the maximum expression is
2^(-127)-2^(-149), obviously 2^(-127) ~~2^(-126) cannot be expressed.
Other special expressions
1. When the exponent part and the decimal part are all 0, it represents a 0 value, which can be divided into +0 and -0 (determined by the sign bit). 0x00000000 represents positive 0, and 0x80000000 represents negative 0.
2. When the exponent part is all 1 and the decimal part is all 0, it means infinity, including positive infinity and negative infinity. 0x7f800000 means positive infinity, and 0xff800000 means negative infinity.
3. When the exponent part is all 1 and the decimal part is not all 0, it means NaN, which is divided into QNaN and SNaN. Both are NaN in Java.
in conclusion:
It can be seen that the value range of floating point numbers is: 2^(-149)~~(2-2^(-23))*2^127, which is Float. MIN_VALUE and Float. MAX_VALUE.
PS: float to hexadecimal, hexadecimal to float
package com.sondon.dev_soceket.test; /** * @Project: Hardware communication* @Package: com.sondon.tcpip * @Class: Test.java * @Company Guangzhou Xundong Network Technology Co., Ltd.* @Author: Cai Wenfeng* @DateTime: April 2, 2015 11:21:53 AM * @Blog: http://blog.csdn.net/caiwenfeng_for_23 * @Description: {Test} */ public class Test { public static void main(String[] args) { String s="3E1E9E9F"; Float value = Float. intBitsToFloat(Integer.valueOf(s.trim(), 16)); System.out.println(value); Float f=0.15490197f; System.out.println(Integer.toHexString(Float.floatToIntBits(f))); } }