Skip to content

Commit

Permalink
Fix promote rules for Mod (#162)
Browse files Browse the repository at this point in the history
* Fix promote rules for `Mod`

* Fix a test

* Bump version

* Fix promotion with large integers, rework `is_prime`
  • Loading branch information
mtsch authored Dec 28, 2022
1 parent be521a2 commit 3f2d63f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
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 <matijacufar@gmail.com>"]
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

2 comments on commit 3f2d63f

@mtsch
Copy link
Owner Author

@mtsch mtsch commented on 3f2d63f Dec 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/74750

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.16.12 -m "<description of version>" 3f2d63fae79dff1acccc6f40695a492f04e37536
git push origin v0.16.12

Please sign in to comment.