更改非正数除法的 python 数学模块行为

发布于 2024-11-08 13:24:12 字数 510 浏览 7 评论 0原文

非正数除法在 C++ 和 Python 编程语言中非常不同:

//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

因此,如您所见,C++ 正在最小化商。 然而,python 的行为是这样的:

#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

我无法编写自己的除法函数,其行为类似于 c++,因为我将使用它来检查 c++ 计算器程序,并且 python 不支持中缀运算符。我可以让 python 的行为像 c++ 一样,同时以简单的方式除以整数吗?例如,设置一些标志或类似的东西?

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 my 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦初醒的夏天 2024-11-15 13:24:12

正如 Thomas K 所说,使用 math.fmod 进行取模,或者如果您确实想要,您可以自己定义它:

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

并且此函数应该模拟 C 风格的除法:

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

您说过您必须 使用 /% 运算符。这是不可能的,因为您无法覆盖内置运算符。不过,您可以定义自己的整数类型和运算符重载 __div____mod__ 运算符。

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.

浴红衣 2024-11-15 13:24:12

没有可以设置的标志来使 python 除法像 c++ 一样。

您建议您不能编写自己的除法函数,但如果您改变主意,您可以这样做:

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

这表明它按照您的规范行事:

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)

这给出了:

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

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
愿得七秒忆 2024-11-15 13:24:12

您还应该查看标准库中的 decimal 模块。

十进制“基于浮点模型,该模型的设计以人为本,并且必然有一个最重要的指导原则 - 计算机必须提供一种与人们在学校学习的算术方式相同的算术。” – 摘自十进制算术规范。

然而,结果

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文