Floating point number

Source: Internet
Author: User

Due to the binary relationship, most decimals under decimal are not perfectly represented in the binary, but can only be represented as infinite loop decimals.

For example, 0.5 can be represented as 0.1,0.4 and can only be represented as 0.011001100110 ...

Decimal decimals are converted to binary decimals, which can be achieved by fractional multiplication of 2 methods.

For example: 0.4

0.4 * 0.8

0.8 * 1.6

0.6 * 1.2

0.2 * 0.4

Takes the integer part of the product, resulting in an infinite loop of 0.0110.

Another way is to see 0.4 as 2/5 is 2 divided by 5, that is 10/101, by the division operation can also get the same value.

Therefore, the computer's limited memory is doomed to be unable to represent infinite repeating decimal, only using approximate values instead. The 32-bit and 64-bit representations are as follows:

650) this.width=650; "style=" border-bottom:0px;border-left:0px;border-top:0px;border-right:0px; "title=" clipboard "border=" 0 "alt=" clipboard "src=" Http://s3.51cto.com/wyfs02/M00/7E/57/wKiom1b8z-rwrTzwAAAXYOOUO_Y022.png "height= "/>"

The value of the order code is represented by the shift code, the code is the complement of the symbol bit inversion, [x] Shift and [x] complement the relationship is the sign bit reciprocal number (only the sign bit is different).

It is also important to note that the order value needs to subtract the offset value 127 is the true exponent value, the offset value is usually 2e-1-1, where e is the length of the order code. As an example of a single-precision floating-point number, its exponential field is 8 bits, the fixed offset value is 28-1-1 = 128?1 = 127

When we represent floating-point numbers, we always make sure that the highest level is 1, and if it is 0, it is multiplied by 2^n, that is, the left shift n becomes 1.

So a 23-bit mantissa can represent a 24-bit valid number. this simplified floating-point representation of operations is called normalization . The normalized data is in the following format

±1.xxxxxx*2^e

When the 1.1010001*2^10100 is represented as 32 bits, the order code needs to be added 127

0001 0100

+0111 1111

——————

1001 0011

Therefore, when the floating-point number is positive, the minimum value is encoded as follows:

650) this.width=650; "style=" border-bottom:0px;border-left:0px;border-top:0px;border-right:0px; "title=" clipboard [1] "border=" 0 "alt=" clipboard[1] "src=" Http://s3.51cto.com/wyfs02/M02/7E/57/wKiom1b8z-ugDMt8AACG9PMSH1M708.png " Height= "/>"

The order code is 0-127=127, and the fractional part is 1, so this value represents 2^-127

650) this.width=650; "style=" border-bottom:0px;border-left:0px;border-top:0px;border-right:0px; "title=" clipboard [2] "border=" 0 "alt=" clipboard[2] "src=" Http://s3.51cto.com/wyfs02/M00/7E/57/wKiom1b8z-vTK4OaAAAMVqAetvk276.png " Height= "/>"

The order code is 255-127=128, the fractional part is divided into 1.11111111111111111111111 (positive number is divided into 1, the fractional part is 23 1), therefore this value represents (2-2^-23) *2^128

1.11111111111111111111111=2-2^-23, this equation let me in the beginning also Leng, think of the next there are many ways to prove.

1.11111111111111111111111=2^0+2^-1+2^-2+2^-3+2^-4..................+2^-22+2^-23

Geometric Series summation formula
650) this.width=650; "style=" border-bottom:0px;border-left:0px;border-top:0px;border-right:0px; "title=" clipboard [3] "border=" 0 "alt=" clipboard[3] "src=" Http://s3.51cto.com/wyfs02/M01/7E/57/wKiom1b8z-zAEzSkAAAH2UOCm2E497.png " Height= "/>"

A1=1,Q=1/2, n=24

So the final result equals 2-2^-23.


There is another way to calculate

1.11111111111111111111111=10-0.000000000000000000000001

The binary representation is 2-2^-23

Similarly, if the highest bit is changed to 1, it indicates a negative number. Therefore, the value range of floating-point numbers is:

