@@ -2,6 +2,11 @@ using IrrationalConstants
22using Documenter
33using 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π)
4853end
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
5459end
5560
5661@testset " doctests" begin
57- DocMeta. setdocmeta! (
58- IrrationalConstants, :DocTestSetup , :(using IrrationalConstants); recursive= true
59- )
6062 doctest (IrrationalConstants; manual= false )
6163end
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
7193end
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))
81103 end
82104end
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
86108end
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π)
181205 @test sec (quartπ) === Float64 (sec (big (quartπ)))
182206 @test cot (quartπ) === Float64 (cot (big (quartπ)))
183207end
208+
209+ # Ref https://github.com/JuliaLang/julia/pull/46054
210+ IrrationalConstants. @irrational irrational_1548_pi 4863.185427757 1548 big (pi )
211+ IrrationalConstants. @irrational irrational_inv_1548_pi 1 / big (irrational_1548_pi)
212+ @testset " IrrationalConstants.@irrational" begin
213+ @test irrational_1548_pi ≈ 1548 big (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 (1548 big (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