Commit c5f2231e authored by Zackery Spytz's avatar Zackery Spytz Committed by GitHub

Fix the cdiv() and cmod() definitions in Shadow.py (GH-3429)

Closes #2643.
parent bd990e4a
...@@ -146,14 +146,16 @@ def compile(f): ...@@ -146,14 +146,16 @@ def compile(f):
# Special functions # Special functions
def cdiv(a, b): def cdiv(a, b):
q = a / b if a < 0:
if q < 0: a = -a
q += 1 b = -b
return q if b < 0:
return (a + b + 1) // b
return a // b
def cmod(a, b): def cmod(a, b):
r = a % b r = a % b
if (a*b) < 0: if (a * b) < 0 and r:
r -= b r -= b
return r return r
......
...@@ -27,6 +27,9 @@ True ...@@ -27,6 +27,9 @@ True
>>> [test_cdiv_cmod(a, b) for a, b in v] >>> [test_cdiv_cmod(a, b) for a, b in v]
[(1, 7), (-1, -7), (1, -7), (-1, 7)] [(1, 7), (-1, -7), (1, -7), (-1, 7)]
>>> [test_cdiv_cmod(a, b) for a, b in [(4, -4), (4, -2), (4, -1)]]
[(-1, 0), (-2, 0), (-4, 0)]
>>> all([mod_int_py(a,b) == a % b for a in range(-10, 10) for b in range(-10, 10) if b != 0]) >>> all([mod_int_py(a,b) == a % b for a in range(-10, 10) for b in range(-10, 10) if b != 0])
True True
>>> all([div_int_py(a,b) == a // b for a in range(-10, 10) for b in range(-10, 10) if b != 0]) >>> all([div_int_py(a,b) == a // b for a in range(-10, 10) for b in range(-10, 10) if b != 0])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment