Skip to content

Commit 4fa7980

Browse files
mgautam98KristofferC
authored andcommitted
Fix rem2pi for NaN inputs, fixes #32888. (#36420)
(cherry picked from commit 665b03e)
1 parent 2415a14 commit 4fa7980

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

base/math.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,8 @@ julia> rem2pi(7pi/4, RoundDown)
11311131
"""
11321132
function rem2pi end
11331133
function rem2pi(x::Float64, ::RoundingMode{:Nearest})
1134+
isnan(x) && return NaN
1135+
11341136
abs(x) < pi && return x
11351137

11361138
n,y = rem_pio2_kernel(x)
@@ -1154,6 +1156,8 @@ function rem2pi(x::Float64, ::RoundingMode{:Nearest})
11541156
end
11551157
end
11561158
function rem2pi(x::Float64, ::RoundingMode{:ToZero})
1159+
isnan(x) && return NaN
1160+
11571161
ax = abs(x)
11581162
ax <= 2*Float64(pi,RoundDown) && return x
11591163

@@ -1179,6 +1183,8 @@ function rem2pi(x::Float64, ::RoundingMode{:ToZero})
11791183
copysign(z,x)
11801184
end
11811185
function rem2pi(x::Float64, ::RoundingMode{:Down})
1186+
isnan(x) && return NaN
1187+
11821188
if x < pi4o2_h
11831189
if x >= 0
11841190
return x
@@ -1208,6 +1214,8 @@ function rem2pi(x::Float64, ::RoundingMode{:Down})
12081214
end
12091215
end
12101216
function rem2pi(x::Float64, ::RoundingMode{:Up})
1217+
isnan(x) && return NaN
1218+
12111219
if x > -pi4o2_h
12121220
if x <= 0
12131221
return x

test/numbers.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,6 +2611,13 @@ end
26112611
@test rem2pi(T(-8), RoundUp) -8+2pi
26122612
end
26132613

2614+
@testset "PR #36420 $T" for T in (Float16, Float32, Float64)
2615+
@test rem2pi(T(NaN), RoundToZero) === T(NaN)
2616+
@test rem2pi(T(NaN), RoundNearest) === T(NaN)
2617+
@test rem2pi(T(NaN), RoundDown) === T(NaN)
2618+
@test rem2pi(T(NaN), RoundUp) === T(NaN)
2619+
end
2620+
26142621
import Base.^
26152622
struct PR20530; end
26162623
struct PR20889; x; end

0 commit comments

Comments
 (0)