Infinities.jl icon indicating copy to clipboard operation
Infinities.jl copied to clipboard

Inconsistency in arithmetic of infinites of different types

Open fingolfin opened this issue 2 years ago • 2 comments

This is fine:

julia> RealInfinity() - RealInfinity()
ERROR: ArgumentError: Cannot subtract ∞ from ∞
Stacktrace:
 [1] -(x::RealInfinity, y::RealInfinity)
   @ Infinities ~/.julia/packages/Infinities/r8p4Q/src/Infinities.jl:195
 [2] top-level scope
   @ REPL[12]:1

This is also fine:

julia> Inf - Inf
NaN

This is not:

julia> RealInfinity() - Inf
+∞

julia> Inf - RealInfinity()
-∞

fingolfin avatar Aug 22 '23 14:08 fingolfin

Thanks for spotting this. I guess somewhere assumes floats are finite...

dlfivefifty avatar Aug 22 '23 15:08 dlfivefifty

Yeah it is this code:

for Typ in (:Number, :Real, :Integer, :AbstractFloat)
    @eval begin
        isless(x::RealInfinity, y::$Typ) = signbit(x) && y ≠ -∞
        isless(x::$Typ, y::RealInfinity) = !signbit(y) && x ≠ ∞
        +(::$Typ, y::RealInfinity) = y
        +(y::RealInfinity, ::$Typ) = y
        -(y::RealInfinity, ::$Typ) = y
        -(::$Typ, y::RealInfinity) = -y
        function *(a::$Typ, y::RealInfinity)
            iszero(a) && throw(ArgumentError("Cannot multiply $a * $y"))
            a > 0 ? y : (-y)
        end
    end
end

fingolfin avatar Aug 23 '23 06:08 fingolfin