From 75303a0b332ca6425fc0f95cca00837bb849e956 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Wed, 23 Mar 2022 07:48:19 +0100 Subject: [PATCH 1/3] Use `IrrationalConstants` and `sinpi`/`cospi` --- .github/workflows/CompatHelper.yml | 31 +++++++++ .gitignore | 1 + Project.toml | 12 ++-- src/Tau.jl | 24 +++++-- src/trig.jl | 102 ----------------------------- test/runtests.jl | 22 ++----- 6 files changed, 63 insertions(+), 129 deletions(-) create mode 100644 .github/workflows/CompatHelper.yml create mode 100644 .gitignore delete mode 100644 src/trig.jl diff --git a/.github/workflows/CompatHelper.yml b/.github/workflows/CompatHelper.yml new file mode 100644 index 0000000..39e1d95 --- /dev/null +++ b/.github/workflows/CompatHelper.yml @@ -0,0 +1,31 @@ +name: CompatHelper +on: + schedule: + - cron: 0 0 * * * + workflow_dispatch: +jobs: + CompatHelper: + runs-on: ubuntu-latest + steps: + - name: "Add the General registry via Git" + run: | + import Pkg + ENV["JULIA_PKG_SERVER"] = "" + Pkg.Registry.add("General") + shell: julia --color=yes {0} + - name: "Install CompatHelper" + run: | + import Pkg + name = "CompatHelper" + uuid = "aa819f21-2bde-4658-8897-bab36330d9b7" + version = "3" + Pkg.add(; name, uuid, version) + shell: julia --color=yes {0} + - name: "Run CompatHelper" + run: | + import CompatHelper + CompatHelper.main() + shell: julia --color=yes {0} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba39cc5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Manifest.toml diff --git a/Project.toml b/Project.toml index 634ad9f..37f2b67 100644 --- a/Project.toml +++ b/Project.toml @@ -1,12 +1,16 @@ name = "Tau" uuid = "c544e3c2-d3e5-5802-ac44-44683f340e4a" -version = "0.2.0" +version = "0.3.0" + +[deps] +IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6" + +[compat] +IrrationalConstants = "0.1" +julia = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] test = ["Test"] - -[compat] -julia = "1" diff --git a/src/Tau.jl b/src/Tau.jl index 7d9b3ad..c5e7cbf 100644 --- a/src/Tau.jl +++ b/src/Tau.jl @@ -1,17 +1,27 @@ -VERSION < v"0.7.0-beta2.199" && __precompile__() - module Tau + +import IrrationalConstants + export tau, τ, sintau, costau, modtau, sinτ, cosτ, modτ -# Use overridden macro definition to define conversion methods for tau -Base.@irrational τ 6.28318530717958647692 (2 * big(pi)) +# Definition of τ as irrational constant +const τ = IrrationalConstants.twoπ const tau = τ -include("trig.jl") +const modτ = mod2pi +const modtau = modτ + +# Trigonometric functions +sinτ(x) = sinpi(2 * x) +cosτ(x) = cospi(2 * x) + +# Optimization for integers +sinτ(x::Integer) = zero(float(x)) +cosτ(x::Integer) = one(float(x)) -modtau(x) = Base.mod2pi(x) -const modτ = modtau +const sintau = sinτ +const costau = cosτ end diff --git a/src/trig.jl b/src/trig.jl deleted file mode 100644 index ebc4a57..0000000 --- a/src/trig.jl +++ /dev/null @@ -1,102 +0,0 @@ -# Adapted from julia/special/trig.jl - -if VERSION < v"0.7.0-beta2.199" - function Base.DomainError(x) - throw(DomainError()) - end -end - -function sintau(x::Real) - if !isfinite(x) - isnan(x) && return x - throw(DomainError(x)) - end - - rx = copysign(float(rem(x,1)),x) - arx = abs(rx) - - if arx <= oftype(rx,1/8) - return sin(tau*rx) - elseif arx <= oftype(rx,3/8) - arx = oftype(rx,1/4) - arx - return copysign(cos(tau*arx),rx) - elseif arx < oftype(rx,5/8) - rx = (oftype(rx,1/2) - arx)*sign(rx) - return sin(tau*rx) - elseif arx <= oftype(rx,7/8) - arx = oftype(rx,3/4) - arx - return -copysign(cos(tau*arx),rx) - else - rx = rx - copysign(one(rx),rx) - return sin(tau*rx) - end -end - -function costau(x::Real) - if !isfinite(x) - isnan(x) && return x - throw(DomainError(x)) - end - - rx = abs(float(rem(x,1))) - - if rx <= oftype(rx,1/8) - return cos(tau*rx) - elseif rx < oftype(rx,3/8) - rx = oftype(rx,1/4) - rx - return sin(tau*rx) - elseif rx <= oftype(rx,5/8) - rx = oftype(rx,1/2) - rx - return -cos(tau*rx) - elseif rx < oftype(rx,7/8) - rx = rx - oftype(rx,3/4) - return sin(tau*rx) - else - rx = one(rx) - rx - return cos(tau*rx) - end -end - -sintau(x::Integer) = zero(x) -costau(x::Integer) = one(x) - -# Implementations of complex sintau/costau are adapted from julia/base/special/trig.jl. -function sintau(z::Complex{T}) where T - F = float(T) - zr, zi = reim(z) - if isinteger(zr) - Complex(zero(F), sinh(tau * zi)) - elseif !isfinite(zr) - if zi == 0 || isinf(zi) - Complex(F(NaN), F(zi)) - else - Complex(F(NaN), F(NaN)) - end - else - tauzi = tau * zi - Complex(sintau(zr) * cosh(tauzi), costau(zr) * sinh(tauzi)) - end -end - -function costau(z::Complex{T}) where T - F = float(T) - zr, zi = reim(z) - if isinteger(zr) - s = copysign(zero(F), zr) - Complex(cosh(tau * zi), isnan(zi) ? s : -flipsign(s,zi)) - elseif !isfinite(zr) - if zi == 0 - Complex(F(NaN), isnan(zr) ? zero(F) : -flipsign(F(zi), zr)) - elseif isinf(zi) - Complex(F(Inf), F(NaN)) - else - Complex(F(NaN), F(NaN)) - end - else - tauzi = tau * zi - Complex(costau(zr) * cosh(tauzi), -sintau(zr) * sinh(tauzi)) - end -end - -const sinτ = sintau -const cosτ = costau diff --git a/test/runtests.jl b/test/runtests.jl index e9cc08b..5a90675 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,15 +1,13 @@ -include("../src/Tau.jl") -using Main.Tau -VERSION < v"0.7.0-beta2.199" ? using Base.Test : using Test +using Tau +using Test @testset "self-identity" begin - @test isa(tau, Irrational) - @test τ == τ - @test τ == tau + @test tau isa Irrational{:twoπ} + @test τ === τ + @test τ === tau end @testset "tau vs. 2pi" begin - @testset "symbols" begin @test τ ≠ 2π # tau is Irrational, can't be equal to an AbstractFloat @test 2π ≠ τ @@ -29,13 +27,10 @@ end @test Float64(Float32(tau)) == Float64(2 * Float32(pi)) @test BigFloat(tau) == 2 * BigFloat(pi) end - end @testset "sintau/costau" 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)))) @@ -50,11 +45,9 @@ end @test @inferred(costau(z)) ≈ cospi(2 * z) end end - end @testset "exact results for integer inputs" begin - @testset "real and complex values passed as integer types" begin for T = (Int, Complex), x = -3:3 @test @inferred(sintau(T(x))) == zero(T) @@ -68,11 +61,9 @@ end @test @inferred(costau(T(x))) == one(T) end end - end @testset "corner cases for abnormal inputs" begin - @testset "real values" begin for x in (Inf, -Inf) @test_throws DomainError sintau(x) @@ -89,13 +80,12 @@ end @test isequal(@inferred(costau(z)), cospi(2 * 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}) == [T] + @test Base.return_types(f, Tuple{T}) == [float(T)] @test Base.return_types(f, Tuple{Complex{T}}) == [Complex{float(T)}] end end From e1a8aef28a4ae6a6290a77dd9786ea5086c934d8 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Fri, 8 Apr 2022 22:18:43 +0200 Subject: [PATCH 2/3] Bump to 1.0.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mosè Giordano --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 37f2b67..86da7e3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Tau" uuid = "c544e3c2-d3e5-5802-ac44-44683f340e4a" -version = "0.3.0" +version = "1.0.0" [deps] IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6" From c3f187d5bd885e006bcec00266f0af0f86ca1bd0 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Fri, 8 Apr 2022 23:05:37 +0200 Subject: [PATCH 3/3] Update README --- README.md | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 29a5bf6..a8b4965 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,8 @@


