为什么 Python 会错误地计算这个表达式?

发布于 2025-01-06 17:00:02 字数 269 浏览 1 评论 0原文

我一直在尝试 Python 的数学能力,并发现了一些有趣的行为。它与以下表达式相关:

(4+4)+3/4/5*35-(3*(5+7))-6+434+5+5+5

>>> 415

但是,如果您按照标准运算顺序计算表达式,则答案应该是 420.25。我还用 WolframAlpha 进行了仔细检查,其给出的答案为 420.25。为什么Python给出了不同的答案?它与它如何评估此类表达式有关吗?它遵循一些约定吗?任何信息将不胜感激,谢谢!

I've been experimenting with the mathematical abilities of Python and I came upon some interesting behavior. It's related to the following expression:

(4+4)+3/4/5*35-(3*(5+7))-6+434+5+5+5

>>> 415

However, if you evaluate the expression with the standard order of operations in mind, the answer should be 420.25. I've also double checked with WolframAlpha, which gives an answer of 420.25. Why is Python giving a different answer? Does it have something to do with how it evaluates such expressions? Is there some convention that its following? Any info would be greatly appreciated, thanks!

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

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

发布评论

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

评论(7

幻梦 2025-01-13 17:00:02

您想使用浮点除法。将其更改为有效:

(4+4)+3.0/4/5*35-(3*(5+7))-6+434+5+5+5

整数除法与浮点除法的一些示例:

Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
>>> 3/4
0
>>> 3.0/4
0.75
>>> 3.0/4.0
0.75

浮点数除以整数是浮点运算。 Python 3 对此进行了更改,因此浮点除法成为默认值:

Python 3.2.2 (default, Sep  5 2011, 21:17:14) 
>>> 3/4
0.75

You want to use floating point division. Changing it to this works:

(4+4)+3.0/4/5*35-(3*(5+7))-6+434+5+5+5

Some examples of integer division vs. floating point division:

Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
>>> 3/4
0
>>> 3.0/4
0.75
>>> 3.0/4.0
0.75

A float divided by an integer is a floating-point operation. Python 3 has changed this so that floating-point division is the default:

Python 3.2.2 (default, Sep  5 2011, 21:17:14) 
>>> 3/4
0.75
情泪▽动烟 2025-01-13 17:00:02

因为,在Python 2中,当运算符和操作数都是整数时,/使用整数除法。

您可以将其更改为浮点数(正如其他回答者所建议的那样),或者使用 from __future__ import divisionhttp://www.python.org/dev/peps/pep-0238/

Because, in Python 2, / uses integer division when both the operator and operand are integers.

You can either change one to a float (as other answerers have suggested), or use from __future__ import division: http://www.python.org/dev/peps/pep-0238/

生生漫 2025-01-13 17:00:02

在 Python 2.x 中 / 运算符是整数除法。如果你写

(4+4)+3.0/4.0/5.0*35-(3*(5+7))-6+434+5+5+5

它会给出预期的答案。

In Python 2.x the / operator is integer division. If you write

(4+4)+3.0/4.0/5.0*35-(3*(5+7))-6+434+5+5+5

it will give the expected answer.

喵星人汪星人 2025-01-13 17:00:02

这取决于您运行的 Python 版本:

$ python3
Python 3.1.4 (default, Nov 12 2011, 12:16:31) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> (4+4)+3/4/5*35-(3*(5+7))-6+434+5+5+5
420.25

在 Python 3 之前,/ 运算符执行整数除法而不是浮点运算。

That depends on the version of Python you're running:

$ python3
Python 3.1.4 (default, Nov 12 2011, 12:16:31) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> (4+4)+3/4/5*35-(3*(5+7))-6+434+5+5+5
420.25

Before Python 3, the / operator performed integer division instead of floating-point.

一影成城 2025-01-13 17:00:02

这是整数除法。在您的示例中,3/4 的计算结果为 0。但是,3.0/4.0 的计算结果为 0.75。

>>> (4+4)+3/4/5*35-(3*(5+7))-6+434+5+5+5
415
>>> (4+4)+3./4/5*35-(3*(5+7))-6+434+5+5+5
420.25

此行为在 Python 版本 3 及更高版本中已更改。如果您希望默认为浮点除法,可以导入该规则。

>>> from __future__ import division
>>> (4+4)+3/4/5*35-(3*(5+7))-6+434+5+5+5
420.25

It's integer division. In your example, 3/4 evaluates to 0. However, 3.0/4.0 evaluates to 0.75.

>>> (4+4)+3/4/5*35-(3*(5+7))-6+434+5+5+5
415
>>> (4+4)+3./4/5*35-(3*(5+7))-6+434+5+5+5
420.25

This behavior was changed in Python versions 3 and greater. If you want it to default to floating point division, you can import the rule.

>>> from __future__ import division
>>> (4+4)+3/4/5*35-(3*(5+7))-6+434+5+5+5
420.25
南…巷孤猫 2025-01-13 17:00:02

值得注意的是,Python 或 WolframAlpha 都不会给出数学上正确的答案,因为在数学中我们在除法之前进行乘法,并且在 Python 中 * 和 / 具有相同的优先级,因此将从左到右求值。

3.0/4/5*35 # equal 5.25 in python
3.0/4/5*35 # equal 3.0/4/(5*35) or 0.004285714285714286 in math

its important to notice that neither python or WolframAlpha will not give you the mathematically right answer since in math we do multiplication before division and in python * and / have the same precedence so will evaluate left to right.

3.0/4/5*35 # equal 5.25 in python
3.0/4/5*35 # equal 3.0/4/(5*35) or 0.004285714285714286 in math
无悔心 2025-01-13 17:00:02

(4+4)+3/4/5*35-(3*(5+7))-6+434+5+5+5 在我的Python中是420.25
但 0.1*0.1 是 0.010000000000000002

(4+4)+3/4/5*35-(3*(5+7))-6+434+5+5+5 is 420.25 in my python
but 0.1*0.1 is 0.010000000000000002

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