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

  • 2020-04-01 04:17:11
  • OfStack

A float takes 4 bytes, which is the same as an int, which is 32 bits.
          The first bit is the sign, zero is positive, and one is negative, which is easy to understand, so you don't have to multiplex.
          The 2-9 bits represent the exponents, so there's a total of 8 (which can be represented as 0-255), and the base here is 2, so in order to represent both positive and negative Numbers, we have to subtract the offset of 127. So the range is going to be minus 127 to 128, plus all 0's and all 1's as special cases, so it's going to be minus 126 to 127.
        The remaining 23 bits represent the decimal part, where the 23 bits represent the 24 bits, because there is a default leading 1(only binary has this feature).
        The end result is minus 1 to the sign times 1. F * 2 ^ (exponent)
        Here :sign is the sign bit, f is the decimal part of 23bit, and exponent is the exponent part.
      2 to the minus 126, minus 2 to the minus 24 times 2 to the minus 127
      This is not yet in the range of float values, because the standard also provides for non-normalized notation, and there are some special provisions.
     
Non-normalized expression:
      When the exponent part is all 0 and the decimal part is not all 0, it represents an unnormalized floating point number, because by default there is no leading 1, but 0.
      The value bit is 0. F * 2 ^ (126), said the scope of a ^ 2 (149) ~ (1-2 ^ (23)) * 2 ^ (126) there are no consider symbols. Why is it -126 instead of -127? If it's minus 127, then the maximum is
2 to the minus 127, minus 2 to the minus 149, obviously 2 to the minus 127, minus 2 to the minus 126 is not going to work.
 

Other special representations
      1. When the exponent part and the decimal part are all 0, it represents the value of 0, which is divided into +0 and -0 (determined by the symbol bit). 0x00000000 means positive 0, 0x80000000 means negative 0.
      2. When the exponent 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 exponent part is all 1 and the decimal part is not all 0, it represents NaN, which is divided into QNaN and SNaN. Both are NaN in Java.
 
Conclusion:
      It can be seen that the scope of floating point Numbers: 2 ^ (149) ~ (2-2 ^ (23)) * 2 ^ 127, namely, Float. MIN_VALUE and Float. MAX_VALUE.

PS: float to hexadecimal, hex to float


package com.sondon.dev_soceket.test; 
 
 
/** 
 * @Project :  The hardware communication  
 * @Package : com.sondon.tcpip 
 * @Class : Test.java 
 * @Company  Guangzhou telecom network technology co. LTD  
 * @Author :  Wen-feng CAI  
 * @DateTime : 2015 years 4 month 2 day   In the morning 11:21:53 
 * @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))); 
  } 
} 


Related articles: