Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behaviour when comparing -0.0 with 0.0 #168

Open
andreasvarga opened this issue Mar 10, 2023 · 1 comment
Open

Inconsistent behaviour when comparing -0.0 with 0.0 #168

andreasvarga opened this issue Mar 10, 2023 · 1 comment

Comments

@andreasvarga
Copy link

The following behaviour I detected performing the function issymmetric on a symmetric matrix (thus preventing a correct test).

julia> C = Matrix(Hermitian(Complex{Double64}[1 0;0 1]))
2×2 Matrix{Complex{Double64}}:
 1.0 + 0.0im  0.0 + 0.0im
 0.0 - 0.0im  1.0 + 0.0im

julia> ishermitian(C)
true

julia> Ce = [real(C) imag(C); -imag(C) real(C)]  # construct a symmetric real matrix 
4×4 Matrix{Double64}:
  1.0   0.0   0.0  0.0
  0.0   1.0  -0.0  0.0
 -0.0  -0.0   1.0  0.0
  0.0  -0.0   0.0  1.0

julia> issymmetric(Ce)  # this test is in my opinion incorrect
false

julia> Ce == transpose(Ce)   # the matrix is symmetric as expected
true

The invalid test can be simply reproduced as shown bellow:


julia> y = -Double64(0)
-0.0

julia> z = Double64(0)
0.0

julia> y === z
false

julia> y == z
true

julia> y != z
true

@andreasvarga
Copy link
Author

I wonder if replacing in compare.jl

@inline function (!=)(x::DoubleFloat{T}, y::DoubleFloat{T}) where {T<:IEEEFloat}
    return (LO(x) !== LO(y)) || (HI(x) !== HI(y) || posnegzeros(HI(x),HI(y)))
end

with

@inline function (!=)(x::DoubleFloat{T}, y::DoubleFloat{T}) where {T<:IEEEFloat}
    return (LO(x) !== LO(y)) || (HI(x) !== HI(y) && !posnegzeros(HI(x),HI(y)))
end

would fix the problem.

This corresponds to what is implemented in Base in operators.jl

!=(x, y) = !(x == y)

An equivalent fix would be

@inline function (!=)(x::DoubleFloat{T}, y::DoubleFloat{T}) where {T<:IEEEFloat}
    return !(==)(x,y)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant