@@ -50,146 +50,146 @@ function test_fixed(::Type{T}, f) where {T}
5050end
5151
5252@testset " conversion" begin
53- @test isapprox (convert (Fixed{Int8,7 }, 0.8 ), 0.797 , atol= 0.001 )
54- @test isapprox (convert (Fixed{Int8,7 }, 0.9 ), 0.898 , atol= 0.001 )
55- @test_throws InexactError convert (Fixed{Int8, 7 }, 0.999 )
56- @test_throws InexactError convert (Fixed{Int8, 7 }, 1.0 )
57- @test_throws InexactError convert (Fixed{Int8, 7 }, 1 )
58- @test_throws InexactError convert (Fixed{Int8, 7 }, 2 )
59- @test_throws InexactError convert (Fixed{Int8, 7 }, 128 )
53+ @test isapprox (convert (Fixed{Int8,7 }, 0.8 ), 0.797 , atol= 0.001 )
54+ @test isapprox (convert (Fixed{Int8,7 }, 0.9 ), 0.898 , atol= 0.001 )
55+ @test_throws InexactError convert (Fixed{Int8, 7 }, 0.999 )
56+ @test_throws InexactError convert (Fixed{Int8, 7 }, 1.0 )
57+ @test_throws InexactError convert (Fixed{Int8, 7 }, 1 )
58+ @test_throws InexactError convert (Fixed{Int8, 7 }, 2 )
59+ @test_throws InexactError convert (Fixed{Int8, 7 }, 128 )
6060end
6161
6262@testset " test_fixed" begin
63- for (TI, f) in [(Int8, 8 ), (Int16, 8 ), (Int16, 10 ), (Int32, 16 )]
64- T = Fixed{TI,f}
65- # println(" Testing $T")
66- test_fixed (T, f)
67- end
63+ for (TI, f) in [(Int8, 8 ), (Int16, 8 ), (Int16, 10 ), (Int32, 16 )]
64+ T = Fixed{TI,f}
65+ # println(" Testing $T")
66+ test_fixed (T, f)
67+ end
6868end
6969
7070@testset " modulus" begin
71- T = Fixed{Int8,7 }
72- for i = - 1.0 : 0.1 : typemax (T)
73- @test i % T === T (i)
74- end
75- @test ( 1.5 % T). i == round (Int, 1.5 * 128 ) % Int8
76- @test (- 0.3 % T). i == round (Int, - 0.3 * 128 ) % Int8
71+ T = Fixed{Int8,7 }
72+ for i = - 1.0 : 0.1 : typemax (T)
73+ @test i % T === T (i)
74+ end
75+ @test ( 1.5 % T). i == round (Int, 1.5 * 128 ) % Int8
76+ @test (- 0.3 % T). i == round (Int, - 0.3 * 128 ) % Int8
7777
78- T = Fixed{Int16,9 }
79- for i = - 64.0 : 0.1 : typemax (T)
80- @test i % T === T (i)
81- end
82- @test ( 65.2 % T). i == round (Int, 65.2 * 512 ) % Int16
83- @test (- 67.2 % T). i == round (Int, - 67.2 * 512 ) % Int16
78+ T = Fixed{Int16,9 }
79+ for i = - 64.0 : 0.1 : typemax (T)
80+ @test i % T === T (i)
81+ end
82+ @test ( 65.2 % T). i == round (Int, 65.2 * 512 ) % Int16
83+ @test (- 67.2 % T). i == round (Int, - 67.2 * 512 ) % Int16
8484end
8585
8686@testset " testapprox" begin
87- for T in [Fixed{Int8,7 }, Fixed{Int16,8 }, Fixed{Int16,10 }]
88- testapprox (T) # defined in ufixed.jl
89- end
87+ for T in [Fixed{Int8,7 }, Fixed{Int16,8 }, Fixed{Int16,10 }]
88+ testapprox (T) # defined in ufixed.jl
89+ end
9090end
9191
9292@testset " reductions" begin
93- F8 = Fixed{Int8,8 }
94- a = F8[0.498 , 0.1 ]
95- acmp = Float64 (a[1 ]) + Float64 (a[2 ])
96- @test sum (a) == acmp
97- @test sum (a, dims= 1 ) == [acmp]
93+ F8 = Fixed{Int8,8 }
94+ a = F8[0.498 , 0.1 ]
95+ acmp = Float64 (a[1 ]) + Float64 (a[2 ])
96+ @test sum (a) == acmp
97+ @test sum (a, dims= 1 ) == [acmp]
9898
99- F6 = Fixed{Int8,6 }
100- a = F6[1.2 , 1.4 ]
101- acmp = Float64 (a[1 ])* Float64 (a[2 ])
102- @test prod (a) == acmp
103- @test prod (a, dims= 1 ) == [acmp]
99+ F6 = Fixed{Int8,6 }
100+ a = F6[1.2 , 1.4 ]
101+ acmp = Float64 (a[1 ])* Float64 (a[2 ])
102+ @test prod (a) == acmp
103+ @test prod (a, dims= 1 ) == [acmp]
104104end
105105
106106@testset " convert result type" begin
107- x = Fixed {Int8,8} (0.3 )
108- for T in (Float16, Float32, Float64, BigFloat)
109- y = convert (T, x)
110- @test isa (y, T)
111- end
107+ x = Fixed {Int8,8} (0.3 )
108+ for T in (Float16, Float32, Float64, BigFloat)
109+ y = convert (T, x)
110+ @test isa (y, T)
111+ end
112112end
113113
114114@testset " Integer conversions" begin
115- @test convert (Int, Q1f6 (1 )) === 1
116- @test convert (Integer, Q1f6 (1 )) === Int8 (1 )
115+ @test convert (Int, Q1f6 (1 )) === 1
116+ @test convert (Integer, Q1f6 (1 )) === Int8 (1 )
117117end
118118
119119@testset " Floating-point conversions" begin
120- @test isa (float (one (Fixed{Int8,6 })), Float32)
121- @test isa (float (one (Fixed{Int32,18 })), Float64)
122- @test isa (float (one (Fixed{Int32,25 })), Float64)
120+ @test isa (float (one (Fixed{Int8,6 })), Float32)
121+ @test isa (float (one (Fixed{Int32,18 })), Float64)
122+ @test isa (float (one (Fixed{Int32,25 })), Float64)
123123end
124124
125125@testset " Show" begin
126- x = Fixed {Int32,5} (0.25 )
127- iob = IOBuffer ()
128- show (iob, x)
129- str = String (take! (iob))
130- @test str == " 0.25Q26f5"
131- @test eval (Meta. parse (str)) == x
126+ x = Fixed {Int32,5} (0.25 )
127+ iob = IOBuffer ()
128+ show (iob, x)
129+ str = String (take! (iob))
130+ @test str == " 0.25Q26f5"
131+ @test eval (Meta. parse (str)) == x
132132end
133133
134134@testset " rand" begin
135- for T in (Fixed{Int8,8 }, Fixed{Int16,8 }, Fixed{Int16,10 }, Fixed{Int32,16 })
136- a = rand (T)
137- @test isa (a, T)
138- a = rand (T, (3 , 5 ))
139- @test ndims (a) == 2 && eltype (a) == T
140- @test size (a) == (3 ,5 )
141- end
135+ for T in (Fixed{Int8,8 }, Fixed{Int16,8 }, Fixed{Int16,10 }, Fixed{Int32,16 })
136+ a = rand (T)
137+ @test isa (a, T)
138+ a = rand (T, (3 , 5 ))
139+ @test ndims (a) == 2 && eltype (a) == T
140+ @test size (a) == (3 ,5 )
141+ end
142142end
143143
144144@testset " floatmin" begin
145- # issue #79
146- @test floatmin (Q11f4) == Q11f4 (0.06 )
145+ # issue #79
146+ @test floatmin (Q11f4) == Q11f4 (0.06 )
147147end
148148
149149@testset " Disambiguation constructors" begin
150- @test_throws ArgumentError Fixed {Int32,16} (' a' )
151- @test_throws InexactError Fixed {Int32,16} (complex (1.0 , 1.0 ))
152- @test Fixed {Int32,16} (complex (1.0 , 0.0 )) == 1
153- @test Fixed {Int32,16} (Base. TwicePrecision (1.0 , 0.0 )) == 1
150+ @test_throws ArgumentError Fixed {Int32,16} (' a' )
151+ @test_throws InexactError Fixed {Int32,16} (complex (1.0 , 1.0 ))
152+ @test Fixed {Int32,16} (complex (1.0 , 0.0 )) == 1
153+ @test Fixed {Int32,16} (Base. TwicePrecision (1.0 , 0.0 )) == 1
154154end
155155
156156@testset " fractional fixed-point numbers" begin
157- # test all-fractional fixed-point numbers (issue #104)
158- for (T, f) in ((Int8, 7 ),
159- (Int16, 15 ),
160- (Int32, 31 ),
161- (Int64, 63 ))
162- tmax = typemax (Fixed{T, f})
163- @test tmax == BigInt (typemax (T)) / BigInt (2 )^ f
164- tol = (tmax + BigFloat (1.0 )) / (sizeof (T) * 8 )
165- for x in range (- 1 , stop= BigFloat (tmax)- tol, length= 50 )
166- @test abs (Fixed {T, f} (x) - x) <= tol
157+ # test all-fractional fixed-point numbers (issue #104)
158+ for (T, f) in ((Int8, 7 ),
159+ (Int16, 15 ),
160+ (Int32, 31 ),
161+ (Int64, 63 ))
162+ tmax = typemax (Fixed{T, f})
163+ @test tmax == BigInt (typemax (T)) / BigInt (2 )^ f
164+ tol = (tmax + BigFloat (1.0 )) / (sizeof (T) * 8 )
165+ for x in range (- 1 , stop= BigFloat (tmax)- tol, length= 50 )
166+ @test abs (Fixed {T, f} (x) - x) <= tol
167+ end
167168 end
168169end
169170
170171@testset " Promotion within Fixed" begin
171- @test @inferred (promote (Q0f7 (0.25 ), Q0f7 (0.75 ))) ===
172- (Q0f7 (0.25 ), Q0f7 (0.75 ))
173- @test @inferred (promote (Fixed {Int16,3} (0.25 ), Fixed {Int8,3} (0.875 ))) ===
174- (Fixed {Int16,3} (0.25 ), Fixed {Int16,3} (0.875 ))
175- @test @inferred (promote (Fixed {Int8,6} (0.125 ), Fixed {Int8,4} (0.75 ))) ===
176- (Fixed {Int16,6} (0.125 ), Fixed {Int16,6} (0.75 ))
177-
178- @test Fixed {Int16,15} (- 1 ) == Fixed {Int8,7} (- 1 )
179- @test Fixed {Int16,15} (0.25 ) == Fixed {Int8,7} (0.25 )
180- @test Fixed {Int16,7} (- 1 ) == Fixed {Int8,7} (- 1 )
181- @test Fixed {Int16,7} (0.25 ) == Fixed {Int8,7} (0.25 )
182- @test Fixed {Int16,15} (- 1 ) == Fixed {Int8,5} (- 1 )
183- @test Fixed {Int16,15} (5 / 32 ) == Fixed {Int8,5} (5 / 32 )
184- @test Fixed {Int16,3} (- 1 ) == Fixed {Int8,5} (- 1 )
185- @test Fixed {Int16,3} (0.25 ) == Fixed {Int8,5} (0.25 )
186-
187- @test promote_type (Q0f7,Float32,Int) == Float32
188- @test promote_type (Q0f7,Int,Float32) == Float32
189- @test promote_type (Int,Q0f7,Float32) == Float32
190- @test promote_type (Int,Float32,Q0f7) == Float32
191- @test promote_type (Float32,Int,Q0f7) == Float32
192- @test promote_type (Float32,Q0f7,Int) == Float32
193- @test promote_type (Q0f7,Q1f6,Q2f5,Q3f4,Q4f3,Q5f2) == Fixed{Int128,7 }
194- end
172+ @test @inferred (promote (Q0f7 (0.25 ), Q0f7 (0.75 ))) ===
173+ (Q0f7 (0.25 ), Q0f7 (0.75 ))
174+ @test @inferred (promote (Fixed {Int16,3} (0.25 ), Fixed {Int8,3} (0.875 ))) ===
175+ (Fixed {Int16,3} (0.25 ), Fixed {Int16,3} (0.875 ))
176+ @test @inferred (promote (Fixed {Int8,6} (0.125 ), Fixed {Int8,4} (0.75 ))) ===
177+ (Fixed {Int16,6} (0.125 ), Fixed {Int16,6} (0.75 ))
178+
179+ @test Fixed {Int16,15} (- 1 ) == Fixed {Int8,7} (- 1 )
180+ @test Fixed {Int16,15} (0.25 ) == Fixed {Int8,7} (0.25 )
181+ @test Fixed {Int16,7} (- 1 ) == Fixed {Int8,7} (- 1 )
182+ @test Fixed {Int16,7} (0.25 ) == Fixed {Int8,7} (0.25 )
183+ @test Fixed {Int16,15} (- 1 ) == Fixed {Int8,5} (- 1 )
184+ @test Fixed {Int16,15} (5 / 32 ) == Fixed {Int8,5} (5 / 32 )
185+ @test Fixed {Int16,3} (- 1 ) == Fixed {Int8,5} (- 1 )
186+ @test Fixed {Int16,3} (0.25 ) == Fixed {Int8,5} (0.25 )
187+
188+ @test promote_type (Q0f7,Float32,Int) == Float32
189+ @test promote_type (Q0f7,Int,Float32) == Float32
190+ @test promote_type (Int,Q0f7,Float32) == Float32
191+ @test promote_type (Int,Float32,Q0f7) == Float32
192+ @test promote_type (Float32,Int,Q0f7) == Float32
193+ @test promote_type (Float32,Q0f7,Int) == Float32
194+ @test promote_type (Q0f7,Q1f6,Q2f5,Q3f4,Q4f3,Q5f2) == Fixed{Int128,7 }
195195end
0 commit comments