Skip to content

Commit

Permalink
support prevfloat(x, int) (JuliaLang#28019)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreySarnoff authored and JeffBezanson committed Jul 11, 2018
1 parent 324b30d commit 93e8651
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 9 additions & 1 deletion base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ uabs(x::BitSigned) = unsigned(abs(x))


"""
nextfloat(x::AbstractFloat, n::Integer)
nextfloat(x::IEEEFloat, n::Integer)
The result of `n` iterative applications of `nextfloat` to `x` if `n >= 0`, or `-n`
applications of `prevfloat` if `n < 0`.
Expand Down Expand Up @@ -642,6 +642,14 @@ such `y` exists (e.g. if `x` is `Inf` or `NaN`), then return `x`.
"""
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` if `n < 0`.
"""
prevfloat(x::AbstractFloat, d::Integer) = nextfloat(x, -d)

"""
prevfloat(x::AbstractFloat)
Expand Down
7 changes: 5 additions & 2 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ end
end

for (a,b) in [(T(12.8),T(0.8)),
(prevfloat(realmin(T)), nextfloat(one(T),-2)),
(nextfloat(zero(T),3), T(0.75)),
(prevfloat(realmin(T)), prevfloat(one(T), 2)),
(prevfloat(realmin(T)), prevfloat(one(T), 2)),
(prevfloat(realmin(T)), nextfloat(one(T), -2)),
(nextfloat(zero(T), 3), T(0.75)),
(prevfloat(zero(T), -3), T(0.75)),
(nextfloat(zero(T)), T(0.5))]

n = Int(log2(a/b))
Expand Down

0 comments on commit 93e8651

Please sign in to comment.