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

Fix promote rules for Mod #162

Merged
merged 4 commits into from
Dec 28, 2022
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Ripserer"
uuid = "aa79e827-bd0b-42a8-9f10-2b302677a641"
authors = ["mtsch <[email protected]>"]
version = "0.16.11"
version = "0.16.12"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
11 changes: 9 additions & 2 deletions src/base/primefield.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Return `true` if `n` is a prime number.
"""
Base.@pure function is_prime(n::Int)
function is_prime(n::Int)
if iseven(n) || n < 2
return n == 2
else
Expand Down Expand Up @@ -81,6 +81,13 @@ Base.zero(::Type{Mod{M}}) where {M} = Mod{M}(0, false)
Base.one(::Type{Mod{M}}) where {M} = Mod{M}(1, false)
Base.sign(i::M) where {M<:Mod} = ifelse(iszero(i), zero(M), one(M))

Base.promote_rule(::Type{Mod{M}}, ::Type{<:Integer}) where {M} = Mod{M}
Base.promote_rule(::Type{Mod{M}}, ::Type{<:Mod}) where {M} = Union{}
function Base.promote_rule(::Type{Mod{M}}, ::Type{I}) where {M,I<:Integer}
if Base.promote_type(I, Int128) === Int128 || Base.promote_type(I, Int128) === UInt128
return Mod{M}
else
return Union{}
end
end

Base.inv(i::Mod{M}) where {M} = Mod{M}(invmod(Int(i), M), false)
2 changes: 1 addition & 1 deletion test/base/primefield.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ end
@test_throws ErrorException Mod{3}(1) < Mod{3}(2)
@test_throws ErrorException Mod{3}(1) ≥ Mod{3}(2)

@test_throws StackOverflowError Mod{3}(1) + Mod{2}(1)
@test_throws ErrorException Mod{3}(1) + Mod{2}(1)
end

@testset "Printing" begin
Expand Down