Skip to content

Commit 1aa65c3

Browse files
when x is NaN in trig functions return x rather than NaN (#49285)
* when x is NaN in trig functions return x rather than NaN * prefer `isnan(x) | isnan(y)` --------- Co-authored-by: mikmoore <95002244+mikmoore@users.noreply.github.com>
1 parent 8fbef6e commit 1aa65c3

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

base/special/hyperbolic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function asinh(x::T) where T <: Union{Float32, Float64}
175175
# return sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1)))
176176
# d) |x| >= 2^28
177177
# return sign(x)*(log(x)+ln2))
178-
if isnan(x) || isinf(x)
178+
if !isfinite(x)
179179
return x
180180
end
181181
absx = abs(x)

base/special/trig.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function sin(x::T) where T<:Union{Float32, Float64}
3434
end
3535
return sin_kernel(x)
3636
elseif isnan(x)
37-
return T(NaN)
37+
return x
3838
elseif isinf(x)
3939
sin_domain_error(x)
4040
end
@@ -103,7 +103,7 @@ function cos(x::T) where T<:Union{Float32, Float64}
103103
end
104104
return cos_kernel(x)
105105
elseif isnan(x)
106-
return T(NaN)
106+
return x
107107
elseif isinf(x)
108108
cos_domain_error(x)
109109
else
@@ -179,7 +179,7 @@ function sincos(x::T) where T<:Union{Float32, Float64}
179179
end
180180
return sincos_kernel(x)
181181
elseif isnan(x)
182-
return T(NaN), T(NaN)
182+
return x, x
183183
elseif isinf(x)
184184
sincos_domain_error(x)
185185
end
@@ -221,7 +221,7 @@ function tan(x::T) where T<:Union{Float32, Float64}
221221
end
222222
return tan_kernel(x)
223223
elseif isnan(x)
224-
return T(NaN)
224+
return x
225225
elseif isinf(x)
226226
tan_domain_error(x)
227227
end
@@ -582,8 +582,8 @@ function atan(y::T, x::T) where T<:Union{Float32, Float64}
582582
# S8) ATAN2(+-INF,+INF ) is +-pi/4 ;
583583
# S9) ATAN2(+-INF,-INF ) is +-3pi/4;
584584
# S10) ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
585-
if isnan(x) || isnan(y) # S1 or S2
586-
return T(NaN)
585+
if isnan(x) | isnan(y) # S1 or S2
586+
return isnan(x) ? x : y
587587
end
588588

589589
if x == T(1.0) # then y/x = y and x > 0, see M2
@@ -1191,7 +1191,7 @@ function sind(x::Real)
11911191
if isinf(x)
11921192
return throw(DomainError(x, "`x` cannot be infinite."))
11931193
elseif isnan(x)
1194-
return oftype(x,NaN)
1194+
return x
11951195
end
11961196

11971197
rx = copysign(float(rem(x,360)),x)
@@ -1222,7 +1222,7 @@ function cosd(x::Real)
12221222
if isinf(x)
12231223
return throw(DomainError(x, "`x` cannot be infinite."))
12241224
elseif isnan(x)
1225-
return oftype(x,NaN)
1225+
return x
12261226
end
12271227

12281228
rx = abs(float(rem(x,360)))

0 commit comments

Comments
 (0)