@@ -4,6 +4,8 @@ using Base.Test
44using Compat
55import Base. Checked: checked_mul
66
7+ include (" utils.jl" )
8+
79@testset " FixedPointDecimals" begin
810
911const SFD2 = FixedDecimal{Int16, 2 }
@@ -77,6 +79,29 @@ if VERSION < v"0.6.0-dev.1849"
7779 Base.:/ (x:: UInt128 , y:: BigInt ) = / (promote (x, y)... )
7880end
7981
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+
80105@testset " max_exp10" begin
81106 @test FixedPointDecimals. max_exp10 (Int8) == 2
82107 @test FixedPointDecimals. max_exp10 (Int64) == 18
@@ -629,18 +654,13 @@ end
629654 f = FixedPointDecimals. max_exp10 (T)
630655 powt = FixedPointDecimals. coefficient (FD{T,f})
631656
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})))
638661
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)
644664
645665 @test trunc (reinterpret (FD{T,f}, typemax (T))) == FD {T,f} (div (typemax (T), powt))
646666 @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)
694714 f = FixedPointDecimals. max_exp10 (T)
695715 powt = FixedPointDecimals. coefficient (FD{T,f})
696716
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})))
706721
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)
711724
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)
714727
715728 # Note: rounding away from zero will result in an exception.
716729 max_int = typemax (T)
0 commit comments