Van Peer
Van Peer

Reputation: 2167

julia inf by inf different results

I'm quite new to Julia - version 1.0.0 on Windows. The documentation states the following

julia> Inf / Inf
NaN

But when I perform the following I'm getting different results

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

Why 1/0 / 1/0 is not NaN, whereas ans/ans is?

Upvotes: 2

Views: 152

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69939

You actually have:

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

so this is consistent.

Now regarding:

julia> 1/0 / 1/0
Inf

Please observe how it is evaluated:

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

so we get a standard left to right evaluation (as should be expected). And you get:

julia> 1/0
Inf

julia> (1/0)/1
Inf

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

And all is OK.

Actually here you have one special thing to observe (this is not directly related to your question but is good to know as it might come up as a next question):

julia> Inf / 0
Inf

julia> Inf / (-0)
Inf

julia> Inf / (0.0)
Inf

julia> Inf / (-0.0)
-Inf

The reason is that integer 0 is the same as -0:

julia> 0 === -0
true

but floats carry sign bit:

julia> 0.0 === -0.0
false

Upvotes: 6

Related Questions