Skip to content

Commit

Permalink
Add sincostau (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Feb 23, 2023
1 parent d2fd316 commit 24787ad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Tau"
uuid = "c544e3c2-d3e5-5802-ac44-44683f340e4a"
version = "1.0.0"
version = "1.1.0"

[deps]
IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
Expand Down
18 changes: 16 additions & 2 deletions src/Tau.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module Tau
import IrrationalConstants

export tau, τ,
sintau, costau, modtau,
sinτ, cosτ, modτ
sintau, costau, sincostau, modtau,
sinτ, cosτ, sincosτ, modτ

# Definition of τ as irrational constant
const τ = IrrationalConstants.twoπ
Expand All @@ -16,12 +16,26 @@ const modtau = modτ
# Trigonometric functions
sinτ(x) = sinpi(2 * x)
cosτ(x) = cospi(2 * x)
function sincosτ(x)
y = 2 * x
# sincospi was added in https://github.com/JuliaLang/julia/pull/35816
@static if VERSION < v"1.6.0-DEV.292"
return (sinpi(y), cospi(y))
else
return sincospi(y)
end
end

# Optimization for integers
sinτ(x::Integer) = zero(float(x))
cosτ(x::Integer) = one(float(x))
function sincosτ(x::Integer)
y = float(x)
return (zero(y), one(y))
end

const sintau = sinτ
const costau = cosτ
const sincostau = sincosτ

end
29 changes: 25 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ end
end
end

@testset "sintau/costau" begin
@testset "sintau/costau/sincostau" begin
@testset "approximate results for fractional inputs" begin
@testset "real values" begin
for T = (Float32, Float64), x = -3:0.1:3.0
@test @inferred(sintau(T(x))) T(sinpi(2 * parse(BigFloat, string(x))))
@test @inferred(costau(T(x))) T(cospi(2 * parse(BigFloat, string(x))))
@test @inferred(sincostau(T(x))) == (sintau(T(x)), costau(T(x)))
end
end

Expand All @@ -43,6 +44,7 @@ end
z = complex(x, y)
@test @inferred(sintau(z)) sinpi(2 * z)
@test @inferred(costau(z)) cospi(2 * z)
@test @inferred(sincostau(z)) == (sintau(z), costau(z))
end
end
end
Expand All @@ -52,13 +54,15 @@ end
for T = (Int, Complex), x = -3:3
@test @inferred(sintau(T(x))) == zero(T)
@test @inferred(costau(T(x))) == one(T)
@test @inferred(sincostau(T(x))) == (sintau(T(x)), costau(T(x)))
end
end

@testset "real and complex values passed as floating point types" begin
for T = (Float32, Float64, BigFloat, Complex), x = -3.0:3.0
@test @inferred(sintau(T(x))) == sign(x) * zero(T)
@test @inferred(costau(T(x))) == one(T)
@test @inferred(sincostau(T(x))) == (sintau(T(x)), costau(T(x)))
end
end
end
Expand All @@ -68,27 +72,40 @@ end
for x in (Inf, -Inf)
@test_throws DomainError sintau(x)
@test_throws DomainError costau(x)
@test_throws DomainError sincostau(x)
end
@test isequal(@inferred(sintau(NaN)), sinpi(NaN))
@test isequal(@inferred(costau(NaN)), cospi(NaN))
@test isequal(@inferred(sincostau(NaN)), (sintau(NaN), costau(NaN)))
end

@testset "complex values" begin
for x in (Inf, NaN, 0), y in (Inf, NaN, 0)
z = complex(x, y)
@test isequal(@inferred(sintau(z)), sinpi(2 * z))
@test isequal(@inferred(costau(z)), cospi(2 * z))
@test isequal(@inferred(sincostau(z)), (sintau(z), costau(z)))
end
end
end

# Adapted from julia/test/math.jl
@testset "type stability" begin
for T = (Int, Float32, Float64, BigFloat), f = (sintau, costau)
@test Base.return_types(f, Tuple{T}) == [float(T)]
@test Base.return_types(f, Tuple{Complex{T}}) == [Complex{float(T)}]
for T = (Int, Float32, Float64, BigFloat)
for f = (sintau, costau)
@test Base.return_types(f, Tuple{T}) == [float(T)]
@test Base.return_types(f, Tuple{Complex{T}}) == [Complex{float(T)}]
end
@test Base.return_types(sincostau, Tuple{T}) == [Tuple{float(T), float(T)}]
@test Base.return_types(sincostau, Tuple{Complex{T}}) == [Tuple{Complex{float(T)}, Complex{float(T)}}]
end
end

@testset "aliases" begin
@test sinτ === sintau
@test cosτ === costau
@test sincosτ === sincostau
end
end

# Adapted from julia/test/mod2pi.jl
Expand All @@ -101,4 +118,8 @@ end
@test modtau(355.0) 3.1416227979431572
@test modtau(355.0f0) 3.1416228f0
@test modtau(Int64(2)^60) == modtau(2.0^60)

# aliases
@test modtau === mod2pi
@test modτ === modtau
end

2 comments on commit 24787ad

@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/78328

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 v1.1.0 -m "<description of version>" 24787ad0cbc9eec8041440c107fd9b02f71f915f
git push origin v1.1.0

Please sign in to comment.