Skip to content

Commit 665b03e

Browse files
authored
Fix rem2pi for NaN inputs, fixes #32888. (#36420)
1 parent e3de4a8 commit 665b03e

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
@@ -1239,6 +1239,8 @@ julia> rem2pi(7pi/4, RoundDown)
12391239
"""
12401240
function rem2pi end
12411241
function rem2pi(x::Float64, ::RoundingMode{:Nearest})
1242+
isnan(x) && return NaN
1243+
12421244
abs(x) < pi && return x
12431245

12441246
n,y = rem_pio2_kernel(x)
@@ -1262,6 +1264,8 @@ function rem2pi(x::Float64, ::RoundingMode{:Nearest})
12621264
end
12631265
end
12641266
function rem2pi(x::Float64, ::RoundingMode{:ToZero})
1267+
isnan(x) && return NaN
1268+
12651269
ax = abs(x)
12661270
ax <= 2*Float64(pi,RoundDown) && return x
12671271

@@ -1287,6 +1291,8 @@ function rem2pi(x::Float64, ::RoundingMode{:ToZero})
12871291
copysign(z,x)
12881292
end
12891293
function rem2pi(x::Float64, ::RoundingMode{:Down})
1294+
isnan(x) && return NaN
1295+
12901296
if x < pi4o2_h
12911297
if x >= 0
12921298
return x
@@ -1316,6 +1322,8 @@ function rem2pi(x::Float64, ::RoundingMode{:Down})
13161322
end
13171323
end
13181324
function rem2pi(x::Float64, ::RoundingMode{:Up})
1325+
isnan(x) && return NaN
1326+
13191327
if x > -pi4o2_h
13201328
if x <= 0
13211329
return x

test/numbers.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,6 +2675,13 @@ end
26752675
@test rem2pi(T(-8), RoundUp) -8+2pi
26762676
end
26772677

2678+
@testset "PR #36420 $T" for T in (Float16, Float32, Float64)
2679+
@test rem2pi(T(NaN), RoundToZero) === T(NaN)
2680+
@test rem2pi(T(NaN), RoundNearest) === T(NaN)
2681+
@test rem2pi(T(NaN), RoundDown) === T(NaN)
2682+
@test rem2pi(T(NaN), RoundUp) === T(NaN)
2683+
end
2684+
26782685
import Base.^
26792686
struct PR20530; end
26802687
struct PR20889; x; end

0 commit comments

Comments
 (0)