From 0a29c05a1b4523dbaa0b1ed7c368df35de0289a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Thu, 10 Oct 2019 14:35:26 +0200 Subject: [PATCH] fix #12332 (#12402) [backport] --- compiler/int128.nim | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler/int128.nim b/compiler/int128.nim index ce8f574bd2..44f77b8e83 100644 --- a/compiler/int128.nim +++ b/compiler/int128.nim @@ -436,7 +436,7 @@ proc divMod*(dividend, divisor: Int128): tuple[quotient, remainder: Int128] = result.quotient = -quotient else: result.quotient = quotient - if isNegativeB: + if isNegativeA: result.remainder = -dividend else: result.remainder = dividend @@ -676,3 +676,12 @@ when isMainModule: doAssert $high(Int128) == "170141183460469231731687303715884105727" doAssert $low(Int128) == "-170141183460469231731687303715884105728" + + var ma = 100'i64 + var mb = 13 + + # sign correctness + doAssert divMod(toInt128( ma),toInt128( mb)) == (toInt128( ma div mb), toInt128( ma mod mb)) + doAssert divMod(toInt128(-ma),toInt128( mb)) == (toInt128(-ma div mb), toInt128(-ma mod mb)) + doAssert divMod(toInt128( ma),toInt128(-mb)) == (toInt128( ma div -mb), toInt128( ma mod -mb)) + doAssert divMod(toInt128(-ma),toInt128(-mb)) == (toInt128(-ma div -mb), toInt128(-ma mod -mb))