如何在CPU内部存储浮点数? [英] How are floating point numbers stored inside the CPU?

查看:183
本文介绍了如何在CPU内部存储浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名初学者,正在学习组装基础知识.现在,在阅读此问题时,我来到了这一段.它解释了如何在内存中存储浮点数.

I am a Beginner and going through Assembly basics. Now while reading the matter, I came to this paragraph. It is explaining about how floating point numbers are stored inside memory.

浮点数的指数是一个8位字段.允许大量或 要存储的数字较小,则将指数解释为正数或 消极的.实际指数是8位字段的值减去127. 127是32位浮点数的指数偏差". 浮点数的分数字段有一个小的惊喜.由于定义了0.0 因为所有位都设置为0,所以不必担心将0.0表示为 指数字段等于127,而分数字段设置为所有O.所有其他 数字至少有一个1位,因此IEEE 754格式使用隐式1 节省空间.因此,如果分数字段是00000000000000000000000,它 被解释为1. 00000000000000000000000.这允许分数 字段有效为24位.这是一个聪明的把戏,使 使OxOO和OxFF的指数场变得特别.

The exponent for a float is an 8 bit field. To allow large numbers or small numbers to be stored, the exponent is interpreted as positive or negative. The actual exponent is the value of the 8 bit field minus 127. 127 is the "exponent bias" for 32 bit floating point numbers. The fraction field of a float holds a small surprise. Since 0.0 is defined as all bits set to 0, there is no need to worry about representing 0.0 as an exponent field equal to 127 and fraction field set to all O's. All other numbers have at least one 1 bit, so the IEEE 754 format uses an implicit 1 bit to save space. So if the fraction field is 00000000000000000000000, it is interpreted as 1 . 00000000000000000000000. This allows the fraction field to be effectively 24 bits. This is a clever trick made possible by making exponent fields of OxOO and OxFF special.

我什么都没得到.

您能解释一下它们如何存储在内存中吗?我不需要参考,我只需要一个很好的解释,这样我就可以轻松理解.

Can you explain me how they are stored inside memory ? I don't need references, I just need a good explanation so that I can easily understand.

推荐答案

浮点数遵循 IEEE754标准.他们之所以使用这套规则,主要是因为浮点数可以(相对)轻松地与整数和其他浮点数进行比较.

Floating point numbers follow the IEEE754 standard. They have been using this set of rules mainly because floating point numbers can be (relatively) easily compared to integers and to other floating point numbers too.

有2种常见的浮点版本:32位( IEEE binary32又称为单精度float )和64位( binary64又称为double精度).它们之间的唯一区别是它们的字段大小:

There are 2 common versions of floating points: 32bit (IEEE binary32 aka single-precision float) and 64bit (binary64 aka double precision). The only difference between them is the size of their fields:

  • 指数:32位为8位,64位为11位
  • 尾数:32位为23位,64位为52位

还有一个附加位,即符号位,用于指定所考虑的数字是正数还是负数.

There's an additional bit, the sign bit, that specifies if the considered number is positive or negative.

现在,以12,375以10为基数(32位)为例:

Now, take for example 12,375 base 10 (32bit):

  • 第一步是将该数字转换为基数2: 很简单,经过一些计算,您将得到:1100.011

  • First step is to convert this number in base 2: it's pretty easy, after some calculations you will have: 1100.011

接下来,您必须移动逗号"直到得到1.100011(直到.之前的唯一数字是1).我们将逗号移动多少次? 3,那是指数.这意味着我们的号码可以表示为1.100011*2^3. (由于它是二进制的,所以不称为小数点.它是基数点"或二进制点".)

Next you have to move the "comma" until you get 1.100011 (until the only digit before the . is a 1). How many times we move the comma? 3, that is the exponent. It means that our number can be represented as 1.100011*2^3. (It's not called a decimal point because this is binary. It's a "radix point" or "binary point".)

.周围移动(并用指数计数那些移动),直到尾数以前导1.开头,这称为归一化".太小而无法以这种方式表示的数字(指数的有限范围)称为次正规数或非正规数.

