Skip to content

Commit 4856c57

Browse files
committed
Fix convert from Integer at the input limits
1 parent 18be9a7 commit 4856c57

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/FixedPointDecimals.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ function round{T, f}(::Type{FD{T, f}}, x::Rational,
204204
end
205205

206206
# conversions and promotions
207-
convert{T, f}(::Type{FD{T, f}}, x::Integer) =
208-
reinterpret(FD{T, f}, round(T, Base.widemul(T(x), T(10)^f)))
207+
function convert{T, f}(::Type{FD{T, f}}, x::Integer)
208+
reinterpret(FD{T, f}, T(widemul(x, coefficient(FD{T, f}))))
209+
end
209210

210211
convert{T <: FD}(::Type{T}, x::AbstractFloat) = round(T, x)
211212

test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ end
109109
@test convert(FD{$T,$f}, $(max_int / powt)).i == $max_int
110110
@test $min_int >= typemin($T)
111111
@test convert(FD{$T,$f}, $(min_int / powt)).i == $min_int
112+
113+
@test_throws InexactError convert(FD{$T,$f}, $T(1))
114+
end
115+
end
116+
117+
for T in CONTAINER_TYPES
118+
f = FixedPointDecimals.max_exp10(T)
119+
powt = FixedPointDecimals.coefficient(FD{T,f})
120+
121+
max_int = typemax(T) ÷ powt * powt
122+
min_int = typemin(T) ÷ powt * powt
123+
124+
@eval begin
125+
@test convert(FD{$T,$f}, $(max_int ÷ powt)) == reinterpret(FD{$T,$f}, $max_int)
126+
@test convert(FD{$T,$f}, $(min_int ÷ powt)) == reinterpret(FD{$T,$f}, $min_int)
127+
128+
@test_throws InexactError convert(FD{$T,$f}, $(max_int ÷ powt) + $T(1))
129+
@test_throws InexactError convert(FD{$T,$f}, $(min_int ÷ powt) - $T(1)) # Overflows with Unsigned
112130
end
113131
end
114132
end

0 commit comments

Comments
 (0)