diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 89c5281e8f0de66bfaa9c832042543de6ebdf370..b3c2503c55befd8eecbf1c02544a5bfe4ccc3f13 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 292690015d7fde169ec67e879906618aa1eec274..51f9eba16f9ff7f6629d3f2769325db00a9a467a 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