From: Robert Bradshaw Date: Sun, 29 Mar 2009 01:02:05 +0000 (-0700) Subject: Test (and fix) corner case in C division X-Git-Tag: 0.12.alpha0~334^2~2^2~1 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=0e658851b21b988eb1d34699e368a2b9dc9daed6;p=cython.git Test (and fix) corner case in C division --- diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 89c5281e..b3c2503c 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -5717,7 +5717,7 @@ static INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s, %(type)s); /* proto */ impl=""" static INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s a, %(type)s b) { %(type)s res = fmod%(math_h_modifier)s(a, b); - res += ((res < 0) ^ (b < 0)) * b; + res += ((res != 0) & ((a < 0) ^ (b < 0))) * b; return res; } """) @@ -5728,9 +5728,9 @@ static INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s, %(type)s); /* proto */ """, impl=""" static INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s a, %(type)s b) { - %(type)s res = a / b; - res -= (res < 0); - return res; + %(type)s q = a / b; + q -= ((q*b != a) & ((a < 0) ^ (b < 0))); + return q; } """) diff --git a/tests/run/cdivision_CEP_516.pyx b/tests/run/cdivision_CEP_516.pyx index 29269001..51f9eba1 100644 --- a/tests/run/cdivision_CEP_516.pyx +++ b/tests/run/cdivision_CEP_516.pyx @@ -27,6 +27,10 @@ True >>> [test_cdiv_cmod(a, b) for a, b in v] [(1, 7), (-1, -7), (1, -7), (-1, 7)] +>>> all([mod_int_py(a,b) == a % b for a in range(-10, 10) for b in range(-10, 10) if b != 0]) +True +>>> all([div_int_py(a,b) == a // b for a in range(-10, 10) for b in range(-10, 10) if b != 0]) +True >>> def simple_warn(msg, *args): print msg >>> import warnings