Skip to content

Commit

Permalink
bugfix: fld1 order of ops (#46938)
Browse files Browse the repository at this point in the history
* bugfix: fld1 order of ops

fixes #28973

(cherry picked from commit fcdc5bc)
  • Loading branch information
JeffreySarnoff authored and KristofferC committed Oct 27, 2022
1 parent 7b2d05c commit 8c2b377
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------
Expand Down
2 changes: 1 addition & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c2b377

Please sign in to comment.