Skip to content

Commit 12be806

Browse files
committed
Fix promotion rules for FixedRational
1 parent 3a73997 commit 12be806

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/fixed_rational.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ end
7878
function Base.promote_rule(::Type{<:FixedRational{T1}}, ::Type{Rational{T2}}) where {T1,T2}
7979
return Rational{promote_type(T1,T2)}
8080
end
81-
function Base.promote_rule(::Type{<:FixedRational{T1}}, ::Type{T2}) where {T1,T2}
81+
function Base.promote_rule(::Type{<:FixedRational{T1}}, ::Type{T2}) where {T1,T2<:Real}
8282
return promote_type(Rational{T1}, T2)
8383
end
84-
85-
# Want to consume integers:
86-
Base.promote(x::Integer, y::F) where {F<:FixedRational} = (F(x), y)
87-
Base.promote(x::F, y::Integer) where {F<:FixedRational} = reverse(promote(y, x))
84+
function Base.promote_rule(::Type{F}, ::Type{<:Integer}) where {F<:FixedRational}
85+
# Want to consume integers:
86+
return F
87+
end
8888

8989
Base.string(x::FixedRational) =
9090
let
@@ -93,7 +93,6 @@ Base.string(x::FixedRational) =
9393
return string(div(x.num, g)) * "//" * string(div(denom(x), g))
9494
end
9595
Base.show(io::IO, x::FixedRational) = print(io, string(x))
96-
Base.zero(::Type{F}) where {F<:FixedRational} = unsafe_fixed_rational(0, eltype(F), val_denom(F))
9796

9897
tryrationalize(::Type{F}, x::F) where {F<:FixedRational} = x
9998
tryrationalize(::Type{F}, x::Union{Rational,Integer}) where {F<:FixedRational} = convert(F, x)

test/unittests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ end
433433
# Promotion rules
434434
@test promote_type(FixedRational{Int64,10},FixedRational{BigInt,10}) == FixedRational{BigInt,10}
435435
@test promote_type(Rational{Int8}, FixedRational{Int,12345}) == Rational{Int}
436-
@test promote_type(Int8, FixedRational{Int,12345}) == promote_type(Int8, Rational{Int})
436+
@test promote_type(Int8, FixedRational{Int,12345}) == FixedRational{Int,12345}
437437
end
438438

439439
@testset "Quantity promotion" begin

0 commit comments

Comments
 (0)