Skip to content

Commit 3ce0e4f

Browse files
committed
fix prevfloat with unsigned stepcount
1 parent ad39f65 commit 3ce0e4f

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

base/float.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -807,13 +807,7 @@ precision(::Type{T}; base::Integer=2) where {T<:AbstractFloat} = _precision(T, b
807807
precision(::T; base::Integer=2) where {T<:AbstractFloat} = precision(T; base)
808808

809809

810-
"""
811-
nextfloat(x::AbstractFloat, n::Integer)
812-
813-
The result of `n` iterative applications of `nextfloat` to `x` if `n >= 0`, or `-n`
814-
applications of [`prevfloat`](@ref) if `n < 0`.
815-
"""
816-
function nextfloat(f::IEEEFloat, d::Integer)
810+
function _nextfloat(f::IEEEFloat, dneg::Bool, da::Unsigned)
817811
F = typeof(f)
818812
fumax = reinterpret(Unsigned, F(Inf))
819813
U = typeof(fumax)
@@ -823,8 +817,6 @@ function nextfloat(f::IEEEFloat, d::Integer)
823817
fneg = fi < 0
824818
fu = unsigned(fi & typemax(fi))
825819

826-
dneg = d < 0
827-
da = uabs(d)
828820
if da > typemax(U)
829821
fneg = dneg
830822
fu = fumax
@@ -851,6 +843,15 @@ function nextfloat(f::IEEEFloat, d::Integer)
851843
reinterpret(F, fu)
852844
end
853845

846+
"""
847+
nextfloat(x::AbstractFloat, n::Integer)
848+
849+
The result of `n` iterative applications of `nextfloat` to `x` if `n >= 0`, or `-n`
850+
applications of [`prevfloat`](@ref) if `n < 0`.
851+
"""
852+
nextfloat(f::AbstractFloat, d::Integer) = _nextfloat(f, d < 0, uabs(d))
853+
nextfloat(f::IEEEFloat, d::Unsigned) = _nextfloat(f, false, d)
854+
854855
"""
855856
nextfloat(x::AbstractFloat)
856857
@@ -859,23 +860,23 @@ If no such `y` exists (e.g. if `x` is `Inf` or `NaN`), then return `x`.
859860
860861
See also: [`prevfloat`](@ref), [`eps`](@ref), [`issubnormal`](@ref).
861862
"""
862-
nextfloat(x::AbstractFloat) = nextfloat(x,1)
863+
nextfloat(x::AbstractFloat) = nextfloat(x, 1)
863864

864865
"""
865866
prevfloat(x::AbstractFloat, n::Integer)
866867
867868
The result of `n` iterative applications of `prevfloat` to `x` if `n >= 0`, or `-n`
868869
applications of [`nextfloat`](@ref) if `n < 0`.
869870
"""
870-
prevfloat(x::AbstractFloat, d::Integer) = nextfloat(x, -d)
871+
prevfloat(x::AbstractFloat, d::Integer) = _nextfloat(x, d > 0, uabs(d))
871872

872873
"""
873874
prevfloat(x::AbstractFloat)
874875
875876
Return the largest floating point number `y` of the same type as `x` such that `y < x`.
876877
If no such `y` exists (e.g. if `x` is `-Inf` or `NaN`), then return `x`.
877878
"""
878-
prevfloat(x::AbstractFloat) = nextfloat(x,-1)
879+
prevfloat(x::AbstractFloat) = nextfloat(x, -1)
879880

880881
for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
881882
for Tf in (Float16, Float32, Float64)

test/numbers.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,10 @@ end
21912191
@test nextfloat(Inf32) === Inf32
21922192
@test prevfloat(-Inf32) === -Inf32
21932193
@test isequal(nextfloat(NaN32), NaN32)
2194+
@test nextfloat(1.0, UInt(5)) == nextfloat(1.0, 5)
2195+
@test prevfloat(1.0, UInt(5)) == prevfloat(1.0, 5)
2196+
@test nextfloat(0.0, typemax(UInt64)) == Inf
2197+
@test prevfloat(0.0, typemax(UInt64)) == -Inf
21942198
end
21952199
@testset "issue #16206" begin
21962200
@test prevfloat(Inf) == 1.7976931348623157e308

0 commit comments

Comments
 (0)