Skip to content

Commit

Permalink
Merge pull request #74 from nickrobinson251/npr/73-rational-promotion
Browse files Browse the repository at this point in the history
Fix promotion between FixedDecimal and Rational
  • Loading branch information
NHDaly authored Sep 8, 2022
2 parents 460eb8f + f4f9494 commit 93cec87
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/FixedPointDecimals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ end

Base.promote_rule(::Type{FD{T, f}}, ::Type{<:Integer}) where {T, f} = FD{T, f}
Base.promote_rule(::Type{<:FD}, ::Type{TF}) where {TF <: AbstractFloat} = TF
Base.promote_rule(::Type{<:FD}, ::Type{Rational{TR}}) where {TR} = Rational{TR}
Base.promote_rule(::Type{<:FD{T}}, ::Type{Rational{TR}}) where {T, TR} = Rational{promote_type(T, TR)}

# TODO: decide if these are the right semantics;
# right now we pick the bigger int type and the bigger decimal point
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ end
@test 1 + FD2(0.1) === FD2(1.1)
@test FD2(0.1) + FD4(0.0001) === FD4(0.1001)
@test WFD2(0.1) + FD4(0.0001) === WFD4(0.1001)

# promotion with Rational
# see https://github.com/JuliaMath/FixedPointDecimals.jl/issues/73
r = Rational{Int8}(1//1)
fd = FixedDecimal{Int128,4}(2.5806)
@test (r + fd) isa Rational{Int128}
end

@testset "float" begin
Expand Down

0 comments on commit 93cec87

Please sign in to comment.