From 3cfe5c74d53ba665e969430baa098273913f1263 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 8 Oct 2010 10:58:08 +0100 Subject: [PATCH] fix div to round correctly (Ulrich's #181). --- C/arith2.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/C/arith2.c b/C/arith2.c index 3e84b5a7a..6427dd2d1 100755 --- a/C/arith2.c +++ b/C/arith2.c @@ -111,7 +111,7 @@ p_div2(Term t1, Term t2) { { Int i1 = IntegerOfTerm(t1); Int i2 = IntegerOfTerm(t2); - Int res; + Int res, mod; if (i2 == 0) goto zero_divisor; if (i1 == Int_MIN && i2 == -1) { @@ -122,7 +122,10 @@ p_div2(Term t1, Term t2) { "// /2 with %d and %d", i1, i2); #endif } - res = (i1 - i1%i2) / i2; + mod = i1%i2; + if (mod && (mod ^ i2) < 0) + mod += i2; + res = (i1 - mod) / i2; RINT(res); } case (CELL)double_e: