diff --git a/NEWS.md b/NEWS.md index 91fb4bf6d3b5f..62683964eb884 100644 --- a/NEWS.md +++ b/NEWS.md @@ -111,6 +111,7 @@ New library features * `extrema` now accepts an `init` keyword argument ([#36265], [#43604]). * `Iterators.countfrom` now accepts any type that defines `+` ([#37747]). * `@time` now separates out % time spent recompiling invalidated methods ([#45015]). +* An issue with order of operations in `fld1` is now fixed ([#28973]). Standard library changes ------------------------ diff --git a/base/operators.jl b/base/operators.jl index e42699062f016..e8e31ec47cf16 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -855,7 +855,7 @@ julia> x == (fld1(x, y) - 1) * y + mod1(x, y) true ``` """ -fld1(x::T, y::T) where {T<:Real} = (m = mod1(x, y); fld(x + y - m, y)) +fld1(x::T, y::T) where {T<:Real} = (m = mod1(x, y); fld((x - m) + y, y)) function fld1(x::T, y::T) where T<:Integer d = div(x, y) return d + (!signbit(x ⊻ y) & (d * y != x)) diff --git a/test/operators.jl b/test/operators.jl index 1c5d2d33bf0f8..432456a2a3b42 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -261,6 +261,9 @@ end end @test fldmod1(4.0, 3) == fldmod1(4, 3) + + # issue 28973 + @test fld1(0.4, 0.9) == fld1(nextfloat(0.4), 0.9) == 1.0 end @testset "Fix12" begin