142
142
@test occursin (" Q0f7 is an 8-bit type representing 256 values from -1.0 to 0.992;" , msg)
143
143
ret = @test_throws ArgumentError convert (Fixed{Int128,100 }, 10.0 ^ 9 )
144
144
msg = ret. value. msg
145
- @test occursin (" Fixed{Int128,100} is a 128-bit type representing 2^128 values" , msg)
145
+ @test occursin (" Fixed{Int128,$(SP) 100} is a 128-bit type representing 2^128 values" , msg)
146
146
end
147
147
148
148
@testset " disambiguation constructors" begin
@@ -214,17 +214,11 @@ end
214
214
@test float (0.75 Q7f24) === 0.75
215
215
@test float (0.75 Q10f53):: BigFloat == big " 0.75"
216
216
217
- @testset " floattype($F )" for F in target (Fixed, :i8 , :i16 , :i32 , :i64 ; ex = :heavy )
218
- @test typemax (F) <= maxintfloat (floattype (F))
219
- end
217
+ test_floattype (Fixed)
220
218
end
221
219
222
220
@testset " conversions from float" begin
223
- @testset " $F (nan)" for F in target (Fixed; ex = :thin )
224
- @test_throws ArgumentError F (Inf )
225
- @test_throws ArgumentError F (- Inf32 )
226
- @test_throws ArgumentError F (NaN )
227
- end
221
+ test_convert_from_nan (Fixed)
228
222
end
229
223
230
224
@testset " conversions to float" begin
@@ -234,22 +228,22 @@ end
234
228
235
229
for Tf in (Float16, Float32, Float64)
236
230
@testset " $Tf (::$F )" for F in target (Fixed, :i8 , :i16 )
237
- T, f = rawtype (F), nbitsfrac (F)
238
- float_err = 0.0
231
+ T, exp2mf = rawtype (F), big ( 2.0 ^- nbitsfrac (F) )
232
+ float_err = zero (Tf)
239
233
for i = typemin (T): typemax (T)
240
- f_expected = Tf (i * BigFloat ( 2 ) ^- f )
234
+ f_expected = Tf (i * exp2mf )
241
235
f_actual = Tf (reinterpret (F, i))
242
236
float_err += abs (f_actual - f_expected)
243
237
end
244
- @test float_err == 0.0
238
+ @test float_err == 0
245
239
end
246
240
@testset " $Tf (::$F )" for F in target (Fixed, :i32 , :i64 , :i128 )
247
- T, f = rawtype (F), nbitsfrac (F)
241
+ T, exp2mf = rawtype (F), big ( 2.0 ^- nbitsfrac (F) )
248
242
error_count = 0
249
243
for i in vcat (typemin (T): (typemin (T)+ 0xFF ),
250
244
- T (0xFF ): T (0xFF ),
251
245
(typemax (T)- 0xFF ): typemax (T))
252
- f_expected = Tf (i * BigFloat ( 2 ) ^- f )
246
+ f_expected = Tf (i * exp2mf )
253
247
isinf (f_expected) && break # for Float16() and Float32()
254
248
f_actual = Tf (reinterpret (F, i))
255
249
f_actual == f_expected && continue
@@ -271,31 +265,21 @@ end
271
265
end
272
266
273
267
@testset " type modulus" begin
268
+ test_rem_type (Fixed)
269
+ test_rem_nan (Fixed)
270
+
274
271
@test Q0f7 (0.2 ) % Q0f7 === Q0f7 (0.2 )
275
272
@test Q1f14 (1.2 ) % Q0f15 === Q0f15 (- 0.8 )
276
273
@test Q1f14 (1.2 ) % Q0f7 === Q0f7 (- 0.8 )
277
274
278
- T = Fixed{Int8,7 }
279
- for i = - 1.0 : 0.1 : typemax (T)
280
- @test i % T === T (i)
281
- end
282
- @test ( 1.5 % T). i == round (Int, 1.5 * 128 ) % Int8
283
- @test (- 0.3 % T). i == round (Int, - 0.3 * 128 ) % Int8
275
+ @test ( 1.5 % Q0f7). i == round (Int, 1.5 * 128 ) % Int8
276
+ @test (- 0.3 % Q0f7). i == round (Int, - 0.3 * 128 ) % Int8
284
277
285
- T = Fixed{Int16,9 }
286
- for i = - 64.0 : 0.1 : typemax (T)
287
- @test i % T === T (i)
288
- end
289
- @test ( 65.2 % T). i == round (Int, 65.2 * 512 ) % Int16
290
- @test (- 67.2 % T). i == round (Int, - 67.2 * 512 ) % Int16
278
+ @test ( 65.2 % Q6f9). i == round (Int, 65.2 * 512 ) % Int16
279
+ @test (- 67.2 % Q6f9). i == round (Int, - 67.2 * 512 ) % Int16
291
280
292
281
@test - 1 % Q0f7 === Q0f7 (- 1 )
293
282
@test - 2 % Q0f7 === Q0f7 (0 )
294
-
295
- # TODO : avoid undefined behavior
296
- @testset " nan % $F " for F in target (Fixed, :i8 , :i16 , :i32 , :i64 ; ex = :thin )
297
- @test NaN % F === NaN32 % F === NaN16 % F === zero (F)
298
- end
299
283
end
300
284
301
285
@testset " neg" begin
312
296
@test saturating_neg (eps (F)) === zero (F) - eps (F)
313
297
@test checked_neg (eps (F)) === zero (F) - eps (F)
314
298
end
315
- for F in target (Fixed, :i8 ; ex = :thin )
316
- xs = typemin (F): eps (F): typemax (F)
317
- fneg (x) = - float (x)
318
- @test all (x -> wrapping_neg (wrapping_neg (x)) === x, xs)
319
- @test all (x -> saturating_neg (x) === clamp (fneg (x), F), xs)
320
- @test all (x -> ! (typemin (F) <= fneg (x) <= typemax (F)) ||
321
- wrapping_neg (x) === checked_neg (x) === fneg (x) % F, xs)
322
- end
299
+ test_neg (Fixed)
323
300
end
324
301
325
302
@testset " abs" begin
332
309
@test saturating_abs (typemin (F)) === typemax (F)
333
310
@test_throws OverflowError checked_abs (typemin (F))
334
311
end
335
- for F in target (Fixed, :i8 ; ex = :thin )
336
- xs = typemin (F): eps (F): typemax (F)
337
- fabs (x) = abs (float (x))
338
- @test all (x -> wrapping_abs (x) === (x > 0 ? x : wrapping_neg (x)), xs)
339
- @test all (x -> saturating_abs (x) === clamp (fabs (x), F), xs)
340
- @test all (x -> ! (typemin (F) <= fabs (x) <= typemax (F)) ||
341
- wrapping_abs (x) === checked_abs (x) === fabs (x) % F, xs)
342
- end
312
+ test_abs (Fixed)
343
313
end
344
314
345
315
@testset " add" begin
357
327
@test saturating_add (zero (F), eps (F)) === saturating_add (eps (F), zero (F)) === eps (F)
358
328
@test checked_add (zero (F), eps (F)) === checked_add (eps (F), zero (F)) === eps (F)
359
329
end
360
- for F in target (Fixed, :i8 ; ex = :thin )
361
- xs = typemin (F): eps (F): typemax (F)
362
- xys = ((x, y) for x in xs, y in xs)
363
- fadd (x, y) = float (x) + float (y)
364
- @test all (((x, y),) -> wrapping_sub (wrapping_add (x, y), y) === x, xys)
365
- @test all (((x, y),) -> saturating_add (x, y) === clamp (fadd (x, y), F), xys)
366
- @test all (((x, y),) -> ! (typemin (F) <= fadd (x, y) <= typemax (F)) ||
367
- wrapping_add (x, y) === checked_add (x, y) === fadd (x, y) % F, xys)
368
- end
330
+ test_add (Fixed)
369
331
end
370
332
371
333
@testset " sub" begin
382
344
@test saturating_sub (eps (F), zero (F)) === eps (F)
383
345
@test checked_sub (eps (F), zero (F)) === eps (F)
384
346
end
385
- for F in target (Fixed, :i8 ; ex = :thin )
386
- xs = typemin (F): eps (F): typemax (F)
387
- xys = ((x, y) for x in xs, y in xs)
388
- fsub (x, y) = float (x) - float (y)
389
- @test all (((x, y),) -> wrapping_add (wrapping_sub (x, y), y) === x, xys)
390
- @test all (((x, y),) -> saturating_sub (x, y) === clamp (fsub (x, y), F), xys)
391
- @test all (((x, y),) -> ! (typemin (F) <= fsub (x, y) <= typemax (F)) ||
392
- wrapping_sub (x, y) === checked_sub (x, y) === fsub (x, y) % F, xys)
393
- end
347
+ test_sub (Fixed)
394
348
end
395
349
396
350
@testset " mul" begin
413
367
@test saturating_mul (typemin (F), typemin (F)) === typemax (F)
414
368
@test_throws OverflowError checked_mul (typemin (F), typemin (F))
415
369
end
416
- for F in target (Fixed, :i8 ; ex = :thin )
417
- xs = typemin (F): eps (F): typemax (F)
418
- xys = ((x, y) for x in xs, y in xs)
419
- fmul (x, y) = float (x) * float (y) # note that precision(Float32) < 32
420
- @test all (((x, y),) -> wrapping_mul (x, y) === fmul (x, y) % F, xys)
421
- @test all (((x, y),) -> saturating_mul (x, y) === clamp (fmul (x, y), F), xys)
422
- @test all (((x, y),) -> ! (typemin (F) <= fmul (x, y) <= typemax (F)) ||
423
- wrapping_mul (x, y) === checked_mul (x, y), xys)
424
- end
370
+ test_mul (Fixed)
425
371
426
372
FixedPointNumbers. mul_with_rounding (1.5 Q6f1, 0.5 Q6f1, RoundNearest) === 1.0 Q6f1
427
373
FixedPointNumbers. mul_with_rounding (1.5 Q6f1, - 0.5 Q6f1, RoundNearest) === - 1.0 Q6f1
482
428
end
483
429
484
430
@testset " approx" begin
485
- @testset " approx $F " for F in target (Fixed, :i8 , :i16 ; ex = :light )
486
- xs = typemin (F): eps (F): typemax (F)- eps (F)
487
- @test all (x -> x ≈ x + eps (F), xs)
488
- @test all (x -> x + eps (F) ≈ x, xs)
489
- @test ! any (x -> x - eps (F) ≈ x + eps (F), xs)
490
- end
431
+ test_isapprox (Fixed)
491
432
492
433
@test isapprox (- 0.5 Q0f7, - 1 Q0f7, rtol= 0.5 , atol= 0 ) # issue 209
493
434
@test isapprox (typemin (Q0f7), typemax (Q0f7), rtol= 2.0 )
511
452
@test clamp (- 1.5f0 , Q0f7) === - 1.0 Q0f7
512
453
@test clamp (1.5 Q1f6, Q0f7) === 0.992 Q0f7
513
454
514
- @testset " clamp(nan, $F )" for F in target (Fixed; ex = :thin )
515
- @test clamp ( Inf , F) === clamp ( Inf32 , F) === typemax (F)
516
- @test clamp (- Inf , F) === clamp (- Inf32 , F) === typemin (F)
517
- @test clamp ( NaN , F) === clamp ( NaN32 , F) === zero (F)
518
- end
455
+ test_clamp_nan (Fixed)
519
456
end
520
457
521
458
@testset " sign-related functions" begin
546
483
@test ! isinf (1 Q7f8)
547
484
548
485
@testset " isinteger" begin
549
- @testset " isinteger(::$F )" for F in target (Fixed, :i8 , :i16 )
550
- xs = typemin (F): eps (F): typemax (F)
551
- @test all (x -> isinteger (x) == isinteger (float (x)), xs)
552
- end
486
+ test_isinteger (Fixed)
553
487
@testset " isinteger(::$F )" for F in target (Fixed, :i32 , :i64 , :i128 )
554
488
fzero, fmax, fmin = zero (F), typemax (F), typemin (F)
555
489
if nbitsfrac (F) == 0
619
553
end
620
554
621
555
@testset " rand" begin
622
- @testset " rand(::$F )" for F in target (Fixed; ex = :thin )
623
- @test isa (rand (F), F)
624
- a = rand (F, (3 , 5 ))
625
- @test ndims (a) == 2 && eltype (a) === F
626
- @test size (a) == (3 ,5 )
627
- end
556
+ test_rand (Fixed)
628
557
@test rand (MersenneTwister (1234 ), Q0f7) === - 0.156 Q0f7
629
558
end
630
559
@@ -695,11 +624,11 @@ end
695
624
@test String (take! (iob)) == " -21845.33334Q15f16"
696
625
697
626
show (iob, Fixed {Int128,64} (- 1.2345e6 ))
698
- @test String (take! (iob)) == " Fixed{Int128,64}(-1.2345e6)"
627
+ @test String (take! (iob)) == " Fixed{Int128,$(SP) 64}(-1.2345e6)"
699
628
700
629
# TODO : remove this test
701
630
show (iob, reinterpret (Fixed{Int8,8 }, signed (0xaa )))
702
- @test String (take! (iob)) == " Fixed{Int8,8}(-0.336)"
631
+ @test String (take! (iob)) == " Fixed{Int8,$(SP) 8}(-0.336)"
703
632
end
704
633
705
634
@testset " summary" begin
0 commit comments