开发者

Changing python math module behaviour for non-positive numbers division

开发者 https://www.devze.com 2023-03-07 06:51 出处:网络
Non-positive number division is quite different in c++ and python programming langugages: //c++: 11 / 3 = 3

Non-positive number division is quite different in c++ and python programming langugages:

//c++:
11 / 3 = 3
11 % 3 = 2
(-11) / 3 = -3
(-11) % 3 = -2
11 / (-3) = -3
11 % (-3) = 2
(-11) / (-3) = 3
(-11) % (-3) = -2

So, as you can see, c++ is minimizing quotient. However, python behaves like that:

#python
11 / 3 = 3
11 % 3 = 2
(-11) / 3 = -4
(-11) % 3 = 1
11 / (-3) = -4
11 % (-3) = -1
(-11) / (-3) = 3
(-11) % (-3) = -2

I can't code 开发者_StackOverflowmy own division function behaving like c++, because I'll use it for checking c++ calculator programs, and python does not support infix operators. Can I make python behaving like c++ while dividing integers in a simple way? For example, setting some flag or something like that?


As Thomas K said, use math.fmod for modulo, or if you really want you can define it yourself:

def cmod(x, y):
    return abs(x) % abs(y) * (1 if x > 0 else -1)

And this function should emulate C-style division:

def cdiv(x, y):
    return abs(x) / abs(y) * cmp(x, 0) * cmp(y, 0)

You said that you must use the / and % operators. This is not possible, since you can't override the operator for built-ins. You can however define your own integer type and operator overload the __div__ and __mod__ operators.


There is no flag you can set to make python division to act like c++.

You advised that you can't code your own division function, but if you change your mind you can do this:

def cpp_int_div(dividend, divisor):
    a, b = dividend, divisor
    sign = 1 if (a>0 and b>0) or (a<0 and b<0) else -1
    return (abs(a)/abs(b)) * sign

def cpp_int_mod(dividend, divisor): # or just use math.fmod  (from Thomas K)
    a, b = dividend, divisor
    sign = 1 if a>0 else -1
    return (abs(a)%abs(b)) * sign

This shows that it acts according to your specification:

print "11 / 3 = %d" % cpp_int_div(11,3)
print "11 %% 3 = %d" % cpp_int_mod(11,3)
print "(-11) / 3 = %d" % cpp_int_div(-11, 3)
print "(-11) %% 3 = %d" % cpp_int_mod(-11, 3)
print "11 / (-3) = %d" % cpp_int_div(11, -3)
print "11 %% (-3) = %d" % cpp_int_mod(11, -3)
print "(-11) / (-3) = %d" % cpp_int_div(-11, -3)
print "(-11) %% (-3) = %d" % cpp_int_mod(-11, -3)

Which gives:

11 / 3 = 3
11 % 3 = 2
(-11) / 3 = -3
(-11) % 3 = -2
11 / (-3) = -3
11 % (-3) = 2
(-11) / (-3) = 3
(-11) % (-3) = -2


You should also check out the decimal module from standard library.

Decimal “is based on a floating-point model which was designed with people in mind, and necessarily has a paramount guiding principle – computers must provide an arithmetic that works in the same way as the arithmetic that people learn at school.” – excerpt from the decimal arithmetic specification.

Yet, the result of

import decimal
decimal.divmod(-11, 3)
>>> (-4, 1)
0

精彩评论

暂无评论...
验证码 换一张
取 消