Skip to content

Remove one of the redundant round definitions; add test coverage #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading