Skip to content

Commit 3f2d63f

Browse files
authored
Fix promote rules for Mod (#162)
* Fix promote rules for `Mod` * Fix a test * Bump version * Fix promotion with large integers, rework `is_prime`
1 parent be521a2 commit 3f2d63f

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Ripserer"
22
uuid = "aa79e827-bd0b-42a8-9f10-2b302677a641"
33
authors = ["mtsch <matijacufar@gmail.com>"]
4-
version = "0.16.11"
4+
version = "0.16.12"
55

66
[deps]
77
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"

src/base/primefield.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
Return `true` if `n` is a prime number.
66
"""
7-
Base.@pure function is_prime(n::Int)
7+
function is_prime(n::Int)
88
if iseven(n) || n < 2
99
return n == 2
1010
else
@@ -81,6 +81,13 @@ Base.zero(::Type{Mod{M}}) where {M} = Mod{M}(0, false)
8181
Base.one(::Type{Mod{M}}) where {M} = Mod{M}(1, false)
8282
Base.sign(i::M) where {M<:Mod} = ifelse(iszero(i), zero(M), one(M))
8383

84-
Base.promote_rule(::Type{Mod{M}}, ::Type{<:Integer}) where {M} = Mod{M}
84+
Base.promote_rule(::Type{Mod{M}}, ::Type{<:Mod}) where {M} = Union{}
85+
function Base.promote_rule(::Type{Mod{M}}, ::Type{I}) where {M,I<:Integer}
86+
if Base.promote_type(I, Int128) === Int128 || Base.promote_type(I, Int128) === UInt128
87+
return Mod{M}
88+
else
89+
return Union{}
90+
end
91+
end
8592

8693
Base.inv(i::Mod{M}) where {M} = Mod{M}(invmod(Int(i), M), false)

test/base/primefield.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ end
5353
@test_throws ErrorException Mod{3}(1) < Mod{3}(2)
5454
@test_throws ErrorException Mod{3}(1) Mod{3}(2)
5555

56-
@test_throws StackOverflowError Mod{3}(1) + Mod{2}(1)
56+
@test_throws ErrorException Mod{3}(1) + Mod{2}(1)
5757
end
5858

5959
@testset "Printing" begin

0 commit comments

Comments
 (0)