gpt4 book ai didi

ruby - 为什么在 Ruby 中是 Float::INFINITY == Float::INFINITY?

转载 作者:数据小太阳 更新时间:2023-10-29 07:09:50 24 4
gpt4 key购买 nike

在数学中,2 个无穷大既不等于,也不大于或小于 then。那么是什么给了?

在 irb 中,Float::INFINITY == Float::INFINITY(在 ruby​​ 1.9.3 中测试)

最佳答案

用更专业的术语来说,这一切都归结为浮点运算的 IEEE 754 标准。

The IEEE 754 standard does implicitly define Infinity == Infinity to be true. The relevant part of the standard is section 5.7: "Four mutually exclusive relations are possible [between two IEEE 754 values]: less than, equal, greater than, and unordered. The last case arises when at least one operand is NaN."

Between any pair of floating point values exactly one of these four relations is true. Therefore, since Infinity is not NaN, Infinity is not unordered with respect to itself. Having one of (Infinity < Infinity) and (Infinity > Infinity) be true wouldn't be consistent, so (Infinity == Infinity).

这取自 http://compilers.iecc.com/comparch/article/98-07-134

关于ruby - 为什么在 Ruby 中是 Float::INFINITY == Float::INFINITY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14249697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com