Skip to content

Commit e296fe7

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 fbf0050 commit e296fe7

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
@@ -147,6 +147,7 @@ Library changes
147147
* `RegexMatch` objects can now be probed for whether a named capture group exists within it through `haskey()` ([#36717]).
148148
* For consistency `haskey(r::RegexMatch, i::Integer)` has also been added and returns if the capture group
149149
for `i` exists ([#37300]).
150+
* An issue with order of operations in `fld1` is now fixed ([#28973]).
150151

151152
Standard library changes
152153
------------------------

base/operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ julia> x == (fld1(x, y) - 1) * y + mod1(x, y)
802802
true
803803
```
804804
"""
805-
fld1(x::T, y::T) where {T<:Real} = (m = mod1(x, y); fld(x + y - m, y))
805+
fld1(x::T, y::T) where {T<:Real} = (m = mod1(x, y); fld((x - m) + y, y))
806806
function fld1(x::T, y::T) where T<:Integer
807807
d = div(x, y)
808808
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
@@ -217,6 +217,9 @@ end
217217
end
218218

219219
@test fldmod1(4.0, 3) == fldmod1(4, 3)
220+
221+
# issue 28973
222+
@test fld1(0.4, 0.9) == fld1(nextfloat(0.4), 0.9) == 1.0
220223
end
221224

222225
@testset "Fix12" begin

0 commit comments

Comments
 (0)