From bb60877b0466007739cf6abf68f5b01ace9faeac Mon Sep 17 00:00:00 2001 From: Jeffrey Sarnoff Date: Mon, 3 Oct 2022 11:38:35 -0400 Subject: [PATCH] bugfix: fld1 order of ops (#46938) * bugfix: fld1 order of ops fixes https://github.com/JuliaLang/julia/issues/28973 (cherry picked from commit fcdc5bc21296520c04349c901e234477c5e2ffad) --- NEWS.md | 1 + base/operators.jl | 2 +- test/operators.jl | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 334517e2e9598..8e0493f8a2b12 100644 --- a/NEWS.md +++ b/NEWS.md @@ -147,6 +147,7 @@ Library changes * `RegexMatch` objects can now be probed for whether a named capture group exists within it through `haskey()` ([#36717]). * For consistency `haskey(r::RegexMatch, i::Integer)` has also been added and returns if the capture group for `i` exists ([#37300]). +* 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 8120e95913e3c..154b4963b7b14 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -802,7 +802,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 bdb0abcdaed1e..c584cfee7f2b5 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -217,6 +217,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