Skip to content

Commit 632b2a6

Browse files
fix: fix comparison involving NaN (#178)
* fix: fix comparison involving `NaN` * test: add allocation tests to NaN equality checking
1 parent f8bfd2f commit 632b2a6

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/comp.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,28 @@ end
5858
(==)(x::MonomialVector, mv::AbstractVector) = x == monomial_vector(mv)
5959

6060
# Comparison of Term
61-
function (==)(p::Polynomial{V,M}, q::Polynomial{V,M}) where {V,M}
61+
function _compare(p::Polynomial{V,M}, q::Polynomial{V,M}, comparator) where {V,M}
6262
# terms should be sorted and without zeros
6363
if MP.nterms(p) != MP.nterms(q)
6464
return false
6565
end
6666
for i in eachindex(p.a)
67-
if p.x[i] != q.x[i]
67+
if !comparator(p.x[i], q.x[i])
6868
# There should not be zero terms
6969
@assert p.a[i] != 0
7070
@assert q.a[i] != 0
7171
return false
7272
end
73-
if p.a[i] != q.a[i]
73+
if !comparator(p.a[i], q.a[i])
7474
return false
7575
end
7676
end
7777
return true
7878
end
7979

80+
(==)(p::Polynomial{V, M}, q::Polynomial{V, M}) where {V, M} = _compare(p, q, (==))
81+
Base.isequal(p::Polynomial{V, M}, q::Polynomial{V, M}) where {V, M} = _compare(p, q, isequal)
82+
8083
function Base.isapprox(
8184
p::Polynomial{V,M,S},
8285
q::Polynomial{V,M,T};

test/comp.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,17 @@ end
9797
_test_monomials([x, y], 1:2, [y, x, y^2, x * y, x^2])
9898
_test_monomials([x, y], [0, 1, 3], [1, y, x, y^3, x*y^2, x^2*y, x^3])
9999
end
100+
101+
@testset "Comparison with NaN" begin
102+
@polyvar p
103+
poly1 = NaN + p
104+
poly2 = NaN + p
105+
@test poly1 != poly2
106+
@test (@allocated poly1 != poly2) == 0
107+
@test poly1 != poly1
108+
@test (@allocated poly1 != poly1) == 0
109+
@test isequal(poly1, poly2)
110+
@test (@allocated isequal(poly1, poly2)) == 0
111+
@test isequal(poly1, poly1)
112+
@test (@allocated isequal(poly1, poly1)) == 0
113+
end

0 commit comments

Comments
 (0)