Infinities.jl
Infinities.jl copied to clipboard
Inconsistency in arithmetic of infinites of different types
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()
-∞
Thanks for spotting this. I guess somewhere assumes floats are finite...
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