Java floating point Storage Format

Source: Internet
Author: User
In Java, floating point numbers have two basic types: float and double. Float occupies 4 bytes, and double occupies 8 bytes. The following uses the float type as an example to describe how to store floating point numbers. Double type and
Float Type storage methods are the same. 1. Storage of Floating Point Numbers The storage format of floating point numbers is special. It is the storage of 4-byte float variables: According to the ieee754 floating point representation standard, a float variable is stored in three parts: Symbol bit: 1 (31), indicating positive and negative float, 0 is positive, 1 is negative
Power Index: 8 digits (23-30), indicating the power of the binary weight
Valid bits: 23 bits (0-22), indicating valid numbers 2. floating point value range

In the storage of float, there are four special storage values:
0x7f800000: positive infinity, float. intbitstofloat () printed as Infinity
0xff800000: negative infinity, printed as-infinity
0x00000000: positive and zero. The printing result is 0.0.
0x80000000: negative zero, printed as-0.0
Note: in Java, infinity! =-Infinity, but 0.0 =-0.0

The above four special storage values divide the storage of float into four segments
[0x00000001, 0x7f7fffff]: number of positive float, total 2 ^ 31-2 ^ 23-1
[0x7f800001, 0x7fffffff]: Non-numeric. The Nan is displayed in the print box. A total of 2 ^ 23-1.
[0x80000001, 0xff7fffff]: number of negative float, 2 ^ 31-2 ^ 23-1 in total
[0xff800001, 0 xffffffff]: Non-numeric. Nan is displayed in the print box. A total of 2 ^ 23-1.

3. format conversion of Floating Point Numbers

Bits indicates an integer with a storage space of 4 bytes. Next we can find the float represented by these 4 bytes.
Type number.

Int S = (BITS> 31) = 0 )? 1:-1; // obtain the 1bit symbol
Int e = (BITS> 23) & 0xff); // retrieves the 8-bit power index
// Retrieve the 23-bit valid bits
Int M = (E = 0 )? (BITS & 0x7fffff) <1 ):
(BITS & 0x7fffff) | 0x800000 );
The floating point number represented by the bucket is S * m * 2 ^ (e-150)

Analysis:
[0x00000001, 0x007fffff]: The corresponding real number range is [(2 ^-149), (2 ^-126)-(2 ^-149)], that is
It is about [1.4e-45, 1.2e-38], and the discrete interval is fixed to (2 ^-149), that is, about 1.4e-45,
The number of real numbers is 2 ^ 23.
[0x00800000, 0x7f7fffff]: The corresponding real number range is [(2 ^-126), (2 ^ 128-2 ^ 104)], that is, approximately
[1.2e-38, 3.4e38]. In the future, each increase of 2 ^ 23 real numbers doubles the discrete interval.

Therefore, the floating point number design completes the entire A = [0x00000000, 0x7f7fffff] discrete space to the range of B = [0.0, 3.4e38]
A ing of the part value. This ing has the following attributes:
<1> the initial interval between the mapped real numbers in B is c = 2 ^-149, and the interval between the numbers after 2 ^ 23 is c = 2 * C.
<2> the ing increases monotonically.

Rating:
The storage design of floating point numbers is essentially an excellent numerical ing designed to make full use of binary storage.
.

 

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.