@@ -4,6 +4,8 @@ using Base.Test
4
4
using Compat
5
5
import Base. Checked: checked_mul
6
6
7
+ include (" utils.jl" )
8
+
7
9
@testset " FixedPointDecimals" begin
8
10
9
11
const SFD2 = FixedDecimal{Int16, 2 }
@@ -77,6 +79,29 @@ if VERSION < v"0.6.0-dev.1849"
77
79
Base.:/ (x:: UInt128 , y:: BigInt ) = / (promote (x, y)... )
78
80
end
79
81
82
+ # Basic tests for the methods created above
83
+ @testset " alt" begin
84
+ @test trunc_alt (FD2, 0.0 ) == FD2 (0 )
85
+ @test floor_alt (FD2, 0.0 ) == FD2 (0 )
86
+ @test ceil_alt (FD2, 0.0 ) == FD2 (0 )
87
+
88
+ @test trunc_alt (FD2, 2.149 ) == FD2 (2.14 )
89
+ @test floor_alt (FD2, 2.149 ) == FD2 (2.14 )
90
+ @test ceil_alt (FD2, 2.149 ) == FD2 (2.15 )
91
+
92
+ @test trunc_alt (FD2, - 2.149 ) == FD2 (- 2.14 )
93
+ @test floor_alt (FD2, - 2.149 ) == FD2 (- 2.15 )
94
+ @test ceil_alt (FD2, - 2.149 ) == FD2 (- 2.14 )
95
+
96
+ @test trunc_alt (FD2, nextfloat (0.0 )) == FD2 (0 )
97
+ @test floor_alt (FD2, nextfloat (0.0 )) == FD2 (0 )
98
+ @test ceil_alt (FD2, nextfloat (0.0 )) == FD2 (0.01 )
99
+
100
+ @test trunc_alt (FD2, prevfloat (0.0 )) == FD2 (0 )
101
+ @test floor_alt (FD2, prevfloat (0.0 )) == FD2 (- 0.01 )
102
+ @test ceil_alt (FD2, prevfloat (0.0 )) == FD2 (0 )
103
+ end
104
+
80
105
@testset " max_exp10" begin
81
106
@test FixedPointDecimals. max_exp10 (Int8) == 2
82
107
@test FixedPointDecimals. max_exp10 (Int64) == 18
@@ -629,18 +654,13 @@ end
629
654
f = FixedPointDecimals. max_exp10 (T)
630
655
powt = FixedPointDecimals. coefficient (FD{T,f})
631
656
632
- # Ideally we would just use `typemax(T)` but due to precision issues with
633
- # floating-point its possible the closest float will exceed `typemax(T)`.
634
- # Additionally, when the division results in a `BigFloat` we need to first
635
- # truncate to a `BigInt` before we can truncate the type we want.
636
- max_int = T (trunc (BigInt, prevfloat (typemax (T) / powt) * powt))
637
- min_int = T (trunc (BigInt, nextfloat (typemin (T) / powt) * powt))
657
+ # When converting from typemax to a floating-point it is possible that due to
658
+ # precision issues that the closest possible float will exceed the typemax.
659
+ max_float = prevfloat (convert (AbstractFloat, typemax (FD{T,f})))
660
+ min_float = nextfloat (convert (AbstractFloat, typemin (FD{T,f})))
638
661
639
- # floating-point inprecision makes it hard to know exactly that value to
640
- # expect. Since we're primarily looking for issues relating to overflow this
641
- # we can have the expected result be a little flexible.
642
- @test value (trunc (FD{T,f}, max_int / powt)) in [max_int, max_int - 1 ]
643
- @test value (trunc (FD{T,f}, min_int / powt)) in [min_int, min_int + 1 ]
662
+ @test trunc (FD{T,f}, max_float) == trunc_alt (FD{T,f}, max_float)
663
+ @test trunc (FD{T,f}, min_float) == trunc_alt (FD{T,f}, min_float)
644
664
645
665
@test trunc (reinterpret (FD{T,f}, typemax (T))) == FD {T,f} (div (typemax (T), powt))
646
666
@test trunc (reinterpret (FD{T,f}, typemin (T))) == FD {T,f} (div (typemin (T), powt))
@@ -694,23 +714,16 @@ epsi{T}(::Type{T}) = eps(T)
694
714
f = FixedPointDecimals. max_exp10 (T)
695
715
powt = FixedPointDecimals. coefficient (FD{T,f})
696
716
697
- # Ideally we would just use `typemax(T)` but due to precision issues with
698
- # floating-point its possible the closest float will exceed `typemax(T)`.
699
- # Additionally, when the division results in a `BigFloat` we need to first
700
- # truncate to a `BigInt` before we can truncate the type we want.
701
- max_int = T (trunc (BigInt, prevfloat (typemax (T) / powt) * powt))
702
- min_int = T (trunc (BigInt, nextfloat (typemin (T) / powt) * powt))
703
-
704
- max_dec = max_int / powt
705
- min_dec = min_int / powt
717
+ # When converting from typemax to a floating-point it is possible that due to
718
+ # precision issues that the closest possible float will exceed the typemax.
719
+ max_float = prevfloat (convert (AbstractFloat, typemax (FD{T,f})))
720
+ min_float = nextfloat (convert (AbstractFloat, typemin (FD{T,f})))
706
721
707
- # Note: Using a larger signed type as the max/min values may be at the
708
- # limits and overflow when adding or subtracting 1.
709
- @test value (floor (FD{T,f}, max_dec)) in [max_int, max_int - 1 ]
710
- @test value (floor (FD{T,f}, min_dec)) in [min_int, signed (widen (min_int)) - 1 ]
722
+ @test floor (FD{T,f}, max_float) == floor_alt (FD{T,f}, max_float)
723
+ @test floor (FD{T,f}, min_float) == floor_alt (FD{T,f}, min_float)
711
724
712
- @test value ( ceil (FD{T,f}, max_dec)) in [max_int, signed ( widen (max_int)) + 1 ]
713
- @test value ( ceil (FD{T,f}, min_dec)) in [min_int, min_int + 1 ]
725
+ @test ceil (FD{T,f}, max_float) == ceil_alt (FD{T,f}, max_float)
726
+ @test ceil (FD{T,f}, min_float) == ceil_alt (FD{T,f}, min_float)
714
727
715
728
# Note: rounding away from zero will result in an exception.
716
729
max_int = typemax (T)
0 commit comments