为什么是sys.maxint< (sys.maxint - 100 + 0.01)在Python? [英] Why is sys.maxint < (sys.maxint - 100 + 0.01) in Python?
问题描述
在一个长64位的系统上, c> sys.maxint 是:
//十进制十六进制
9223372036854775807 0x7fffffffffffffff
所以 sys.maxint - 100
是:
9223372036854775707 0x7fffffffffffff9b
添加 0.01
会强制在添加之前将该值舍入为双精度浮点数。以双精度表示的两个最接近的值是:
9223372036854774784 0x7ffffffffffffc00
9223372036854775808 0x8000000000000000
因为 sys.maxint - 100
更接近于第二个)价值,它四舍五入。 0.01 给出: $ b
不能以双精度表示,所以再次舍入为:
9223372036854775808 0x8000000000000000
sys.maxint - 100 + 0.01
的值实际上大于 sys.maxint $ c的值$ C>。然而,在许多现代语言中,整数和浮点之间的比较迫使整数值在比较发生之前转换为浮点;如果这是python的情况下,
sys.maxint
将四舍五入到相同的值,他们会比较相等。看来,这不是在Python的情况。我不熟悉python数值的细节,但这是一个有趣的语言好奇。
Why is sys.maxint < (sys.maxint - 100 + 0.01) in Python?
On a system with 64-bit longs, sys.maxint
is:
// decimal hexadecimal
9223372036854775807 0x7fffffffffffffff
So sys.maxint - 100
is:
9223372036854775707 0x7fffffffffffff9b
Adding 0.01
forces this value to be rounded to double-precision floating point before the addition. The two closest values that are representable in double-precision are:
9223372036854774784 0x7ffffffffffffc00
9223372036854775808 0x8000000000000000
Because sys.maxint - 100
is closer to the second (larger) value, it rounds up. Adding 0.01
gives:
9223372036854775808.01 0x8000000000000000.028f5c28f5c...
which is not representable in double-precision, so it is rounded again, to:
9223372036854775808 0x8000000000000000
So the value of sys.maxint - 100 + 0.01
is actually larger than the value of sys.maxint
. However, in many modern languages, comparison between an integer and a float forces the integer value to be converted to floating point before the comparison takes place; if this were the case in python, sys.maxint
would be rounded up to the same value, and they would compare equal. It seems that this is not the case in Python. I'm not familiar with the details of python numerics, but this is an interesting curiosity of the language.
这篇关于为什么是sys.maxint< (sys.maxint - 100 + 0.01)在Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!