diff --git a/base/float.jl b/base/float.jl index 7b3aec3f7fe42..515c94804e896 100644 --- a/base/float.jl +++ b/base/float.jl @@ -807,13 +807,8 @@ precision(::Type{T}; base::Integer=2) where {T<:AbstractFloat} = _precision(T, b precision(::T; base::Integer=2) where {T<:AbstractFloat} = precision(T; base) -""" - nextfloat(x::AbstractFloat, n::Integer) - -The result of `n` iterative applications of `nextfloat` to `x` if `n >= 0`, or `-n` -applications of [`prevfloat`](@ref) if `n < 0`. -""" -function nextfloat(f::IEEEFloat, d::Integer) +function _nextfloat(f::IEEEFloat, dneg::Bool, da::Integer) + # da must be > 0 F = typeof(f) fumax = reinterpret(Unsigned, F(Inf)) U = typeof(fumax) @@ -823,8 +818,6 @@ function nextfloat(f::IEEEFloat, d::Integer) fneg = fi < 0 fu = unsigned(fi & typemax(fi)) - dneg = d < 0 - da = uabs(d) if da > typemax(U) fneg = dneg fu = fumax @@ -851,6 +844,14 @@ function nextfloat(f::IEEEFloat, d::Integer) reinterpret(F, fu) end +""" + nextfloat(x::AbstractFloat, n::Integer) + +The result of `n` iterative applications of `nextfloat` to `x` if `n >= 0`, or `-n` +applications of [`prevfloat`](@ref) if `n < 0`. +""" +nextfloat(f::AbstractFloat, d::Integer) = _nextfloat(f, isnegative(d), uabs(d)) + """ nextfloat(x::AbstractFloat) @@ -859,7 +860,7 @@ If no such `y` exists (e.g. if `x` is `Inf` or `NaN`), then return `x`. See also: [`prevfloat`](@ref), [`eps`](@ref), [`issubnormal`](@ref). """ -nextfloat(x::AbstractFloat) = nextfloat(x,1) +nextfloat(x::AbstractFloat) = nextfloat(x, 1) """ prevfloat(x::AbstractFloat, n::Integer) @@ -867,7 +868,7 @@ nextfloat(x::AbstractFloat) = nextfloat(x,1) The result of `n` iterative applications of `prevfloat` to `x` if `n >= 0`, or `-n` applications of [`nextfloat`](@ref) if `n < 0`. """ -prevfloat(x::AbstractFloat, d::Integer) = nextfloat(x, -d) +prevfloat(x::AbstractFloat, d::Integer) = _nextfloat(x, ispositive(d), uabs(d)) """ prevfloat(x::AbstractFloat) @@ -875,7 +876,7 @@ prevfloat(x::AbstractFloat, d::Integer) = nextfloat(x, -d) Return the largest floating point number `y` of the same type as `x` such that `y < x`. If no such `y` exists (e.g. if `x` is `-Inf` or `NaN`), then return `x`. """ -prevfloat(x::AbstractFloat) = nextfloat(x,-1) +prevfloat(x::AbstractFloat) = nextfloat(x, -1) for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128) for Tf in (Float16, Float32, Float64) diff --git a/test/numbers.jl b/test/numbers.jl index faaa3d293626d..d2bc2317da948 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -2191,6 +2191,10 @@ end @test nextfloat(Inf32) === Inf32 @test prevfloat(-Inf32) === -Inf32 @test isequal(nextfloat(NaN32), NaN32) + @test nextfloat(1.0, UInt(5)) == nextfloat(1.0, 5) + @test prevfloat(1.0, UInt(5)) == prevfloat(1.0, 5) + @test nextfloat(0.0, typemax(UInt64)) == Inf + @test prevfloat(0.0, typemax(UInt64)) == -Inf end @testset "issue #16206" begin @test prevfloat(Inf) == 1.7976931348623157e308