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;
}
""")
""",
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;
}
""")
>>> [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