朱莉娅·芬奇inf的不同结果

时间:2018-09-07 08:53:11

标签: julia

我是Julia的新手-Windows上的1.0.0版本。 documentation声明以下内容

julia> Inf / Inf
NaN

但是当我执行以下操作时,我会得到不同的结果

julia> 1/0
Inf

julia> 1/0 / 1/0  # this should be NaN right, tried (1/0)/(1/0) as well
Inf

julia> 1/0
Inf

julia> ans/ans
NaN

为什么1/0 / 1/0不是NaN,而ans/ans是?

1 个答案:

答案 0 :(得分:5)

您实际上有:

julia> (1/0)/(1/0)
NaN

所以这是一致的。

现在有关:

julia> 1/0 / 1/0
Inf

请观察其评估方式:

julia> :(1/0 / 1/0)
:(((1 / 0) / 1) / 0)

所以我们得到了从左到右的标准评估(应该可以预期)。然后您得到:

julia> 1/0
Inf

julia> (1/0)/1
Inf

julia> ((1/0)/1)/0
Inf

一切都很好。

实际上,您需要注意一件事(这与您的问题没有直接关系,但很高兴知道,因为它可能会作为下一个问题出现)

julia> Inf / 0
Inf

julia> Inf / (-0)
Inf

julia> Inf / (0.0)
Inf

julia> Inf / (-0.0)
-Inf

原因是整数0-0相同:

julia> 0 === -0
true

但浮点数带有符号位:

julia> 0.0 === -0.0
false