Skip to content

Commit cc62fc0

Browse files
committed
Fix multiply at the input limits
1 parent d15e2dc commit cc62fc0

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/FixedPointDecimals.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ end
128128
# TODO: can we use floating point to speed this up? after we build a
129129
# correctness test suite.
130130
function *{T, f}(x::FD{T, f}, y::FD{T, f})
131-
powt = T(10)^f
132-
quotient, remainder = fldmod(Base.widemul(x.i, y.i), powt)
131+
powt = coefficient(FD{T,f})
132+
quotient, remainder = fldmod(widemul(x.i, y.i), powt)
133133
reinterpret(FD{T, f}, _round_to_even(quotient, remainder, powt))
134134
end
135135

test/runtests.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,27 @@ end
229229
@test 20 * FD{Int8,1}(0.1) == FD{Int8,1}(2.0)
230230
@test FD{Int8,1}(0.1) * 20 == FD{Int8,1}(2.0)
231231
end
232+
233+
@testset "limits" begin
234+
for T in CONTAINER_TYPES
235+
x = FixedPointDecimals.max_exp10(T)
236+
f = x + 1
237+
238+
@eval begin
239+
scalar = reinterpret(FD{$T,$f}, $T(10)^$x) # 0.1
240+
241+
# Since multiply will round the result we'll make sure our value does not
242+
# always rounds down.
243+
max_int = typemax($T) - (typemax($T) % 10)
244+
min_int = typemin($T) - (typemin($T) % 10)
245+
246+
@test reinterpret(FD{$T,$f}, max_int) * scalar ==
247+
reinterpret(FD{$T,$f}, div(max_int, 10))
248+
@test reinterpret(FD{$T,$f}, min_int) * scalar ==
249+
reinterpret(FD{$T,$f}, div(min_int, 10))
250+
end
251+
end
252+
end
232253
end
233254

234255
@testset "division" begin

0 commit comments

Comments
 (0)