@@ -2,6 +2,11 @@ using IrrationalConstants
2
2
using Documenter
3
3
using Test
4
4
5
+ const ALLCONSTANTS = filter! (
6
+ x -> x isa IrrationalConstants. IrrationalConstant,
7
+ map (Base. Fix1 (getproperty, IrrationalConstants), names (IrrationalConstants)),
8
+ )
9
+
5
10
@testset " k*pi" begin
6
11
@test isapprox (2 * pi , twoπ)
7
12
@test isapprox (4 * pi , fourπ)
48
53
end
49
54
50
55
@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
52
57
@test isequal (i== j, hash (i)== hash (j))
53
58
end
54
59
end
55
60
56
61
@testset " doctests" begin
57
- DocMeta. setdocmeta! (
58
- IrrationalConstants, :DocTestSetup , :(using IrrationalConstants); recursive= true
59
- )
60
62
doctest (IrrationalConstants; manual= false )
61
63
end
62
64
63
65
# copied from https://github.com/JuliaLang/julia/blob/cf5ae0369ceae078cf6a29d7aa34f48a5a53531e/test/numbers.jl
64
66
# and adapted to irrationals in this package
65
67
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
71
93
end
72
94
73
95
@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
75
97
@test isequal (i== j, Float64 (i)== Float64 (j))
76
98
@test isequal (i!= j, Float64 (i)!= Float64 (j))
77
99
@test isequal (i<= j, Float64 (i)<= Float64 (j))
81
103
end
82
104
end
83
105
84
- @testset " IrrationalConstant Inverses, JuliaLang/Julia Issue #30882" begin
106
+ @testset " IrrationalConstants Inverses, JuliaLang/Julia Issue #30882" begin
85
107
@test @inferred (inv (twoπ)) ≈ 0.15915494309189535
86
108
end
87
109
88
110
@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
107
131
108
132
@test 5293386250278608690 // 842468587426513207 < twoπ
109
133
@test ! (5293386250278608690 // 842468587426513207 > twoπ)
181
205
@test sec (quartπ) === Float64 (sec (big (quartπ)))
182
206
@test cot (quartπ) === Float64 (cot (big (quartπ)))
183
207
end
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