Skip to content

Commit a2d52e1

Browse files
authored
Change the implementation of rotate! to avoid unary minus (#1323)
The current implementation of `rotate!(x, y, c, s)` overwrites `x` with `c*x + s*y` and `y` with `-conj(s)*x + c*y`. There exists this edge case of `rotate!([NaN], [NaN], false, false)` ```julia julia> rotate!([NaN], [NaN], false, false) ([0.0], [NaN]) ``` The new `x` becomes `0.0`, because `false` acts as a ["strong zero" in Julia](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Arithmetic-Operators) (so `false * NaN == 0.0`). When calculating the new `y`, the code executes `-conj(false)*NaN + false*NaN`. However, the unary minus, `-`, turns the `false::Bool` to `0::Int`, which is no longer a strong zero, and thus, we get `NaN`. This is inconsistent with ```julia julia> reflect!([NaN], [NaN], false, false) ([0.0], [0.0]) ``` This PR changes `-conj(s)*x + c*y` to `c*y - conj(s)*x` to avoid unary minus, so that ```julia julia> rotate!([NaN], [NaN], false, false) # This PR ([0.0], [0.0]) ``` I noted this from JuliaGPU/CUDA.jl#2607 (comment). More discussion in https://discourse.julialang.org/t/negative-false-no-longer-acts-as-a-strong-zero/128506/1
1 parent c9b6456 commit a2d52e1

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/generic.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ end
17261726
"""
17271727
rotate!(x, y, c, s)
17281728
1729-
Overwrite `x` with `c*x + s*y` and `y` with `-conj(s)*x + c*y`.
1729+
Overwrite `x` with `s*y + c*x` and `y` with `c*y - conj(s)*x`.
17301730
Returns `x` and `y`.
17311731
17321732
!!! compat "Julia 1.5"
@@ -1741,8 +1741,8 @@ function rotate!(x::AbstractVector, y::AbstractVector, c, s)
17411741
for i in eachindex(x,y)
17421742
@inbounds begin
17431743
xi, yi = x[i], y[i]
1744-
x[i] = c *xi + s*yi
1745-
y[i] = -conj(s)*xi + c*yi
1744+
x[i] = s*yi + c *xi
1745+
y[i] = c*yi - conj(s)*xi
17461746
end
17471747
end
17481748
return x, y

0 commit comments

Comments
 (0)