Skip to content

Commit 8c2b377

Browse files
JeffreySarnoffKristofferC
authored andcommitted
bugfix: fld1 order of ops (#46938)
* bugfix: fld1 order of ops fixes #28973 (cherry picked from commit fcdc5bc)
1 parent 7b2d05c commit 8c2b377

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
@@ -111,6 +111,7 @@ New library features
111111
* `extrema` now accepts an `init` keyword argument ([#36265], [#43604]).
112112
* `Iterators.countfrom` now accepts any type that defines `+` ([#37747]).
113113
* `@time` now separates out % time spent recompiling invalidated methods ([#45015]).
114+
* An issue with order of operations in `fld1` is now fixed ([#28973]).
114115

115116
Standard library changes
116117
------------------------

base/operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ julia> x == (fld1(x, y) - 1) * y + mod1(x, y)
855855
true
856856
```
857857
"""
858-
fld1(x::T, y::T) where {T<:Real} = (m = mod1(x, y); fld(x + y - m, y))
858+
fld1(x::T, y::T) where {T<:Real} = (m = mod1(x, y); fld((x - m) + y, y))
859859
function fld1(x::T, y::T) where T<:Integer
860860
d = div(x, y)
861861
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
@@ -261,6 +261,9 @@ end
261261
end
262262

263263
@test fldmod1(4.0, 3) == fldmod1(4, 3)
264+
265+
# issue 28973
266+
@test fld1(0.4, 0.9) == fld1(nextfloat(0.4), 0.9) == 1.0
264267
end
265268

266269
@testset "Fix12" begin

0 commit comments

Comments
 (0)