Skip to content

Commit

Permalink
Add exact results for trig fcns on π (JuliaLang#42595)
Browse files Browse the repository at this point in the history
Since Julia has gone to the trouble to encode π exactly,
and trig functions are defined in terms of π, they should give exact
answers for this particular constant.
  • Loading branch information
timholy committed Oct 23, 2021
1 parent 030a0f9 commit 31f67db
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions base/mathconstants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,10 @@ Base.literal_pow(::typeof(^), ::Irrational{:ℯ}, ::Val{p}) where {p} = exp(p)
Base.log(::Irrational{:ℯ}) = 1 # use 1 to correctly promote expressions like log(x)/log(ℯ)
Base.log(::Irrational{:ℯ}, x::Number) = log(x)

Base.sin(::Irrational{:π}) = 0.0
Base.cos(::Irrational{:π}) = -1.0
Base.sincos(::Irrational{:π}) = (0.0, -1.0)
Base.tan(::Irrational{:π}) = 0.0
Base.cot(::Irrational{:π}) = -1/0

end # module
2 changes: 1 addition & 1 deletion base/special/exp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ See also [`exp2`](@ref), [`exp10`](@ref) and [`cis`](@ref).
julia> exp(1.0)
2.718281828459045
julia> exp(im * pi) == cis(pi)
julia> exp(im * pi) cis(pi)
true
```
""" exp(x::Real)
Expand Down
5 changes: 5 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ end
@test occursin("3.14159", sprint(show, MIME"text/plain"(), π))
@test repr(Any[pi ℯ; ℯ pi]) == "Any[π ℯ; ℯ π]"
@test string(pi) == "π"

@test sin(π) === sinpi(1) == tan(π) == sinpi(1 // 1) == 0
@test cos(π) === cospi(1) == sec(π) == cospi(1 // 1) == -1
@test csc(π) == 1/0 && cot(π) == -1/0
@test sincos(π) === sincospi(1) == (0, -1)
end

@testset "frexp,ldexp,significand,exponent" begin
Expand Down

0 comments on commit 31f67db

Please sign in to comment.