Skip to content

Commit

Permalink
fix conversion bug; add //
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdoris committed May 5, 2019
1 parent e194e4d commit 2b1ef4a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
29 changes: 26 additions & 3 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Base.:*(x::T, y::T) where {T<:InfExtended} =
end

Base.:/(x::Infinite, y::Infinite) = throw(DivideError())
Base.:/(x::InfExtended{T}, y::InfExtended{T}) where {T<:Real} = InfExtended{typeof(one(T)/one(T))}(
@generated Base.:/(x::InfExtended{T}, y::InfExtended{T}) where {T<:Real} = :(convert($(InfExtended(typeof(one(T)/one(T)))),
if isinf(x)
if isinf(y)
throw(DivideError())
Expand All @@ -46,6 +46,29 @@ Base.:/(x::InfExtended{T}, y::InfExtended{T}) where {T<:Real} = InfExtended{type
if isinf(y)
zero(T)
else
x / y
x.val / y.val
end
end)
end))

Base.abs(x::Infinite) = PosInf
Base.abs(x::InfExtended{T}) where {T<:Real} = InfExtended{T}(abs(x.val))

Base.:(//)(x::Infinite, y::Infinite) = throw(DivideError())
@generated Base.:(//)(x::InfExtended{T}, y::InfExtended{S}) where {T<:Real,S<:Real} = :(convert($(InfExtended(typeof(one(T)//one(S)))),
if isinf(x)
if isinf(y)
throw(DivideError())
else
Infinite(signbit(x) signbit(y))
end
else
if isinf(y)
zero(T)
else
x.val // y.val
end
end))
Base.:(//)(x::InfExtended, y::Real) = //(promote(x,y)...)
Base.:(//)(x::Real, y::InfExtended) = //(promote(x,y)...)
Base.:(//)(x::Infinite, y::Real) = //(promote(x,y)...)
Base.:(//)(x::Real, y::Infinite) = //(promote(x,y)...)
2 changes: 1 addition & 1 deletion src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The union of `T` and `Infinite`: either `T` if infinity can be represented as a
Converts `x` to a `InfExtended(typeof(x))`.
"""
@generated InfExtended(x::T) where {T<:Real} = hasinf(T) ? :x : :($(InfExtended{T})(x))
@generated InfExtended(x::T) where {T<:Real} = hasinf(T) ? :x : :($(InfExtended(T))(x))

"""
InfMinusInfError()
Expand Down
6 changes: 3 additions & 3 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Base.promote_rule(::Type{InfExtended{T}}, ::Type{Infinite}) where {T<:Real} = In
pinf = posinf(T)
ninf = neginf(T)
mkpinf = pinf===nothing ? :(throw(InexactError(:convert,T,x))) : pinf
mkninf = ninf===nothing ? :(throw(InexactError(:convert,T,x))) : pinf
:(x.signbit ? $mkpinf : $mkninf)
mkninf = ninf===nothing ? :(throw(InexactError(:convert,T,x))) : ninf
:(x.signbit ? $mkninf : $mkpinf)
end
Base.convert(::Type{T}, x::InfExtended) where {T<:Real} = convert(T, x.val)
@generated Base.convert(::Type{T}, x::InfExtended{S}) where {T<:Real,S<:Real} = :(convert($(typeof(convert(T,zero(S)))), x.val))
Base.convert(::Type{Infinite}, x::Real) = isinf(x) ? Infinite(signbit(x)) : throw(InexactError(:convert,Infinite,x))
Base.convert(::Type{Infinite}, x::Infinite) = x
Base.convert(::Type{T}, x::S) where {T<:InfExtended, S<:Real} = T(x)
Expand Down

0 comments on commit 2b1ef4a

Please sign in to comment.