The scope of float type in Java and its conversion with hexadecimal example _java

Source: Internet
Author: User

Float takes 4 bytes, and int is the same, that is, 32bit. The
      1th bit notation, 0 for positive numbers, and 1 for negative numbers, is well understood, not multiple. The
      第2-9个 bit represents an exponent, altogether 8 (can represent 0-255), where the base is 2, minus 127 offsets to represent both positive and negative numbers. In this case the range is (-127 to 128), and all 0 and 1 are treated as special, so the direct representation is 126 to 127.
     The remaining 23 digits represent the decimal portion, where 23 bits 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 a symbol bit, F is a decimal part of 23bit, exponent is an exponent, and finally the range is (because positive numbers are symmetric, only positive ones are concerned)
    2^ ( -126) ~ 2 (1-2^ ( -24)) * 2^127
    This is not a float value range, because the standard also stipulates a nonstandard notation. There are also special provisions.
   
Non-normalized representations:
    when the exponent part is full 0 and the decimal part is not full 0, the nonstandard floating-point number, because there is no leading 1, but 0.
    takes a value bit 0. F * 2^ (-126), indicating the range bit 2^ (-149) ~ ~ (1-2^ (-23)) * 2^ (-126) No notation is considered here. Why is this here-126 instead of 127? If it is-127, then the maximum representation is
2^ (-127) -2^ (-149), and obviously 2^ (-127) ~~2^ (-126) cannot be represented.
 

Other special representations
1. When the exponent and the fractional part are all 0 o'clock, representing 0 values, there are +0 and 0 points (the sign bit is determined), 0x00000000 indicates that positive 0,0x80000000 represents negative 0.
2. The exponent part is all 1, the fractional part all 0 o'clock, represents the infinity, has the positive infinite and the negative infinite, the 0x7f800000 expresses positive infinite, the 0xff800000 expresses the negative infinity.
3. The index part is all 1, the decimal part is not all 0 o'clock, indicates nan, divides into Qnan and Snan,java are Nan.

Conclusion:
You can see the value range of floating-point numbers: 2^ (-149) ~ ~ (2-2^ (-23)) *2^127, that is float. Min_value and float. Max_value.

Ps:float 16, 16 into float

Package com.sondon.dev_soceket.test; 
 
 
/** 
 * @Project: Hardware communication 
 * @Package: Com.sondon.tcpip 
 * @Class: Test.java 
 * @Company Guangzhou Mobile network Technology Co., Ltd. 
 * @Au Thor: Cai Wen 
 * @DateTime: April 2, 2015 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= "3e1e9e9 F "; 
    Float value = Float.intbitstofloat (integer.valueof (S.trim ()); 
    System.out.println (value); 
     
    Float f=0.15490197f; 
    System.out.println (Integer.tohexstring (Float.floattointbits (f))); 
  } 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.