Skip to content

Commit

Permalink
Support rounding Irrationals (#45598)
Browse files Browse the repository at this point in the history
  • Loading branch information
theabhirath authored Aug 3, 2023
1 parent b3e8bd0 commit 881e08b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
5 changes: 0 additions & 5 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,6 @@ trunc(::Type{Signed}, x::IEEEFloat) = trunc(Int,x)
trunc(::Type{Unsigned}, x::IEEEFloat) = trunc(UInt,x)
trunc(::Type{Integer}, x::IEEEFloat) = trunc(Int,x)

# fallbacks
floor(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundDown))
ceil(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundUp))
round(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundNearest))

# Bool
trunc(::Type{Bool}, x::AbstractFloat) = (-1 < x < 2) ? 1 <= x : throw(InexactError(:trunc, Bool, x))
floor(::Type{Bool}, x::AbstractFloat) = (0 <= x < 2) ? 1 <= x : throw(InexactError(:floor, Bool, x))
Expand Down
8 changes: 8 additions & 0 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ trunc(x::Real; kwargs...) = round(x, RoundToZero; kwargs...)
floor(x::Real; kwargs...) = round(x, RoundDown; kwargs...)
ceil(x::Real; kwargs...) = round(x, RoundUp; kwargs...)

# fallbacks
trunc(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundToZero; kwargs...)
floor(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundDown; kwargs...)
ceil(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundUp; kwargs...)
round(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundNearest; kwargs...)

round(::Type{T}, x::Real, r::RoundingMode) where {T} = convert(T, round(x, r))

round(x::Integer, r::RoundingMode) = x

# round x to multiples of 1/invstep
Expand Down
2 changes: 2 additions & 0 deletions base/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ round(::Type{T}, ::Missing, ::RoundingMode=RoundNearest) where {T} =
throw(MissingException(missing_conversion_msg(T)))
round(::Type{T}, x::Any, r::RoundingMode=RoundNearest) where {T>:Missing} = round(nonmissingtype_checked(T), x, r)
# to fix ambiguities
round(::Type{T}, x::Real, r::RoundingMode=RoundNearest) where {T>:Missing} = round(nonmissingtype_checked(T), x, r)
round(::Type{T}, x::Rational{Tr}, r::RoundingMode=RoundNearest) where {T>:Missing,Tr} = round(nonmissingtype_checked(T), x, r)
round(::Type{T}, x::Rational{Bool}, r::RoundingMode=RoundNearest) where {T>:Missing} = round(nonmissingtype_checked(T), x, r)

Expand All @@ -158,6 +159,7 @@ for f in (:(ceil), :(floor), :(trunc))
($f)(::Type{T}, x::Any) where {T>:Missing} = $f(nonmissingtype_checked(T), x)
# to fix ambiguities
($f)(::Type{T}, x::Rational) where {T>:Missing} = $f(nonmissingtype_checked(T), x)
($f)(::Type{T}, x::Real) where {T>:Missing} = $f(nonmissingtype_checked(T), x)
end
end

Expand Down
4 changes: 0 additions & 4 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,6 @@ for (S, T) in ((Rational, Integer), (Integer, Rational), (Rational, Rational))
end
end

trunc(::Type{T}, x::Rational) where {T} = round(T, x, RoundToZero)
floor(::Type{T}, x::Rational) where {T} = round(T, x, RoundDown)
ceil(::Type{T}, x::Rational) where {T} = round(T, x, RoundUp)

round(x::Rational, r::RoundingMode=RoundNearest) = round(typeof(x), x, r)

function round(::Type{T}, x::Rational{Tr}, r::RoundingMode=RoundNearest) where {T,Tr}
Expand Down
11 changes: 11 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,17 @@ Base.@irrational i46051 4863.185427757 1548big(pi)
# issue #46051
@test sprint(show, "text/plain", i46051) == "i46051 = 4863.185427757..."
end

@testset "Irrational round, float, ceil" begin
using .MathConstants
@test round(π) === 3.0
@test round(Int, ℯ) === 3
@test floor(ℯ) === 2.0
@test floor(Int, φ) === 1
@test ceil(γ) === 1.0
@test ceil(Int, catalan) === 1
end

@testset "issue #6365" begin
for T in (Float32, Float64)
for i = 9007199254740992:9007199254740996
Expand Down

4 comments on commit 881e08b

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package evaluation job you requested has completed - possible new issues were detected.
The full report is available.

@vtjnash
Copy link
Member

@vtjnash vtjnash commented on 881e08b Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

Please sign in to comment.