Why doesn't 1//0 throw an error?

Hi, I have noticed that 1//0 does not throw any errors, which is a bit unexpected. Even more, inv(0//1) is 1//0. On the other hand, 0//0 throws an argument error (understandably). Is this the intended behavior?

My current understanding is that 1//0 mimics the behavior of Inf. However, the main difference is that 0*Inf is a NaN, whereas 0*(1//0) throws an error. I encountered this situation when looking at this issue in StaticArrays.jl, where A=@SMatrix zeros(5,5);det(A) works fine and A=@SMatrix zeros(Rational,5,5);det(A) does not for the reason stated above.

2 Likes

Come down to pure laziness, but in a good way.

Start with the rationals.

struct Rational{T<:Integer} <: Real
    num::T
    den::T
end

So

typeof(1//00) is Rational(1, 0) but no error is immediately thrown. That’s the job of any function to evaluate and decide how to treat it.

No. It was intentional, with analogies to floating point.

3 Likes

0*Inf is a NaN, whereas 0*(1//0) throws an error

If 1//0 is intended to be analogous to floating point Inf, then this seems like a place where that analogy is broken.

That is the intent. It works out such that Inf falls out naturally from the rest of the rational math, while NaN doesn’t.

2 Likes

1//0 being analogous to +Inf doesn’t mean Rational has to comply with IEEE 754, and num and den doesn’t have room for NaNs. Rational also throws an error upon num overflow instead of going to ±Inf (I suppose 1//0 and -1//0 for Rational), and Integer doesn’t (usually) have a negative zero.

1 Like