1
1
using FixedPointDecimals
2
+ import FixedPointDecimals: FD
2
3
using Base. Test
3
4
using Compat
4
5
5
6
const SFD2 = FixedDecimal{Int16, 2 }
6
7
const SFD4 = FixedDecimal{Int16, 4 }
7
8
const FD1 = FixedDecimal{Int, 1 }
8
9
const FD2 = FixedDecimal{Int, 2 }
10
+ const FD3 = FixedDecimal{Int, 3 }
9
11
const FD4 = FixedDecimal{Int, 4 }
10
12
const WFD2 = FixedDecimal{Int128, 2 }
11
13
const WFD4 = FixedDecimal{Int128, 4 }
@@ -37,12 +39,22 @@ const keyvalues = Dict(
37
39
reinterpret (WFD4, 164435910993133062409572187012743929911 ),
38
40
typemax (WFD4)])
39
41
42
+ # Floating point values written as integer strings. Useful for testing behaviours of
43
+ # trunc, floor, and ceil.
44
+ const INT_2_2 = " 22000000000000001776356839400250464677" # 2.2
45
+ const INT_2_3 = " 22999999999999998223643160599749535322" # 2.3
46
+
47
+
40
48
# numbers that may cause overflow
41
49
islarge (x) = x == typemin (x) || abs (x) > 1000
42
50
43
51
# numbers that can never cause overflow
44
52
issmall (x) = - 1 < x ≤ 1
45
53
54
+ function parse_int {T, f} (:: Type{FD{T, f}} , val:: AbstractString ; ceil:: Bool = false )
55
+ reinterpret (FD{T, f}, parse (T, val[1 : (f + 1 )]) + T (ceil))
56
+ end
57
+
46
58
@testset " conversion" begin
47
59
@testset for x in keyvalues[FD2]
48
60
@testset for T in [Rational{Int128}, WFD2, WFD4]
285
297
@test isinteger (Base. widemul (10 , FD2 (trunc (FD1, x))))
286
298
@test abs (FD2 (trunc (FD1, x))) ≥ 0
287
299
end
300
+
301
+ @testset " truncate precision" begin
302
+ @test trunc (FD2, 2.3 ) != trunc (FD3, 2.3 )
303
+ @test trunc (FD2, 2.3 ) == FD2 (2.29 )
304
+ @test trunc (FD3, 2.3 ) == FD3 (2.299 )
305
+
306
+ for f in 0 : 12
307
+ trunc (FD{Int64, f}, 2.3 ) == parse_int (FD{Int64, f}, INT_2_3)
308
+ end
309
+ end
288
310
end
289
311
290
312
# eps that works for integers too
@@ -303,6 +325,24 @@ epsi{T}(::Type{T}) = eps(T)
303
325
@test ceil (T, x) - epsi (T) < x ≤ ceil (T, x)
304
326
end
305
327
end
328
+
329
+ @testset " floor, ceil precision" begin
330
+ @test floor (FD2, 2.3 ) != floor (FD3, 2.3 )
331
+ @test floor (FD2, 2.3 ) == FD2 (2.29 )
332
+ @test floor (FD3, 2.3 ) == FD3 (2.299 )
333
+
334
+ for f in 0 : 12
335
+ floor (FD{Int64, f}, 2.3 ) == parse_int (FD{Int64, f}, INT_2_3)
336
+ end
337
+
338
+ @test ceil (FD2, 2.2 ) != ceil (FD3, 2.2 )
339
+ @test ceil (FD2, 2.2 ) == FD2 (2.21 )
340
+ @test ceil (FD3, 2.2 ) == FD3 (2.201 )
341
+
342
+ for f in 0 : 12
343
+ ceil (FD{Int64, f}, 2.2 ) == parse_int (FD{Int64, f}, INT_2_2, ceil= true )
344
+ end
345
+ end
306
346
end
307
347
308
348
@testset " show" begin
0 commit comments