Skip to content

Commit

Permalink
Add trigonometric functions for special irrationals (#21)
Browse files Browse the repository at this point in the history
* Add trigonometric functions for special irrationals

* Fix tests

* Try to fix tests on Julia nightly

* Bump version

* Improve definition for quartpi

* Update tests
  • Loading branch information
devmotion authored Feb 26, 2023
1 parent 6ecf42b commit 12da953
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "IrrationalConstants"
uuid = "92d709cd-6900-40b7-9082-c6be49f344b6"
authors = ["JuliaMath"]
version = "0.2.1"
version = "0.2.2"

[compat]
julia = "1"
Expand Down
1 change: 1 addition & 0 deletions src/IrrationalConstants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ export

include("macro.jl")
include("stats.jl")
include("trigonometric.jl")

end # module
34 changes: 34 additions & 0 deletions src/trigonometric.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Functions return `Float64`, consistent with Base
# https://github.com/JuliaLang/julia/pull/42595
# Values at poles are defined to be consistent with `cot(0)` and `cot(π)`
# https://github.com/JuliaLang/julia/issues/7123
# https://github.com/JuliaLang/julia/blob/e3d366f1966595ba737220df49e220610823b331/base/mathconstants.jl#L130

# `sin`
Base.sin(::Twoπ) = 0.0
Base.sin(::Fourπ) = 0.0
Base.sin(::Halfπ) = 1.0
Base.sin(::Quartπ) = Float64(invsqrt2)

# `cos`
Base.cos(::Twoπ) = 1.0
Base.cos(::Fourπ) = 1.0
Base.cos(::Halfπ) = 0.0
Base.cos(::Quartπ) = Float64(invsqrt2)

# `sincos`
Base.sincos(::Twoπ) = (0.0, 1.0)
Base.sincos(::Fourπ) = (0.0, 1.0)
Base.sincos(::Halfπ) = (1.0, 0.0)
Base.sincos(::Quartπ) = (Float64(invsqrt2), Float64(invsqrt2))

# `tan`
Base.tan(::Twoπ) = 0.0
Base.tan(::Fourπ) = 0.0
Base.tan(::Halfπ) = 1/0
Base.tan(::Quartπ) = 1.0

# `csc`, `sec`, and `cot` are defined automatically, so we do not define them
# there is one exception where we can improve accuracy:
Base.csc(::Quartπ) = Float64(sqrt2)
Base.sec(::Quartπ) = Float64(sqrt2)
31 changes: 31 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,34 @@ end
@test @inferred(round(loghalf, mode)) == 0.0
end
end

@testset "trigonometric functions" begin
# 2π, 4π
for (n, x) in ((2, twoπ), (4, fourπ))
@test sin(x) === sinpi(n) === sin(0.0)
@test cos(x) === cospi(n) === cos(0.0)
end

# halfπ, quartπ
for (r, x) in ((big"0.5", halfπ), (big"0.25", quartπ))
@test sin(x) === Float64(sinpi(r))
@test cos(x) === Float64(cospi(r))
end

# Check consistency of definitions
for x in (twoπ, fourπ, halfπ, quartπ)
@test sincos(x) === (sin(x), cos(x))
@test tan(x) === sin(x) / cos(x)
end

# Check `csc`, `sec`, and `cot`
for x in (twoπ, fourπ, halfπ)
# These are defined automatically via sin, cos, and tan
@test csc(x) === 1 / sin(x)
@test sec(x) === 1 / cos(x)
@test cot(x) === csc(x) / sec(x)
end
@test csc(quartπ) === Float64(csc(big(quartπ)))
@test sec(quartπ) === Float64(sec(big(quartπ)))
@test cot(quartπ) === Float64(cot(big(quartπ)))
end

2 comments on commit 12da953

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/78577

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.2 -m "<description of version>" 12da9534697cb5665c0ac305899b5c733a4b4bcb
git push origin v0.2.2

Please sign in to comment.