From 023691056e1f789baff4f3167205187158903514 Mon Sep 17 00:00:00 2001 From: Gautam Mishra Date: Mon, 25 Jul 2022 03:46:01 +0530 Subject: [PATCH] Fix rem2pi for NaN inputs, fixes #32888. (#36420) --- base/math.jl | 8 ++++++++ test/numbers.jl | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/base/math.jl b/base/math.jl index af193f996dbf5..27c9caa095d67 100644 --- a/base/math.jl +++ b/base/math.jl @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/test/numbers.jl b/test/numbers.jl index 46c9fe50678f0..5f7553ab6c66e 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -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