简体   繁体   English

如何将2+(2/7)转换为IEEE 754浮点

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

Can someone explain to me the steps to convert a number in decimal format (such as 2+(2/7)) into IEEE 754 Floating Point representation? 有人可以向我解释将十进制格式的数字(例如2+(2/7))转换为IEEE 754浮点表示的步骤吗? Thanks! 谢谢!

First, 2 + 2/7 isn't in what most people would call "decimal format". 首先, 2 + 2/7不是大多数人所说的“十进制格式”。 "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. 当然,在这一点上,它不再精确等于2 + 2/7 ,但对于大多数用途来说“足够接近”。

We do something similar to convert a number to a IEEE-754 format; 我们做类似的事情将数字转换为IEEE-754格式。 instead of base 10, we begin by writing the number in base 2: 而不是以10为底,我们从以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^e * 1.xxx...的形式将数字“规范化”,以表示某些指数e (具体而言,我们数字前导位的数字位置):

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. 在这一点上,我们必须选择一种特定的IEEE-754格式,因为我们需要知道要保留多少位数。 Let's choose "single-precision", which has a 24-bit significand. 让我们选择“单精度”,它具有24位有效数字。 We round the repeating binary number to 24 bits: 我们将重复的二进制数四舍五入为24位:

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: 由于要舍入的尾随位大于1000... ,因此该数字舍入为:

2^1 * 1.00100100100100100100101

Now, how does this value actually get encoded in IEEE-754 format? 现在,该值实际上如何以IEEE-754格式编码? 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: 单精度格式有一个前导符号位(零,因为数字为正数),后跟八位包含二进制值127 + e位,后跟有效位数的小数部分:

0 10000000 00100100100100100100101
s exponent fraction of significand

In hexadecimal, this gives 0x40124925 . 十六进制为0x40124925

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM