Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ julia> conj(1 + 3im)
conj(z::Complex) = Complex(real(z),-imag(z))
abs(z::Complex) = hypot(real(z), imag(z))
abs2(z::Complex) = real(z)*real(z) + imag(z)*imag(z)
inv(z::Complex) = conj(z)/abs2(z)
function inv(z::Complex)
c, d = reim(z)
(isinf(c) | isinf(d)) && return complex(copysign(zero(c), c), flipsign(-zero(d), d))
complex(c, -d)/(c * c + d * d)
end
inv(z::Complex{<:Integer}) = inv(float(z))

+(z::Complex) = Complex(+real(z), +imag(z))
Expand Down Expand Up @@ -346,7 +350,7 @@ function /(a::Complex{T}, b::Complex{T}) where T<:Real
end

inv(z::Complex{<:Union{Float16,Float32}}) =
oftype(z, conj(widen(z))/abs2(widen(z)))
oftype(z, inv(widen(z)))

/(z::Complex{T}, w::Complex{T}) where {T<:Union{Float16,Float32}} =
oftype(z, widen(z)*inv(widen(w)))
Expand Down Expand Up @@ -430,6 +434,7 @@ end

function inv(w::ComplexF64)
c, d = reim(w)
(isinf(c) | isinf(d)) && return complex(copysign(0.0, c), flipsign(-0.0, d))
half = 0.5
two = 2.0
cd = max(abs(c), abs(d))
Expand Down
40 changes: 40 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,46 @@ end
end
end

@testset "division by Inf, issue#23134" begin
@testset "$T" for T in (Float32, Float64, BigFloat)
@test isequal(one(T) / complex(T(Inf)), complex(zero(T), -zero(T)))
@test isequal(one(T) / complex(T(Inf), one(T)), complex(zero(T), -zero(T)))
@test isequal(one(T) / complex(T(Inf), T(NaN)), complex(zero(T), -zero(T)))
@test isequal(one(T) / complex(T(Inf), T(Inf)), complex(zero(T), -zero(T)))

@test isequal(one(T) / complex(T(-Inf)), complex(-zero(T), -zero(T)))
@test isequal(one(T) / complex(T(-Inf), one(T)), complex(-zero(T), -zero(T)))
@test isequal(one(T) / complex(T(-Inf), T(NaN)), complex(-zero(T), -zero(T)))
@test isequal(one(T) / complex(T(-Inf), T(Inf)), complex(-zero(T), -zero(T)))

@test isequal(one(T) / complex(T(Inf),-zero(T)), complex(zero(T), zero(T)))
@test isequal(one(T) / complex(T(Inf),-one(T)), complex(zero(T), zero(T)))
@test isequal(one(T) / complex(T(Inf),T(-NaN)), complex(zero(T), zero(T)))
@test isequal(one(T) / complex(T(Inf),T(-Inf)), complex(zero(T), zero(T)))

@test isequal(one(T) / complex(T(-Inf),-zero(T)),complex(-zero(T), zero(T)))
@test isequal(one(T) / complex(T(-Inf),-one(T)), complex(-zero(T), zero(T)))
@test isequal(one(T) / complex(T(-Inf),T(-NaN)), complex(-zero(T), zero(T)))
@test isequal(one(T) / complex(T(-Inf),T(-Inf)), complex(-zero(T), zero(T)))

@test isequal(one(T) / complex(zero(T), T(Inf)), complex(zero(T), -zero(T)))
@test isequal(one(T) / complex(one(T), T(Inf)), complex(zero(T), -zero(T)))
@test isequal(one(T) / complex(T(NaN), T(Inf)), complex(zero(T), -zero(T)))

@test isequal(one(T) / complex(zero(T), T(-Inf)), complex(zero(T), zero(T)))
@test isequal(one(T) / complex(one(T), T(-Inf)), complex(zero(T), zero(T)))
@test isequal(one(T) / complex(T(NaN), T(-Inf)), complex(zero(T), zero(T)))

@test isequal(one(T) / complex(-zero(T), T(Inf)), complex(-zero(T), -zero(T)))
@test isequal(one(T) / complex(-one(T), T(Inf)), complex(-zero(T), -zero(T)))
@test isequal(one(T) / complex(T(-NaN), T(Inf)), complex(-zero(T), -zero(T)))

@test isequal(one(T) / complex(-zero(T), T(-Inf)), complex(-zero(T), zero(T)))
@test isequal(one(T) / complex(-one(T), T(-Inf)), complex(-zero(T), zero(T)))
@test isequal(one(T) / complex(T(-NaN), T(-Inf)), complex(-zero(T), zero(T)))
end
end

@testset "complex^real, issue #14342" begin
for T in (Float32, Float64, BigFloat), p in (T(-21//10), -21//10)
z = T(2)+0im
Expand Down