有人可以解释这个浮点行为吗? [英] Can someone explain this floating-point behavior?

查看:222
本文介绍了有人可以解释这个浮点行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的启发我试图找出那里发生了什么(我的答案更直观,但我不能完全理解为什么)



< (相当于64位的Python):

 >>> sys.maxint 
9223372036854775807
>>> Python使用IEEE 754浮点运算符(float)($ s
9.2233720368547758e + 18

点表示,有效地具有53位的重要性。不过,就我所知,上面例子中的重要部分需要57位(如果你放下隐含的引导1,则需要56位)来表示。有人可以解释这种差异吗?

解决方案

也许下面的内容会帮助你清理一下:

 >>>十六进制(int(float(sys.maxint)))
'0x8000000000000000L'

那么 float(sys.maxint)实际上是2的幂。因此,二进制的尾数正好是 1 。在IEEE 754中,隐含了前导 1。,所以在机器表示中,这个数字的尾数由全零位组成。



<实际上,代表这个数字的IEEE位模式如下:

pre $ 0x43E0000000000000

观察到只有前三个半字符(符号和指数)非零。有效数字完全由零组成。因此,它不需要表示56(甚至53)位。


Inspired by this question, I was trying to find out what exactly happens there (my answer was more intuitive, but I cannot exactly understand the why of it).

I believe it comes down to this (running 64 bit Python):

>>> sys.maxint
9223372036854775807
>>> float(sys.maxint)
9.2233720368547758e+18

Python uses the IEEE 754 floating-point representation, which effectively has 53 bits for the significant. However, as far as I understand it, the significant in the above example would require 57 bits (56 if you drop the implied leading 1) to be represented. Can someone explain this discrepancy?

解决方案

Perhaps the following will help clear things up:

>>> hex(int(float(sys.maxint)))
'0x8000000000000000L'

This shows that float(sys.maxint) is in fact a power of 2. Therefore, in binary its mantissa is exactly 1. In IEEE 754 the leading 1. is implied, so in the machine representation this number's mantissa consists of all zero bits.

In fact, the IEEE bit pattern representing this number is as follows:

0x43E0000000000000

Observe that only the first three nibbles (the sign and the exponent) are non-zero. The significand consists entirely of zeroes. As such it doesn't require 56 (nor indeed 53) bits to be represented.

这篇关于有人可以解释这个浮点行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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