简体   繁体   English

macOS 上的浮点二进制表示

[英]Floating point binary representation on macOS

I wrote a small program to see how my computer stores floating point numbers (this is on macOS Big Sur), and results looked like this:我写了一个小程序,看看我的电脑如何存储浮点数(这是在 macOS Big Sur 上),结果如下所示:

Float value = 2.000000
Memory representation: 00000000 00000000 00000000 01000000

Float value = 4.000000
Memory representation: 00000000 00000000 10000000 01000000

Float value = 8.000000
Memory representation: 00000000 00000000 00000000 01000001

Float value = 16.000000
Memory representation: 00000000 00000000 10000000 01000001

Float value = 32.000000
Memory representation: 00000000 00000000 00000000 01000010

I believe I am printing out the bits correctly, so my question is how is macOS storing each part of the floating point number?我相信我正确地打印出这些位,所以我的问题是 macOS 如何存储浮点数的每个部分? Meaning how should each of these bit strings be divided into the exponent, fractional part, and sign?意思是如何将这些位串中的每一个分成指数、小数部分和符号? It also looks like the sign bit is stored at the beginning of the last byte, rather than at the beginning of the first byte like I was expecting:看起来符号位存储在最后一个字节的开头,而不是像我期望的那样存储在第一个字节的开头:

Float value = 2.000000
Memory representation: 00000000 00000000 00000000 01000000

Float value = -2.000000
Memory representation: 00000000 00000000 00000000 11000000

You appear to be printing the bits with a reversed byte order.您似乎正在以相反的字节顺序打印这些位。 Apple tools used IEEE-754 binary32 for the float type. Apple 工具使用 IEEE-754 binary32 作为float类型。 IEEE-754 only specifies the encoding to bit strings; IEEE-754 只指定位串的编码; it does not specify how the bits of the string are ordered in memory.它没有指定字符串的位在 memory 中如何排序。 Apple tools store the bit strings in memory as if the bits were interpreted as a binary numeral (most significant bit first) and an unsigned int with that value were written to memory. Apple 工具将位字符串存储在 memory 中,就好像这些位被解释为二进制数字(最高有效位在前)并且具有该值的unsigned int被写入 memory。

The bit string that encodes 2 is 01000000 00000000 00000000 00000000.编码 2 的位串是 01000000 00000000 00000000 00000000。

The bit string that encodes 4 is 01000000 10000000 00000000 00000000.编码 4 的位串是 01000000 10000000 00000000 00000000。

The bit string that encodes 8 is 01000001 00000000 00000000 00000000.编码 8 的位串是 01000001 00000000 00000000 00000000。

The bit string that encodes 16 is 01000001 10000000 00000000 00000000.编码 16 的位串是 01000001 10000000 00000000 00000000。

The bit string that encodes 32 is 01000010 00000000 00000000 00000000.编码 32 的位串是 01000010 00000000 00000000 00000000。

In the strings as shown above, the first bit encodes the sign.在如上所示的字符串中,第一位编码符号。 The next eight bits encode the exponent and the first bit of the significand.接下来的八位编码指数和有效数字的第一位。 The last 23 bits encode the remaining bits of the significand.最后 23 位对有效数字的剩余位进行编码。

For 32, the first bit is 0, meaning + or (−1) 0 = +1.对于 32,第一位是 0,表示 + 或 (-1) 0 = +1。

The next eight bits are 10000100. Taken as binary, these represent a value of 132. That encodes the exponent with a bias of 127, so the exponent represented is 132−127 = 5, so the scale of the floating point number is 2 5 .接下来的 8 位是 10000100。以二进制表示,它们表示值 132。它以 127 的偏差对指数进行编码,因此表示的指数为 132−127 = 5,因此浮点数的比例为 2 5 . Additionally, the fact that these bits are not all zeros (nor all ones) means the leading bit of the significand is 1.此外,这些位不是全零(也不是全一)的事实意味着有效数字的前导位是 1。

The remaining 23 bits are zeros, so the remaining bits of the significand are zeros.剩余的 23 位为零,因此有效数的剩余位为零。 Thus the significand, in binary, is 1.00000000000000000000000.因此,二进制的有效数字是 1.00000000000000000000000。

So the number represented is +1•2 5 •1.00000000000000000000000 = 32.所以表示的数字是+1•2 5 •1.00000000000000000000000 = 32。

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

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