Skip to content

Commit

Permalink
Restrict round signature to avoid ambiguities
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Oct 18, 2023
1 parent 4f99e9e commit 83fd4d0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/HeckeMiscInteger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ for sym in (:trunc, :round, :ceil, :floor)
@eval begin
# support `trunc(ZZRingElem, 1.23)` etc. for arbitrary reals
Base.$sym(::Type{ZZRingElem}, a::Real) = ZZRingElem(Base.$sym(BigInt, a))
Base.$sym(::Type{ZZRingElem}, a::Rational) = ZZRingElem(Base.$sym(BigInt, a))
Base.$sym(::Type{ZZRingElem}, a::Rational{T}) where T = ZZRingElem(Base.$sym(BigInt, a))
Base.$sym(::Type{ZZRingElem}, a::Rational{Bool}) = ZZRingElem(Base.$sym(BigInt, a))

Check warning on line 261 in src/HeckeMiscInteger.jl

View check run for this annotation

Codecov / codecov/patch

src/HeckeMiscInteger.jl#L261

Added line #L261 was not covered by tests

# for integers we don't need to round in between
Base.$sym(::Type{ZZRingElem}, a::Integer) = ZZRingElem(a)
Expand Down
10 changes: 5 additions & 5 deletions src/flint/fmpq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ Base.trunc(::Type{QQFieldElem}, a::QQFieldElem) = QQFieldElem(trunc(ZZRingElem,
Base.trunc(::Type{ZZRingElem}, a::QQFieldElem) = is_positive(a) ? floor(ZZRingElem, a) : ceil(ZZRingElem, a)

Base.round(x::QQFieldElem, ::RoundingMode{:Up}) = ceil(x)
Base.round(::Type{T}, x::QQFieldElem, ::RoundingMode{:Up}) where T = ceil(T, x)
Base.round(::Type{T}, x::QQFieldElem, ::RoundingMode{:Up}) where T <: RingElement = ceil(T, x)

Base.round(x::QQFieldElem, ::RoundingMode{:Down}) = floor(x)
Base.round(::Type{T}, x::QQFieldElem, ::RoundingMode{:Down}) where T = floor(T, x)
Base.round(::Type{T}, x::QQFieldElem, ::RoundingMode{:Down}) where T <: RingElement = floor(T, x)

Base.round(x::QQFieldElem, ::RoundingMode{:Nearest}) = round(QQFieldElem, x, RoundNearest)
function Base.round(::Type{T}, x::QQFieldElem, ::RoundingMode{:Nearest}) where T
function Base.round(::Type{T}, x::QQFieldElem, ::RoundingMode{:Nearest}) where T <: RingElement
d = denominator(x)
n = numerator(x)
if d == 2
Expand All @@ -219,13 +219,13 @@ function Base.round(::Type{T}, x::QQFieldElem, ::RoundingMode{:Nearest}) where T
end

Base.round(x::QQFieldElem, ::RoundingMode{:NearestTiesAway}) = sign(x) * floor(abs(x) + 1 // 2)
function Base.round(::Type{T}, x::QQFieldElem, ::RoundingMode{:NearestTiesAway}) where T
function Base.round(::Type{T}, x::QQFieldElem, ::RoundingMode{:NearestTiesAway}) where T <: RingElement
tmp = floor(T, abs(x) + 1 // 2)
return is_positive(x) ? tmp : -tmp
end

Base.round(a::QQFieldElem) = round(QQFieldElem, a)
Base.round(::Type{T}, a::QQFieldElem) where T = round(T, a, RoundNearestTiesAway)
Base.round(::Type{T}, a::QQFieldElem) where T <: RingElement = round(T, a, RoundNearestTiesAway)


nbits(a::QQFieldElem) = nbits(numerator(a)) + nbits(denominator(a))
Expand Down

0 comments on commit 83fd4d0

Please sign in to comment.