Using Python 2.7 and -1/2 will be result in -1, since -1 is floor of -0.5. But I want to get result of 0, which is 1/2 (result in 0), then negative 1/2 (final result still 0) Wondering if there is an elegant way to do this?
My current solution is using abs and check their sign first, which seems not very elegant, wondering if anyone have more elegant solutions?
Source code in Python 2.7,
a = -1
b = 2
print a/b # output -1
# prefer -(1/2) = 0
r = abs(a)/abs(b)
if (a > 0 and b < 0) or (a < 0 and b > 0):
r = -r
print r