Moving the . around (and counting those moves with the exponent) until the mantissa starts with a leading 1. is called "normalizing". A number that's too small to be represented that way (limited range of the exponent) is called a subnormal or denormal number.

此后,我们必须对指数添加偏倚. 32位浮点数中的8位指数字段为127.我们为什么要这样做?很好的答案是:因为这样我们可以更轻松地将浮点数与整数进行比较. (将FP位模式比较为整数会告诉您哪个具有更大的幅度(如果它们具有相同的符号).)而且,递增位模式(包括从尾数到指数的进位)会将幅度增加到下一个可表示的值. (nextafter())

After that we have to add the bias to the exponent. That's 127 for the 8-bit exponent field in 32bit floats. Why should we do this? Well the answer is: because in this way we can more easily compare floating points with integers. (Comparing FP bit-patterns as integer tells you which one has larger magnitude, if they have the same sign.) Also, incrementing the bit-pattern (including carry from the mantissa into exponent) increases the magnitude to the next representable value. (nextafter())

如果我们不这样做,则负指数将使用二补码表示,实际上将1放在最高有效位.但是以这种方式,较小的浮点似乎大于正指数的浮点.出于这个原因:我们只加127,在这个小技巧的情况下,所有正指数都从10000000底数2(即1底数10)开始,而负指数最大达到01111110底数2(即-1基数10). ).

If we didn't do this a negative exponent would be represented using two-complement notation, essentially putting a 1 in the most significant bit. But in this way a smaller floating point seems to be greater than a positive-exponent floating point. For this reason: we just add 127, with this little "trick" all positive exponents starts from 10000000 base 2 (which is 1 base 10) while negative exponents reach at most 01111110 base 2 (which is -1 base 10).

在我们的示例中,归一化指数为10000010以2为底.

In our example the normalized exponent is 10000010 base 2.

  • 最后要做的是在指数后添加尾数(.100011),结果是:

01000001010001100000000000000000
 |  exp ||      mantix         |

(第一位是符号位)

有一个不错的在线转换器,它可以可视化32位浮点数的位,并显示它所代表的十进制数.您可以修改其中一个,它也会更新另一个. https://www.h-schmidt.net/FloatConverter/IEEE754.html

There's a nice online converter that visualizes the bits of a 32-bit float, and shows the decimal number it represents. You can modify either and it updates the other. https://www.h-schmidt.net/FloatConverter/IEEE754.html

那是简单的版本,这是一个好的开始.通过省略它来简化:

That was the simple version which is a good start. It simplified by leaving out:

  • 非数字NaN(有偏指数=全1;尾数= 0)
  • +-无穷大(有偏指数=全1;尾数= 0)
  • 对于次正规数没有多说(有偏指数= 0表示尾数中的前导0而不是正常1).

有关单精度和双精度的Wikipedia文章非常出色,其中有图表,并给出了一些极端情况和细节的解释.有关完整的详细信息,请参见它们.

The Wikipedia articles on single and double precision are excellent, with diagrams and lots of explanation of corner cases and details. See them for the complete details.

此外,某些(大多数是历史性的)计算机使用的不是IEEE-754的FP格式.

Also, some (mostly historical) computers use FP formats that aren't IEEE-754.

还有其他IEEE-754格式,例如16位半精度,一种值得注意的扩展精度格式是80位x87,它显式存储有效数字的前导1,而不是用零或非隐式表示. -零指数.

And there are other IEEE-754 formats, like 16-bit half-precision, and one notable extended-precision format is 80-bit x87 which stores the leading 1 of the significand explicitly, instead of implied by a zero or non-zero exponent.

IEEE-754甚至定义了一些十进制浮点格式,使用10 ^ exp精确表示十进制小数而不是二进制小数. (硬件对此的支持是有限的,但确实存在).

IEEE-754 even defines some decimal floating-point formats, using 10^exp to exactly represent decimal fractions instead of binary fractions. (HW support for these is limited but does exist).

这篇关于如何在CPU内部存储浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