[-(2-2^-23) *2^128,-2^-127] and [2^-127, (2-2^-23) *2^128] converted to decimal is
650) this.width=650; "style=" border-bottom:0px;border-left:0px;border-top:0px;border-right:0px; "title=" clipboard [4] "border=" 0 "alt=" clipboard[4] "src=" Http://s3.51cto.com/wyfs02/M01/7E/57/wKiom1b8z-3zETxLAADAPAYHEQI585.png " height= "134"/>

If the axis is displayed, other areas will cause an overflow. In fact, this floating-point number is not continuous on the axis, it can only be a very close approximation, but this approximation is sufficient to meet the general application.

The floating-point representation of the above is only a normal way of interpretation, there will be some improvements in actual use, such as the above floating point number can not represent the real 0.

The most widely used method of representing floating-point numbers is defined by the IEEE 754 standard, which defines 32-bit single-precision and 64-bit double precision.

The IEEE754 standard defines some special values. As shown, excerpted from Wikipedia

650) this.width=650; "style=" border-bottom:0px;border-left:0px;border-top:0px;border-right:0px; "title=" clipboard [5] "border=" 0 "alt=" clipboard[5] "src=" Http://s3.51cto.com/wyfs02/M01/7E/53/wKioL1b80JKSxUG5AACWlzlJ1Mw379.png " height= "421"/>

Row by line explanation:

1. Positive zero and negative 0, except for the sign bit, all the numbers are 0 cases, considered to be ±0. This 0 is an exact value.

2. When the exponent is 0111 1111=127, minus the offset 127, that is, the actual exponent is 0, the mantissa is full zero, the default number before the decimal point is 1, so this situation is considered ±1.0

3. When the exponential field is 0 and the trailing digit is not 0 o'clock, the number is considered to be non-normalized. Normalized number, by default, is considered to be 1 before the decimal point, instead of normalized number, the default is that the number before the decimal point is 0.

Also, when the number is non-normalized, the specified offset is 1 larger than the original and becomes-126.

4. The exponential field is not 0, the normalized number, at which point the Mantissa field is the default number before the decimal point is 1, the minimum normalized number of the actual exponent is 1-127=-126, that is, the smallest normalized number is ±2^-126

5. The maximum index cannot be 255,255 reserved, only 254, so the actual index value is 254-127=127. The largest normalized number is 1.11111111111111111111111 (binary) ^127

6. The exponential domain is all 1 and the Mantissa field is 0, indicating positive and negative infinity.

7. The exponential domain is all 1, the Mantissa field is not 0, and is an illegal floating-point number.

The above rules are summarized as follows:

form index decimal part
0 0 0
non-statute form 0 not 0
statute form 1-254 any
Infinity 255 0
NaN 255 Non-Zero

The introduction of non-formal floating-point numbers is necessary and valid, except that their absolute value is less than the absolute value of all the statute floats, i.e. all non-statute floating-point numbers are closer to 0 than the statute floating-point numbers.

650) this.width=650; "style=" border-bottom:0px;border-left:0px;border-top:0px;border-right:0px; "title=" clipboard [6] "border=" 0 "alt=" clipboard[6] "src=" Http://s3.51cto.com/wyfs02/M02/7E/53/wKioL1b80JTTNuZfAAElUs5Tp_U517.png " height= "266"/>

It fills a part of the gap, so that the number more evenly cover the axis, the use of non-normalized numbers, called the progressive underflow, if not in this way, then the absolute minimum of the Statute floating point and 0 distance is the distance between the adjacent small floating point 2^23 times! Can be said to be very abrupt under overflow to 0. A bad consequence of this is that two small floating-point numbers ranging from X to Y are subtracted, and the result will be 0.

Based on some of the special values in the previously listed IEEE754, you can see that the range of 32-bit floating-point values is -3.4x10^38 to +3.4x10^38

Resources

"Computer Composition and Architecture"

https://zh.wikipedia.org/wiki/IEEE_754

This article is from "a blog" blog, make sure to keep this source http://cnn237111.blog.51cto.com/2359144/1758841

Floating point number

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.