-[![travis][travis-img]](https://travis-ci.org/JuliaMath/Tau.jl) -[![appveyor][appveyor-img]](https://ci.appveyor.com/project/waldyrious/tau-jl) -[![codecov][codecov-img]](http://codecov.io/github/JuliaMath/Tau.jl) - -[travis-img]: https://img.shields.io/travis/JuliaMath/Tau.jl/master.svg?label=Linux,%20macOS -[appveyor-img]: https://img.shields.io/appveyor/ci/waldyrious/tau-jl/master.svg?label=Windows -[codecov-img]: https://img.shields.io/codecov/c/github/JuliaMath/Tau.jl/master.svg?label=coverage +[![CI](https://github.com/JuliaMath/Tau.jl/actions/workflows/CI.yml/badge.svg?branch=master)](https://github.com/JuliaMath/Tau.jl/actions/workflows/CI.yml?query=branch%3Amaster) +[![codecov](https://img.shields.io/codecov/c/github/JuliaMath/Tau.jl/master.svg?label=coverage)](http://codecov.io/github/JuliaMath/Tau.jl) This [Julia](https://github.com/JuliaLang/julia) [package](http://pkg.julialang.org/) defines the [Tau](http://www.tauday.com/tau-manifesto) constant @@ -23,10 +18,13 @@ tau ≈ 2*pi After installing this package with `Pkg.add("Tau")`, it can be used as follows: ```julia -using Tau +julia> using Tau + +julia> tau === τ ≈ 2*pi +true -tau == τ ≈ 2*pi # => true -typeof(tau) # => Irrational{:τ} +julia> typeof(tau) +Irrational{:twoπ} ``` Note: to input the τ character, type `\tau` then press Tab. @@ -34,10 +32,18 @@ Note: to input the τ character, type `\tau` then press Tab. The tau variants of `sinpi`, `cospi`, and `mod2pi` are also defined: ```julia -sintau(1//4) # => 1.0 -costau(1//2) # => -1.0 +julia> sintau(1//4) +1.0 + +julia> costau(1//2) +-1.0 + +julia> modtau(9*pi/4) +0.7853981633974481 ``` +Alternatively, one can use the Unicode aliases `sinτ`, `cosτ`, and `modτ`. + ## The tau != 2pi inequality When this package was first created, the equality `tau == 2pi` did hold true,