开发者

How to convert 2+(2/7) to IEEE 754 floating point

开发者 https://www.devze.com 2023-02-27 15:32 出处:网络
Can someone explain to me the steps to convert a number in decimal format (such as 2+(2/7)) into IEEE 75开发者_JAVA技巧4 Floating Point representation? Thanks!First, 2 + 2/7 isn\'t in what most people

Can someone explain to me the steps to convert a number in decimal format (such as 2+(2/7)) into IEEE 75开发者_JAVA技巧4 Floating Point representation? Thanks!


First, 2 + 2/7 isn't in what most people would call "decimal format". "Decimal format" would more commonly be used to indicate a number like:

2.285714285714285714285714285714285714285714...

Even the ... is a little bit fast and loose. More commonly, the number would be truncated or rounded to some number of decimal digits:

2.2857142857142857

Of course, at this point, it is no longer exactly equal to 2 + 2/7, but is "close enough" for most uses.

We do something similar to convert a number to a IEEE-754 format; instead of base 10, we begin by writing the number in base 2:

10.010010010010010010010010010010010010010010010010010010010010...

Next we "normalize" the number, by writing it in the form 2^e * 1.xxx... for some exponent e (specifically, the digit position of the leading bit of our number):

2^1 * 1.0010010010010010010010010010010010010010010010010010010010010...

At this point, we have to choose a specific IEEE-754 format, because we need to know how many digits to keep around. Let's choose "single-precision", which has a 24-bit significand. We round the repeating binary number to 24 bits:

2^1 * 1.00100100100100100100100  10010010010010010010010010010010010010...
           24 leading bits          bits to be rounded away

Because the trailing bits to be rounded off are larger than 1000..., the number rounds up to:

2^1 * 1.00100100100100100100101

Now, how does this value actually get encoded in IEEE-754 format? The single-precision format has a leading signbit (zero, because the number is positive), followed by eight bits that contain the value 127 + e in binary, followed by the fractional part of the significand:

0 10000000 00100100100100100100101
s exponent fraction of significand

In hexadecimal, this gives 0x40124925.

0

精彩评论

暂无评论...
验证码 换一张
取 消