Skip to content

Commit cde03bb

Browse files
authored
Fix tests and update with recent changes in Base (#36)
1 parent 3734ce6 commit cde03bb

File tree

4 files changed

+174
-80
lines changed

4 files changed

+174
-80
lines changed

Project.toml

+1-1
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.2"
4+
version = "0.2.3"
55

66
[compat]
77
julia = "1"

src/macro.jl

+46-26
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@ if VERSION < v"1.2.0-DEV.337"
3434
Base.inv(x::IrrationalConstant) = 1/x
3535
end
3636

37+
# https://github.com/JuliaLang/julia/pull/50894
38+
Base.typemin(::Type{T}) where {T<:IrrationalConstant} = T()
39+
Base.typemax(::Type{T}) where {T<:IrrationalConstant} = T()
40+
3741
"""
38-
@irrational sym val def [T]
39-
@irrational(sym, val, def[, T])
42+
@irrational sym [val] def [T]
43+
44+
Define an instance named `sym` of a new singleton type `T` representing an irrational constant as subtype of
45+
`IrrationalConstants.IrrationalConstant <: AbstractIrrational`,
46+
with arbitrary-precision definition in terms of `BigFloat`s given by the expression `def`.
4047
41-
Define a new singleton type `T` representing an irrational constant as subtype of
42-
`IrrationalConstants.IrrationalConstant <: AbstractIrrational` with an instance named `sym`, pre-computed `Float64` value `val`,
43-
and arbitrary-precision definition in terms of `BigFloat`s given by the expression `def`.
48+
Optionally provide a pre-computed `Float64` value `val` which must equal `Float64(def)`.
49+
It will be computed automatically if omitted.
4450
4551
As default, `T` is set to `sym` with the first character converted to uppercase.
4652
@@ -49,30 +55,36 @@ returns `false`.
4955
5056
# Examples
5157
52-
```jldoctest
53-
julia> IrrationalConstants.@irrational(twoπ, 6.2831853071795864769, 2*big(π))
58+
```jldoctest; setup = :(import IrrationalConstants)
59+
julia> IrrationalConstants.@irrational twoπ 2*big(π)
5460
5561
julia> twoπ
5662
twoπ = 6.2831853071795...
5763
58-
julia> IrrationalConstants.@irrational sqrt2 1.4142135623730950488 √big(2)
64+
julia> IrrationalConstants.@irrational sqrt2 1.4142135623730950488 √big(2)
5965
6066
julia> sqrt2
6167
sqrt2 = 1.4142135623730...
6268
63-
julia> IrrationalConstants.@irrational halfτ 3.14159265358979323846 pi
69+
julia> IrrationalConstants.@irrational halfτ 3.14159265358979323846 pi
6470
6571
julia> halfτ
6672
halfτ = 3.1415926535897...
6773
68-
julia> IrrationalConstants.@irrational sqrt2 1.4142135623730950488 big(2)
69-
ERROR: AssertionError: big($(Expr(:escape, :sqrt2))) isa BigFloat
74+
julia> IrrationalConstants.@irrational sqrt3 1.7320508075688772 big(3)
75+
ERROR: AssertionError: big($(Expr(:escape, :sqrt3))) isa BigFloat
7076
71-
julia> IrrationalConstants.@irrational sqrt2 1.41421356237309 √big(2)
72-
ERROR: AssertionError: Float64($(Expr(:escape, :sqrt2))) == Float64(big($(Expr(:escape, :sqrt2))))
77+
julia> IrrationalConstants.@irrational sqrt5 2.2360679775 √big(5)
78+
ERROR: AssertionError: Float64($(Expr(:escape, :sqrt5))) == Float64(big($(Expr(:escape, :sqrt5))))
7379
```
7480
"""
75-
macro irrational(sym, val, def, T=Symbol(uppercasefirst(string(sym))))
81+
macro irrational(sym::Symbol, val::Float64, def::Union{Symbol,Expr}, T::Symbol=Symbol(uppercasefirst(string(sym))))
82+
irrational(sym, val, def, T)
83+
end
84+
macro irrational(sym::Symbol, def::Union{Symbol,Expr}, T::Symbol=Symbol(uppercasefirst(string(sym))))
85+
irrational(sym, :(big($(esc(sym)))), def, T)
86+
end
87+
function irrational(sym::Symbol, val::Union{Float64,Expr}, def::Union{Symbol,Expr}, T::Symbol)
7688
esym = esc(sym)
7789
qsym = esc(Expr(:quote, sym))
7890
eT = esc(T)
@@ -90,17 +102,23 @@ macro irrational(sym, val, def, T=Symbol(uppercasefirst(string(sym))))
90102
end
91103
else
92104
# newer Julia versions
93-
isa(def, Symbol) ? quote
94-
function Base.BigFloat(::$eT, r::Base.MPFR.MPFRRoundingMode=Base.MPFR.ROUNDING_MODE[]; precision=precision(BigFloat))
95-
c = BigFloat(; precision=precision)
96-
ccall(($(string("mpfr_const_", def)), :libmpfr),
97-
Cint, (Ref{BigFloat}, Base.MPFR.MPFRRoundingMode), c, r)
98-
return c
105+
if isa(def, Symbol)
106+
# support older Julia versions prior to https://github.com/JuliaLang/julia/pull/51362
107+
r = VERSION < v"1.12.0-DEV.78" ? :(Base.MPFR.ROUNDING_MODE[]) : :(Base.Rounding.rounding_raw(BigFloat))
108+
quote
109+
function Base.BigFloat(::$eT, r::Base.MPFR.MPFRRoundingMode=$r; precision=precision(BigFloat))
110+
c = BigFloat(; precision=precision)
111+
ccall(($(string("mpfr_const_", def)), :libmpfr),
112+
Cint, (Ref{BigFloat}, Base.MPFR.MPFRRoundingMode), c, r)
113+
return c
114+
end
99115
end
100-
end : quote
101-
function Base.BigFloat(::$eT; precision=precision(BigFloat))
102-
setprecision(BigFloat, precision) do
103-
$(esc(def))
116+
else
117+
quote
118+
function Base.BigFloat(::$eT; precision=precision(BigFloat))
119+
setprecision(BigFloat, precision) do
120+
$(esc(def))
121+
end
104122
end
105123
end
106124
end
@@ -109,8 +127,10 @@ macro irrational(sym, val, def, T=Symbol(uppercasefirst(string(sym))))
109127
struct $T <: IrrationalConstant end
110128
const $esym = $eT()
111129
$bigconvert
112-
Base.Float64(::$eT) = $val
113-
Base.Float32(::$eT) = $(Float32(val))
130+
let v = $val, v64 = Float64(v), v32 = Float32(v)
131+
Base.Float64(::$eT) = v64
132+
Base.Float32(::$eT) = v32
133+
end
114134
Base.show(io::IO, ::$eT) = print(io, $qsym)
115135
@assert isa(big($esym), BigFloat)
116136
@assert Float64($esym) == Float64(big($esym))

src/stats.jl

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
# mathematical constants related to statistics
22

3-
@irrational twoπ 6.2831853071795864769 2 * big(π)
4-
@irrational fourπ 12.566370614359172954 4 * big(π)
5-
@irrational halfπ 1.5707963267948966192 big(π) / 2
6-
@irrational quartπ 0.7853981633974483096 big(π) / 4
3+
@irrational twoπ 2 * big(π)
4+
@irrational fourπ 4 * big(π)
5+
@irrational halfπ big(π) / 2
6+
@irrational quartπ big(π) / 4
77

8-
@irrational invπ 0.31830988618379067154 inv(big(π))
9-
@irrational twoinvπ 0.63661977236758134308 2 / big(π)
10-
@irrational fourinvπ 1.27323954473516268615 4 / big(π)
11-
@irrational inv2π 0.159154943091895335769 inv(2 * big(π))
12-
@irrational inv4π 0.079577471545947667884 inv(4 * big(π))
8+
@irrational invπ inv(big(π))
9+
@irrational twoinvπ 2 / big(π)
10+
@irrational fourinvπ 4 / big(π)
11+
@irrational inv2π inv(2 * big(π))
12+
@irrational inv4π inv(4 * big(π))
1313

14-
@irrational sqrt2 1.4142135623730950488 sqrt(big(2))
15-
@irrational sqrt3 1.7320508075688772935 sqrt(big(3))
16-
@irrational sqrtπ 1.7724538509055160273 sqrt(big(π))
17-
@irrational sqrt2π 2.5066282746310005024 sqrt(2 * big(π))
18-
@irrational sqrt4π 3.5449077018110320546 sqrt(4 * big(π))
19-
@irrational sqrthalfπ 1.2533141373155002512 sqrt(big(π) / 2)
14+
@irrational sqrt2 sqrt(big(2))
15+
@irrational sqrt3 sqrt(big(3))
16+
@irrational sqrtπ sqrt(big(π))
17+
@irrational sqrt2π sqrt(2 * big(π))
18+
@irrational sqrt4π sqrt(4 * big(π))
19+
@irrational sqrthalfπ sqrt(big(π) / 2)
2020

21-
@irrational invsqrt2 0.7071067811865475244 inv(sqrt(big(2)))
22-
@irrational invsqrtπ 0.5641895835477563 inv(sqrt(big(π)))
23-
@irrational invsqrt2π 0.3989422804014326779 inv(sqrt(2 * big(π)))
21+
@irrational invsqrt2 inv(sqrt(big(2)))
22+
@irrational invsqrtπ inv(sqrt(big(π)))
23+
@irrational invsqrt2π inv(sqrt(2 * big(π)))
2424

25-
@irrational loghalf -0.6931471805599453094 log(inv(big(2)))
26-
@irrational logtwo 0.6931471805599453094 log2
27-
@irrational logten 2.302585092994046 log(big(10))
28-
@irrational logπ 1.1447298858494001741 log(big(π))
29-
@irrational log2π 1.8378770664093454836 log(2 * big(π))
30-
@irrational log4π 2.5310242469692907930 log(4 * big(π))
25+
@irrational loghalf log(inv(big(2)))
26+
@irrational logtwo log2
27+
@irrational logten log(big(10))
28+
@irrational logπ log(big(π))
29+
@irrational log2π log(2 * big(π))
30+
@irrational log4π log(4 * big(π))

test/runtests.jl

+103-29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ using IrrationalConstants
22
using Documenter
33
using Test
44

5+
const ALLCONSTANTS = filter!(
6+
x -> x isa IrrationalConstants.IrrationalConstant,
7+
map(Base.Fix1(getproperty, IrrationalConstants), names(IrrationalConstants)),
8+
)
9+
510
@testset "k*pi" begin
611
@test isapprox(2*pi, twoπ)
712
@test isapprox(4*pi, fourπ)
@@ -48,30 +53,47 @@ end
4853
end
4954

5055
@testset "hash" begin
51-
for i in (twoπ, invπ, sqrt2, logtwo), j in (twoπ, invπ, sqrt2, logtwo)
56+
for i in ALLCONSTANTS, j in ALLCONSTANTS
5257
@test isequal(i==j, hash(i)==hash(j))
5358
end
5459
end
5560

5661
@testset "doctests" begin
57-
DocMeta.setdocmeta!(
58-
IrrationalConstants, :DocTestSetup, :(using IrrationalConstants); recursive=true
59-
)
6062
doctest(IrrationalConstants; manual=false)
6163
end
6264

6365
# copied from https://github.com/JuliaLang/julia/blob/cf5ae0369ceae078cf6a29d7aa34f48a5a53531e/test/numbers.jl
6466
# and adapted to irrationals in this package
6567

66-
@testset "IrrationalConstant zero and one" begin
67-
@test one(twoπ) === true
68-
@test zero(twoπ) === false
69-
@test one(typeof(twoπ)) === true
70-
@test zero(typeof(twoπ)) === false
68+
@testset "IrrationalConstants zero and one" begin
69+
for i in ALLCONSTANTS
70+
@test one(i) === true
71+
@test zero(i) === false
72+
@test one(typeof(i)) === true
73+
@test zero(typeof(i)) === false
74+
end
75+
end
76+
77+
@testset "IrrationalConstants iszero, isfinite, isinteger, and isone" begin
78+
for i in ALLCONSTANTS
79+
@test !iszero(i)
80+
@test !isone(i)
81+
@test !isinteger(i)
82+
@test isfinite(i)
83+
end
84+
end
85+
86+
@testset "IrrationalConstants promote_type" begin
87+
for T in (Float16, Float32, Float64)
88+
for i in ALLCONSTANTS
89+
@test T(2.0) * i T(2.0) * T(i)
90+
@test T(2.0) * i isa T
91+
end
92+
end
7193
end
7294

7395
@testset "IrrationalConstants compared with IrrationalConstants" begin
74-
for i in (twoπ, invπ, sqrt2, logtwo), j in (twoπ, invπ, sqrt2, logtwo)
96+
for i in ALLCONSTANTS, j in ALLCONSTANTS
7597
@test isequal(i==j, Float64(i)==Float64(j))
7698
@test isequal(i!=j, Float64(i)!=Float64(j))
7799
@test isequal(i<=j, Float64(i)<=Float64(j))
@@ -81,29 +103,31 @@ end
81103
end
82104
end
83105

84-
@testset "IrrationalConstant Inverses, JuliaLang/Julia Issue #30882" begin
106+
@testset "IrrationalConstants Inverses, JuliaLang/Julia Issue #30882" begin
85107
@test @inferred(inv(twoπ)) 0.15915494309189535
86108
end
87109

88110
@testset "IrrationalConstants compared with Rationals and Floats" begin
89-
@test Float64(twoπ, RoundDown) < twoπ
90-
@test Float64(twoπ, RoundUp) > twoπ
91-
@test !(Float64(twoπ, RoundDown) > twoπ)
92-
@test !(Float64(twoπ, RoundUp) < twoπ)
93-
@test Float64(twoπ, RoundDown) <= twoπ
94-
@test Float64(twoπ, RoundUp) >= twoπ
95-
@test Float64(twoπ, RoundDown) != twoπ
96-
@test Float64(twoπ, RoundUp) != twoπ
97-
98-
@test Float32(twoπ, RoundDown) < twoπ
99-
@test Float32(twoπ, RoundUp) > twoπ
100-
@test !(Float32(twoπ, RoundDown) > twoπ)
101-
@test !(Float32(twoπ, RoundUp) < twoπ)
102-
103-
@test prevfloat(big(twoπ)) < twoπ
104-
@test nextfloat(big(twoπ)) > twoπ
105-
@test !(prevfloat(big(twoπ)) > twoπ)
106-
@test !(nextfloat(big(twoπ)) < twoπ)
111+
for i in ALLCONSTANTS
112+
@test Float64(i, RoundDown) < i
113+
@test Float64(i, RoundUp) > i
114+
@test !(Float64(i, RoundDown) > i)
115+
@test !(Float64(i, RoundUp) < i)
116+
@test Float64(i, RoundDown) <= i
117+
@test Float64(i, RoundUp) >= i
118+
@test Float64(i, RoundDown) != i
119+
@test Float64(i, RoundUp) != i
120+
121+
@test Float32(i, RoundDown) < i
122+
@test Float32(i, RoundUp) > i
123+
@test !(Float32(i, RoundDown) > i)
124+
@test !(Float32(i, RoundUp) < i)
125+
126+
@test prevfloat(big(i)) < i
127+
@test nextfloat(big(i)) > i
128+
@test !(prevfloat(big(i)) > i)
129+
@test !(nextfloat(big(i)) < i)
130+
end
107131

108132
@test 5293386250278608690//842468587426513207 < twoπ
109133
@test !(5293386250278608690//842468587426513207 > twoπ)
@@ -181,3 +205,53 @@ end
181205
@test sec(quartπ) === Float64(sec(big(quartπ)))
182206
@test cot(quartπ) === Float64(cot(big(quartπ)))
183207
end
208+
209+
# Ref https://github.com/JuliaLang/julia/pull/46054
210+
IrrationalConstants.@irrational irrational_1548_pi 4863.185427757 1548big(pi)
211+
IrrationalConstants.@irrational irrational_inv_1548_pi 1/big(irrational_1548_pi)
212+
@testset "IrrationalConstants.@irrational" begin
213+
@test irrational_1548_pi 1548big(pi)
214+
@test Float64(irrational_1548_pi) == 1548π
215+
@test irrational_1548_pi 1548pi
216+
@test irrational_1548_pi != 1548pi
217+
@test irrational_inv_1548_pi inv(1548big(pi))
218+
@test Float64(irrational_inv_1548_pi) == 1/(1548π)
219+
@test irrational_inv_1548_pi inv(1548pi)
220+
@test irrational_inv_1548_pi != inv(1548pi)
221+
end
222+
223+
# Ref https://github.com/JuliaLang/julia/pull/50894
224+
@testset "irrational special values" begin
225+
for v ALLCONSTANTS
226+
@test v === typemin(v) === typemax(v)
227+
end
228+
end
229+
230+
# Ref https://github.com/JuliaLang/julia/pull/55911
231+
@testset "logtwo to `BigFloat` with `setrounding`" begin
232+
function irrational_to_big_float(c::AbstractIrrational)
233+
BigFloat(c)
234+
end
235+
236+
function irrational_to_big_float_with_rounding_mode(c::AbstractIrrational, rm::RoundingMode)
237+
f = () -> irrational_to_big_float(c)
238+
setrounding(f, BigFloat, rm)
239+
end
240+
241+
function irrational_to_big_float_with_rounding_mode_and_precision(c::AbstractIrrational, rm::RoundingMode, prec::Int)
242+
f = () -> irrational_to_big_float_with_rounding_mode(c, rm)
243+
setprecision(f, BigFloat, prec)
244+
end
245+
246+
# Prior to https://github.com/JuliaLang/julia/pull/40872 `setprecision(BigFloat, precision)` required precision >= 2
247+
minprecision = VERSION < v"1.8.0-DEV.367" ? 2 : 1
248+
249+
# logtwo is the only constant defined based on an MPFR constant (similar to π, γ, catalan)
250+
c = logtwo
251+
for p minprecision:40
252+
@test (
253+
irrational_to_big_float_with_rounding_mode_and_precision(c, RoundDown, p) < c <
254+
irrational_to_big_float_with_rounding_mode_and_precision(c, RoundUp, p)
255+
)
256+
end
257+
end

0 commit comments

Comments
 (0)