Storage Format of floating point numbers in Java

Source: Internet
Author: User
When a colleague asked me: what is the value range of double in Java, I am confused, except that floating point numbers are composed of symbol bit, exponent bit, and decimal place, nothing else knows. In the university, "Computer composition" Middle School also forgot everything.
I checked some materials, wrote some test code, and finally figured it out. I 'd like to take a note here. 1. Three storage formatsJava follows the IEEE 754 standard. In this specification, three types of floating point numbers are mentioned: Single-precision, double-precision, and double-precision extension.
The storage of these three types of floating point numbers is composed of three parts: the symbol bit, the index bit, And the decimal place. The difference is that the three indexes and the decimal places are different.
The IEEE Single-precision format has 24-bit valid numerical precision, and occupies 32 digits in total. The IEEE dual-precision format has a 53-bit valid digital precision, and occupies a total of 64-bit. For dual-precision expansion, IEEE requires that it has at least 64-bit Valid Digital Precision and occupies at least 79 digits in total.

2. Double Precision formatNow, we only analyze double-precision floating-point numbers, that is, double. The other two types can be described in this way.
The IEEE dual-precision format consists of 52 decimal places F, 11-bit offset index E, and 1-bit symbol S.
These fields are stored consecutively in two 32-bit characters. As shown in:

The two consecutive 32-bit characters are numbered as one 64-bit character, where 0-51 digits store the 52-bit decimal places F; 52: 62 digits store the 11-bit bias index E; the 63rd-bit storage symbol S.
If S is 0, it indicates an integer and 1 indicates a negative number.
E [52: 62] A total of 11 bits indicate the bias index, that is, the order code. It is an unsigned number. The value range is [0,211-1], that is, []. When 0 <e <2047, it indicates the exponent value is e-1023; when it is 0, it indicates the exponent value is-1022; and when e = 2047, it represents an infinite or meaningless number (which will be discussed later ).
Now, the last decimal part is left, that is, the ending number.
Decimals are classified into normalized and non-normalized numbers. The left decimal point of the normalized number implies 1, while the left decimal point of the non-normalized number is 0. What are the advantages of implicit 1 on the left of the decimal point? We know that any decimal point can be expressed by scientific Notation: A number can be expressed in the form of a × 10n, where 1 ≤ A <10, n is an integer. In decimal, the integer part of A must be an integer ranging from 1 to 9. In binary, the integer part of a can only be 1. Since it must be 1, it is obviously not worthwhile to waste 1-bit storage space for this common waste. Therefore, 1 is hidden. This is why the decimal part is normalized when 0 <e <2047.
So why is the fractional part of E equal to 0 non-normalized? The reason is very simple. If a normalized number is also used at this time, its value range will surely be faulty. That is to say, between the maximum and minimum values, there are some numbers in the range of numerical values that cannot be expressed.
As we mentioned above, when e = 2047, it represents an infinite or meaningless number. If at this time, the fractional part is all 0, it is infinite, so positive infinity or negative infinity is determined by the symbol bit. If at least one decimal part is not 0, it is a meaningless number.
The chart is shown as follows:

The following table lists the relations between the bit modes of the dual-precision storage format and the bit modes of the IEEE values:
The bit mode of the meaningless number (Nan, non-number) in is only one of the many bit modes that can represent Nan. In addition, we note that although the decimal values of + 0 and-0 are equal, their bit modes are different. 3. Sample CodeNow that we have a clear understanding of the storage format, we can write code to deepen our understanding of floating point storage.
The double class in Java encapsulates double operations. We can easily use double to operate double. The function Double. longbitstodouble () can convert the given bit mode to double. If you want to verify whether the hexadecimal 0x7fefffffffffff-Bit mode is decimal, this is very easy. You only need the following two lines of code: double value = double. longbitstodouble (0x7fefffffffffffffl );
System. Out. println (value); we can see that the output result is: 1.7976931348623157e308, which is also consistent with our expectation. I will not demonstrate other examples one by one. Here I will attach a link to the code. <Download> 〉 References:
1. IEEE Standard 754 for binary floating-point Arithmetic
2. Numerical Computation Guide (http://gceclub.sun.com.cn/TT/sunstudio/NCG/819-4817-10.pdf) (end)

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.