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

Add more methods to Base.:(==) #139

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ Base.://(x::Quaternion, y::Real) = quat(real(x)//y, imag_part(x).//y...)
Base.://(x::Number, y::Quaternion) = x*conj(y)//abs2(y)

Base.:(==)(q::Quaternion, w::Quaternion) = (q.s == w.s) & (q.v1 == w.v1) & (q.v2 == w.v2) & (q.v3 == w.v3)
Base.:(==)(q::Quaternion, x::Real) = isreal(q) && real(q) == x
Base.:(==)(x::Real, q::Quaternion) = isreal(q) && real(q) == x
Base.:(==)(q::Quaternion, z::Complex) = isreal(q) && isreal(z) && real(q) == real(z)
Base.:(==)(z::Complex, q::Quaternion) = isreal(q) && isreal(z) && real(q) == real(z)

function Base.isequal(q::Quaternion, w::Quaternion)
isequal(q.s, w.s) & isequal(q.v1, w.v1) & isequal(q.v2, w.v2) & isequal(q.v3, w.v3)
end
Expand Down
18 changes: 16 additions & 2 deletions test/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ end
@test Quaternion(1, 2, 3, 4) != Quaternion(1, 5, 3, 4)
@test Quaternion(1, 2, 3, 4) != Quaternion(1, 2, 5, 4)
@test Quaternion(1, 2, 3, 4) != Quaternion(1, 2, 3, 5)

@test Quaternion(1, 2, 3, 4) != 1
@test Quaternion(1, 0, 0, 0) == 1
@test Quaternion(1, 0, 0, 0) != 0
@test 1 != Quaternion(1, 2, 3, 4)
@test 1 == Quaternion(1, 0, 0, 0)
@test 0 != Quaternion(1, 0, 0, 0)

@test Quaternion(1, 0, 0, 0) == Complex(1, 0)
@test Quaternion(1, 0, 0, 0) != Complex(1, 1)
@test Quaternion(1, 0, 0, 0) != Complex(0, 1)
@test Quaternion(0, 1, 0, 0) != Complex(0, 1)
@test Complex(1, 0) == Quaternion(1, 0, 0, 0)
@test Complex(1, 1) != Quaternion(1, 0, 0, 0)
@test Complex(0, 1) != Quaternion(1, 0, 0, 0)
@test Complex(0, 1) != Quaternion(0, 1, 0, 0)
end

@testset "isequal" begin
Expand All @@ -83,8 +99,6 @@ end
(Quaternion(1.0, 2, 3, 4), Quaternion(2.0))
@test promote(Quaternion(1.0f0), Quaternion(2.0)) ===
(Quaternion(1.0), Quaternion(2.0))

@test Quaternion(1) == 1.0
end

@testset "shorthands" begin
Expand Down