Skip to content

Commit 687ed92

Browse files
committed
a bit of code deduplication
1 parent cc81a0b commit 687ed92

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

base/special/trig.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,9 @@ Return a `T(NaN)` if `isnan(x)`.
11041104
See also [`sinc`](@ref).
11051105
"""
11061106
cosc(x::Number) = _cosc(float(x))
1107+
function _cosc_generic(x)
1108+
((pi*x)*cospi(x)-sinpi(x))/((pi*x)*x)
1109+
end
11071110
function _cosc(x::Number)
11081111
# naive cosc formula is susceptible to catastrophic
11091112
# cancellation error near x=0, so we use the Taylor series
@@ -1123,17 +1126,17 @@ function _cosc(x::Number)
11231126
end
11241127
return π*s
11251128
else
1126-
return isinf_real(x) ? zero(x) : ((pi*x)*cospi(x)-sinpi(x))/((pi*x)*x)
1129+
return isinf_real(x) ? zero(x) : _cosc_generic(x)
11271130
end
11281131
end
11291132
# hard-code Float64/Float32 Taylor series, with coefficients
11301133
# Float64.([(-1)^n*big(pi)^(2n)/((2n+1)*factorial(2n-1)) for n = 1:6])
11311134
_cosc(x::Union{Float64,ComplexF64}) =
11321135
fastabs(x) < 0.14 ? x*evalpoly(x^2, (-3.289868133696453, 3.2469697011334144, -1.1445109447325053, 0.2091827825412384, -0.023460810354558236, 0.001781145516372852)) :
1133-
isinf_real(x) ? zero(x) : ((pi*x)*cospi(x)-sinpi(x))/((pi*x)*x)
1136+
isinf_real(x) ? zero(x) : _cosc_generic(x)
11341137
_cosc(x::Union{Float32,ComplexF32}) =
11351138
fastabs(x) < 0.26f0 ? x*evalpoly(x^2, (-3.289868f0, 3.2469697f0, -1.144511f0, 0.20918278f0)) :
1136-
isinf_real(x) ? zero(x) : ((pi*x)*cospi(x)-sinpi(x))/((pi*x)*x)
1139+
isinf_real(x) ? zero(x) : _cosc_generic(x)
11371140
_cosc(x::Float16) = Float16(_cosc(Float32(x)))
11381141
_cosc(x::ComplexF16) = ComplexF16(_cosc(ComplexF32(x)))
11391142

0 commit comments

Comments
 (0)