Skip to content
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
25 changes: 13 additions & 12 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -859,23 +860,23 @@ 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)

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))
Copy link
Member

Choose a reason for hiding this comment

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

Now packages defining custom AbstractFloat are expected to define a method for Base._nextfloat?

Copy link
Member Author

Choose a reason for hiding this comment

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

no, they should define prevfloat separately and explicitly as the old fallback in Base was not correct, leading to behavior like

julia> prevfloat(Measurement{Float64}(1.0), 0x01)
1.0000000000000566 ± 0.0

Copy link
Member Author

Choose a reason for hiding this comment

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


"""
prevfloat(x::AbstractFloat)

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)
Expand Down
4 changes: 4 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down