Skip to content

Commit fcdc5bc

Browse files
bugfix: fld1 order of ops (#46938)
* bugfix: fld1 order of ops fixes #28973
1 parent 1720a54 commit fcdc5bc

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Library changes
8989
* `eachslice` now works over multiple dimensions; `eachslice`, `eachrow` and `eachcol` return
9090
a `Slices` object, which allows dispatching to provide more efficient methods ([#32310]).
9191
* `@kwdef` is now exported and added to the public API ([#46273])
92+
* An issue with order of operations in `fld1` is now fixed ([#28973]).
9293

9394
Standard library changes
9495
------------------------

base/operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ julia> x == (fld1(x, y) - 1) * y + mod1(x, y)
842842
true
843843
```
844844
"""
845-
fld1(x::T, y::T) where {T<:Real} = (m = mod1(x, y); fld(x + y - m, y))
845+
fld1(x::T, y::T) where {T<:Real} = (m = mod1(x, y); fld((x - m) + y, y))
846846
function fld1(x::T, y::T) where T<:Integer
847847
d = div(x, y)
848848
return d + (!signbit(x y) & (d * y != x))

test/operators.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ end
274274
end
275275

276276
@test fldmod1(4.0, 3) == fldmod1(4, 3)
277+
278+
# issue 28973
279+
@test fld1(0.4, 0.9) == fld1(nextfloat(0.4), 0.9) == 1.0
277280
end
278281

279282
@testset "Fix12" begin

0 commit comments

Comments
 (0)