Skip to content

Commit

Permalink
Fix sincos for reals that are not AbstractFloats (#25292)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpsanders authored and StefanKarpinski committed Jan 24, 2018
1 parent 3b9c7d7 commit 27407e0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
8 changes: 7 additions & 1 deletion base/special/trig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,13 @@ function sincos(x::T) where T<:Union{Float32, Float64}
return -co, si
end
end
sincos(x::Real) = sincos(float(x))

_sincos(x::AbstractFloat) = sincos(x)
_sincos(x) = (sin(x), cos(x))

sincos(x) = _sincos(float(x))



# There's no need to write specialized kernels, as inlining takes care of remo-
# ving superfluous calculations.
Expand Down
38 changes: 38 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -862,3 +862,41 @@ end
@test isnan_type(T, acos(T(NaN)))
end
end

# Define simple wrapper of a Float type:
struct FloatWrapper <: Real
x::Float64
end

import Base: +, -, *, /, ^, sin, cos, exp, sinh, cosh, convert, isfinite, float, promote_rule

for op in (:+, :-, :*, :/, :^)
@eval $op(x::FloatWrapper, y::FloatWrapper) = FloatWrapper($op(x.x, y.x))
end

for op in (:sin, :cos, :exp, :sinh, :cosh, :-)
@eval $op(x::FloatWrapper) = FloatWrapper($op(x.x))
end

for op in (:isfinite,)
@eval $op(x::FloatWrapper) = $op(x.x)
end

convert(::Type{FloatWrapper}, x::Int) = FloatWrapper(float(x))
promote_rule(::Type{FloatWrapper}, ::Type{Int}) = FloatWrapper

float(x::FloatWrapper) = x

@testset "exp(Complex(a, b)) for a and b of non-standard real type #25292" begin

x = FloatWrapper(3.1)
y = FloatWrapper(4.1)

@test sincos(x) == (sin(x), cos(x))

z = Complex(x, y)

@test isa(exp(z), Complex)
@test isa(sin(z), Complex)
@test isa(cos(z), Complex)
end
4 changes: 2 additions & 2 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,9 @@ let repr = sprint(show, "text/html", methods(f16580))
end

if isempty(Base.GIT_VERSION_INFO.commit)
@test contains(Base.url(first(methods(sin))),"https://github.com/JuliaLang/julia/tree/v$VERSION/base/missing.jl#L")
@test contains(Base.url(which(sin, (Float64,))), "https://github.com/JuliaLang/julia/tree/v$VERSION/base/special/trig.jl#L")
else
@test contains(Base.url(first(methods(sin))),"https://github.com/JuliaLang/julia/tree/$(Base.GIT_VERSION_INFO.commit)/base/missing.jl#L")
@test contains(Base.url(which(sin, (Float64,))), "https://github.com/JuliaLang/julia/tree/$(Base.GIT_VERSION_INFO.commit)/base/special/trig.jl#L")
end

# print_matrix should be able to handle small and large objects easily, test by
Expand Down

0 comments on commit 27407e0

Please sign in to comment.