Skip to content

Commit b763728

Browse files
authored
Remove weird Rational dispatch and add pi functions to list (#50850)
Should fix #48735
1 parent fd695a7 commit b763728

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

base/math.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ sincos(a::Float16) = Float16.(sincos(Float32(a)))
15711571
for f in (:sin, :cos, :tan, :asin, :atan, :acos,
15721572
:sinh, :cosh, :tanh, :asinh, :acosh, :atanh,
15731573
:exp, :exp2, :exp10, :expm1, :log, :log2, :log10, :log1p,
1574-
:exponent, :sqrt, :cbrt)
1574+
:exponent, :sqrt, :cbrt, :sinpi, :cospi, :sincospi, :tanpi)
15751575
@eval function ($f)(x::Real)
15761576
xf = float(x)
15771577
x === xf && throw(MethodError($f, (x,)))

base/special/trig.jl

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -789,16 +789,14 @@ Compute ``\\sin(\\pi x)`` more accurately than `sin(pi*x)`, especially for large
789789
790790
See also [`sind`](@ref), [`cospi`](@ref), [`sincospi`](@ref).
791791
"""
792-
function sinpi(_x::T) where T<:Union{IEEEFloat, Rational}
792+
function sinpi(_x::T) where T<:IEEEFloat
793793
x = abs(_x)
794794
if !isfinite(x)
795795
isnan(x) && return x
796796
throw(DomainError(x, "`x` cannot be infinite."))
797797
end
798798
# For large x, answers are all 1 or zero.
799-
if T <: AbstractFloat
800-
x >= maxintfloat(T) && return copysign(zero(T), _x)
801-
end
799+
x >= maxintfloat(T) && return copysign(zero(T), _x)
802800

803801
# reduce to interval [0, 0.5]
804802
n = round(2*x)
@@ -820,16 +818,14 @@ end
820818
821819
Compute ``\\cos(\\pi x)`` more accurately than `cos(pi*x)`, especially for large `x`.
822820
"""
823-
function cospi(x::T) where T<:Union{IEEEFloat, Rational}
821+
function cospi(x::T) where T<:IEEEFloat
824822
x = abs(x)
825823
if !isfinite(x)
826824
isnan(x) && return x
827825
throw(DomainError(x, "`x` cannot be infinite."))
828826
end
829827
# For large x, answers are all 1 or zero.
830-
if T <: AbstractFloat
831-
x >= maxintfloat(T) && return one(T)
832-
end
828+
x >= maxintfloat(T) && return one(T)
833829

834830
# reduce to interval [0, 0.5]
835831
n = round(2*x)
@@ -856,16 +852,14 @@ where `x` is in radians), returning a tuple `(sine, cosine)`.
856852
857853
See also: [`cispi`](@ref), [`sincosd`](@ref), [`sinpi`](@ref).
858854
"""
859-
function sincospi(_x::T) where T<:Union{IEEEFloat, Rational}
855+
function sincospi(_x::T) where T<:IEEEFloat
860856
x = abs(_x)
861857
if !isfinite(x)
862858
isnan(x) && return x, x
863859
throw(DomainError(x, "`x` cannot be infinite."))
864860
end
865861
# For large x, answers are all 1 or zero.
866-
if T <: AbstractFloat
867-
x >= maxintfloat(T) && return (copysign(zero(T), _x), one(T))
868-
end
862+
x >= maxintfloat(T) && return (copysign(zero(T), _x), one(T))
869863

870864
# reduce to interval [0, 0.5]
871865
n = round(2*x)
@@ -895,7 +889,7 @@ Compute ``\\tan(\\pi x)`` more accurately than `tan(pi*x)`, especially for large
895889
896890
See also [`tand`](@ref), [`sinpi`](@ref), [`cospi`](@ref), [`sincospi`](@ref).
897891
"""
898-
function tanpi(_x::T) where T<:Union{IEEEFloat, Rational}
892+
function tanpi(_x::T) where T<:IEEEFloat
899893
# This is modified from sincospi.
900894
# Would it be faster or more accurate to make a tanpi_kernel?
901895
x = abs(_x)
@@ -905,9 +899,7 @@ function tanpi(_x::T) where T<:Union{IEEEFloat, Rational}
905899
end
906900
# For large x, answers are all zero.
907901
# All integer values for floats larger than maxintfloat are even.
908-
if T <: AbstractFloat
909-
x >= maxintfloat(T) && return copysign(zero(T), _x)
910-
end
902+
x >= maxintfloat(T) && return copysign(zero(T), _x)
911903

912904
# reduce to interval [0, 0.5]
913905
n = round(2*x)
@@ -932,10 +924,10 @@ cospi(x::Integer) = isodd(x) ? -one(float(x)) : one(float(x))
932924
tanpi(x::Integer) = x >= 0 ? (isodd(x) ? -zero(float(x)) : zero(float(x))) :
933925
(isodd(x) ? zero(float(x)) : -zero(float(x)))
934926
sincospi(x::Integer) = (sinpi(x), cospi(x))
935-
sinpi(x::Real) = sin(pi*x)
936-
cospi(x::Real) = cos(pi*x)
937-
sincospi(x::Real) = sincos(pi*x)
938-
tanpi(x::Real) = tan(pi*x)
927+
sinpi(x::AbstractFloat) = sin(pi*x)
928+
cospi(x::AbstractFloat) = cos(pi*x)
929+
sincospi(x::AbstractFloat) = sincos(pi*x)
930+
tanpi(x::AbstractFloat) = tan(pi*x)
939931
tanpi(x::Complex) = sinpi(x) / cospi(x) # Is there a better way to do this?
940932

941933
function sinpi(z::Complex{T}) where T

test/math.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,3 +1558,9 @@ for T in (Float16, Float32, Float64)
15581558
@test Core.Compiler.is_foldable(Base.infer_effects(^, (T,Int)))
15591559
@test Core.Compiler.is_foldable(Base.infer_effects(^, (T,T)))
15601560
end
1561+
1562+
@testset "BigInt Rationals with special funcs" begin
1563+
@test sinpi(big(1//1)) == big(0.0)
1564+
@test tanpi(big(1//1)) == big(0.0)
1565+
@test cospi(big(1//1)) == big(-1.0)
1566+
end

0 commit comments

Comments
 (0)