Skip to content

Commit

Permalink
Remove one of the redundant round definitions; add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
NHDaly committed Feb 17, 2025
1 parent 515934e commit dc53d19
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/FixedPointDecimals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ for fn in [:trunc, :floor, :ceil]
val = _apply_exact_float($(Symbol(fn, "mul")), T, x, powt)
reinterpret(FD{T, f}, val)
end
# needed to avoid ambiguity
# needed to avoid ambiguity with e.g. `floor(::Type{T}, x::Rational)`
@eval function Base.$fn(::Type{FD{T, f}}, x::Rational) where {T, f}
reinterpret(FD{T, f}, $fn(T, x * coefficient(FD{T, f})))
end
Expand All @@ -309,15 +309,12 @@ function Base.round(::Type{FD{T, f}}, x::Real, ::RoundingMode{:Nearest}=RoundNea
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))
end

# needed to avoid ambiguity
function Base.round(::Type{FD{T, f}}, x::Rational, ::RoundingMode{:Nearest}=RoundNearest) where {T, f}
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))
end
# needed to avoid ambiguity with `round(::Type{T}, x::Rational{Bool}, ::RoundingMode)`
function Base.round(::Type{FD{T, f}}, x::Rational{Bool}, ::RoundingMode{:Nearest}=RoundNearest) where {T, f}
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))
end
function Base.round(::Type{FD{T, f}}, x::Rational{Tr}, ::RoundingMode{:Nearest}=RoundNearest) where {T, f, Tr}
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))
end


Expand Down
3 changes: 3 additions & 0 deletions test/FixedDecimal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1533,5 +1533,8 @@ end
@test ceil(FD3, 4//3) == FD3(1.334)
@test round(FD3, true) == FD3(1.000)
@test round(FD3, 4//3) == FD3(1.333)
@test round(FD3, 1 // 1) == FD3(1)
@test round(FD3, BigInt(1) // 1) == FD3(1)
@test round(FD3, true // true) == FD3(1)
@test Bool(FixedDecimal{Int,4}(1))
end
1 change: 1 addition & 0 deletions test/aqua_test.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Test, FixedPointDecimals
@testset "Aqua" begin
using Aqua
Aqua.test_all(FixedPointDecimals)
Expand Down

0 comments on commit dc53d19

Please sign in to comment.