Skip to content

Commit 12da953

Browse files
authored
Add trigonometric functions for special irrationals (#21)
* Add trigonometric functions for special irrationals * Fix tests * Try to fix tests on Julia nightly * Bump version * Improve definition for quartpi * Update tests
1 parent 6ecf42b commit 12da953

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "IrrationalConstants"
22
uuid = "92d709cd-6900-40b7-9082-c6be49f344b6"
33
authors = ["JuliaMath"]
4-
version = "0.2.1"
4+
version = "0.2.2"
55

66
[compat]
77
julia = "1"

src/IrrationalConstants.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ export
2828

2929
include("macro.jl")
3030
include("stats.jl")
31+
include("trigonometric.jl")
3132

3233
end # module

src/trigonometric.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Functions return `Float64`, consistent with Base
2+
# https://github.com/JuliaLang/julia/pull/42595
3+
# Values at poles are defined to be consistent with `cot(0)` and `cot(π)`
4+
# https://github.com/JuliaLang/julia/issues/7123
5+
# https://github.com/JuliaLang/julia/blob/e3d366f1966595ba737220df49e220610823b331/base/mathconstants.jl#L130
6+
7+
# `sin`
8+
Base.sin(::Twoπ) = 0.0
9+
Base.sin(::Fourπ) = 0.0
10+
Base.sin(::Halfπ) = 1.0
11+
Base.sin(::Quartπ) = Float64(invsqrt2)
12+
13+
# `cos`
14+
Base.cos(::Twoπ) = 1.0
15+
Base.cos(::Fourπ) = 1.0
16+
Base.cos(::Halfπ) = 0.0
17+
Base.cos(::Quartπ) = Float64(invsqrt2)
18+
19+
# `sincos`
20+
Base.sincos(::Twoπ) = (0.0, 1.0)
21+
Base.sincos(::Fourπ) = (0.0, 1.0)
22+
Base.sincos(::Halfπ) = (1.0, 0.0)
23+
Base.sincos(::Quartπ) = (Float64(invsqrt2), Float64(invsqrt2))
24+
25+
# `tan`
26+
Base.tan(::Twoπ) = 0.0
27+
Base.tan(::Fourπ) = 0.0
28+
Base.tan(::Halfπ) = 1/0
29+
Base.tan(::Quartπ) = 1.0
30+
31+
# `csc`, `sec`, and `cot` are defined automatically, so we do not define them
32+
# there is one exception where we can improve accuracy:
33+
Base.csc(::Quartπ) = Float64(sqrt2)
34+
Base.sec(::Quartπ) = Float64(sqrt2)

test/runtests.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,34 @@ end
150150
@test @inferred(round(loghalf, mode)) == 0.0
151151
end
152152
end
153+
154+
@testset "trigonometric functions" begin
155+
# 2π, 4π
156+
for (n, x) in ((2, twoπ), (4, fourπ))
157+
@test sin(x) === sinpi(n) === sin(0.0)
158+
@test cos(x) === cospi(n) === cos(0.0)
159+
end
160+
161+
# halfπ, quartπ
162+
for (r, x) in ((big"0.5", halfπ), (big"0.25", quartπ))
163+
@test sin(x) === Float64(sinpi(r))
164+
@test cos(x) === Float64(cospi(r))
165+
end
166+
167+
# Check consistency of definitions
168+
for x in (twoπ, fourπ, halfπ, quartπ)
169+
@test sincos(x) === (sin(x), cos(x))
170+
@test tan(x) === sin(x) / cos(x)
171+
end
172+
173+
# Check `csc`, `sec`, and `cot`
174+
for x in (twoπ, fourπ, halfπ)
175+
# These are defined automatically via sin, cos, and tan
176+
@test csc(x) === 1 / sin(x)
177+
@test sec(x) === 1 / cos(x)
178+
@test cot(x) === csc(x) / sec(x)
179+
end
180+
@test csc(quartπ) === Float64(csc(big(quartπ)))
181+
@test sec(quartπ) === Float64(sec(big(quartπ)))
182+
@test cot(quartπ) === Float64(cot(big(quartπ)))
183+
end

0 commit comments

Comments
 (0)