Skip to content

Commit

Permalink
Fix rem2pi for NaN inputs, fixes JuliaLang#32888. (JuliaLang#36420)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautam98 authored and Francesco Fucci committed Aug 11, 2022
1 parent 378ef9c commit 0236910
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,8 @@ julia> rem2pi(7pi/4, RoundDown)
"""
function rem2pi end
function rem2pi(x::Float64, ::RoundingMode{:Nearest})
isnan(x) && return NaN

abs(x) < pi && return x

n,y = rem_pio2_kernel(x)
Expand All @@ -1262,6 +1264,8 @@ function rem2pi(x::Float64, ::RoundingMode{:Nearest})
end
end
function rem2pi(x::Float64, ::RoundingMode{:ToZero})
isnan(x) && return NaN

ax = abs(x)
ax <= 2*Float64(pi,RoundDown) && return x

Expand All @@ -1287,6 +1291,8 @@ function rem2pi(x::Float64, ::RoundingMode{:ToZero})
copysign(z,x)
end
function rem2pi(x::Float64, ::RoundingMode{:Down})
isnan(x) && return NaN

if x < pi4o2_h
if x >= 0
return x
Expand Down Expand Up @@ -1316,6 +1322,8 @@ function rem2pi(x::Float64, ::RoundingMode{:Down})
end
end
function rem2pi(x::Float64, ::RoundingMode{:Up})
isnan(x) && return NaN

if x > -pi4o2_h
if x <= 0
return x
Expand Down
7 changes: 7 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2675,6 +2675,13 @@ end
@test rem2pi(T(-8), RoundUp) -8+2pi
end

@testset "PR #36420 $T" for T in (Float16, Float32, Float64)
@test rem2pi(T(NaN), RoundToZero) === T(NaN)
@test rem2pi(T(NaN), RoundNearest) === T(NaN)
@test rem2pi(T(NaN), RoundDown) === T(NaN)
@test rem2pi(T(NaN), RoundUp) === T(NaN)
end

import Base.^
struct PR20530; end
struct PR20889; x; end
Expand Down

0 comments on commit 0236910

Please sign in to comment.