Skip to content

Commit

Permalink
be careful about signed zero for complex real isequal (JuliaLang#48997)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscardssmith authored Mar 25, 2023
1 parent 88a1fc7 commit 4853a52
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ bswap(z::Complex) = Complex(bswap(real(z)), bswap(imag(z)))
==(x::Real, z::Complex) = isreal(z) && real(z) == x

isequal(z::Complex, w::Complex) = isequal(real(z),real(w)) & isequal(imag(z),imag(w))
isequal(z::Complex, w::Real) = isequal(real(z),w) & isequal(imag(z),zero(w))
isequal(z::Real, w::Complex) = isequal(z,real(w)) & isequal(zero(z),imag(w))

in(x::Complex, r::AbstractRange{<:Real}) = isreal(x) && real(x) in r

Expand Down
7 changes: 6 additions & 1 deletion test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ end
@testset for T in (Float16, Float32, Float64, BigFloat)
t = true
f = false

@testset "equality" begin
@test isequal(T(0.0)*im, T(0.0))
@test !isequal(T(0.0)*im, T(-0.0))
@test isequal(Complex(T(-0.0), T(0.0)), T(-0.0))
@test !isequal(T(-0.0)*im, T(-0.0))
end
@testset "add and subtract" begin
@test isequal(T(+0.0) + im, Complex(T(+0.0), T(+1.0)))
@test isequal(T(-0.0) + im, Complex(T(-0.0), T(+1.0)))
Expand Down

0 comments on commit 4853a52

Please sign in to comment.