Skip to content

Commit 881e08b

Browse files
authored
Support rounding Irrationals (#45598)
1 parent b3e8bd0 commit 881e08b

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

base/float.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,6 @@ trunc(::Type{Signed}, x::IEEEFloat) = trunc(Int,x)
379379
trunc(::Type{Unsigned}, x::IEEEFloat) = trunc(UInt,x)
380380
trunc(::Type{Integer}, x::IEEEFloat) = trunc(Int,x)
381381

382-
# fallbacks
383-
floor(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundDown))
384-
ceil(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundUp))
385-
round(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundNearest))
386-
387382
# Bool
388383
trunc(::Type{Bool}, x::AbstractFloat) = (-1 < x < 2) ? 1 <= x : throw(InexactError(:trunc, Bool, x))
389384
floor(::Type{Bool}, x::AbstractFloat) = (0 <= x < 2) ? 1 <= x : throw(InexactError(:floor, Bool, x))

base/floatfuncs.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ trunc(x::Real; kwargs...) = round(x, RoundToZero; kwargs...)
154154
floor(x::Real; kwargs...) = round(x, RoundDown; kwargs...)
155155
ceil(x::Real; kwargs...) = round(x, RoundUp; kwargs...)
156156

157+
# fallbacks
158+
trunc(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundToZero; kwargs...)
159+
floor(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundDown; kwargs...)
160+
ceil(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundUp; kwargs...)
161+
round(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundNearest; kwargs...)
162+
163+
round(::Type{T}, x::Real, r::RoundingMode) where {T} = convert(T, round(x, r))
164+
157165
round(x::Integer, r::RoundingMode) = x
158166

159167
# round x to multiples of 1/invstep

base/missing.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ round(::Type{T}, ::Missing, ::RoundingMode=RoundNearest) where {T} =
146146
throw(MissingException(missing_conversion_msg(T)))
147147
round(::Type{T}, x::Any, r::RoundingMode=RoundNearest) where {T>:Missing} = round(nonmissingtype_checked(T), x, r)
148148
# to fix ambiguities
149+
round(::Type{T}, x::Real, r::RoundingMode=RoundNearest) where {T>:Missing} = round(nonmissingtype_checked(T), x, r)
149150
round(::Type{T}, x::Rational{Tr}, r::RoundingMode=RoundNearest) where {T>:Missing,Tr} = round(nonmissingtype_checked(T), x, r)
150151
round(::Type{T}, x::Rational{Bool}, r::RoundingMode=RoundNearest) where {T>:Missing} = round(nonmissingtype_checked(T), x, r)
151152

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

base/rational.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,6 @@ for (S, T) in ((Rational, Integer), (Integer, Rational), (Rational, Rational))
484484
end
485485
end
486486

487-
trunc(::Type{T}, x::Rational) where {T} = round(T, x, RoundToZero)
488-
floor(::Type{T}, x::Rational) where {T} = round(T, x, RoundDown)
489-
ceil(::Type{T}, x::Rational) where {T} = round(T, x, RoundUp)
490-
491487
round(x::Rational, r::RoundingMode=RoundNearest) = round(typeof(x), x, r)
492488

493489
function round(::Type{T}, x::Rational{Tr}, r::RoundingMode=RoundNearest) where {T,Tr}

test/numbers.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,17 @@ Base.@irrational i46051 4863.185427757 1548big(pi)
11681168
# issue #46051
11691169
@test sprint(show, "text/plain", i46051) == "i46051 = 4863.185427757..."
11701170
end
1171+
1172+
@testset "Irrational round, float, ceil" begin
1173+
using .MathConstants
1174+
@test round(π) === 3.0
1175+
@test round(Int, ℯ) === 3
1176+
@test floor(ℯ) === 2.0
1177+
@test floor(Int, φ) === 1
1178+
@test ceil(γ) === 1.0
1179+
@test ceil(Int, catalan) === 1
1180+
end
1181+
11711182
@testset "issue #6365" begin
11721183
for T in (Float32, Float64)
11731184
for i = 9007199254740992:9007199254740996

0 commit comments

Comments
 (0)