gpt4 book ai didi

math - 术语 "BODMAS"是什么意思?

转载 作者:行者123 更新时间:2023-12-03 14:33:04 25 4
gpt4 key购买 nike

什么是 BODMAS,为什么它在编程中很有用?

最佳答案

http://www.easymaths.com/What_on_earth_is_Bodmas.htm :

What do you think the answer to 2 + 3 x 5 is?

Is it (2 + 3) x 5 = 5 x 5 = 25 ?

or 2 + (3 x 5) = 2 + 15 = 17 ?

BODMAS can come to the rescue and give us rules to follow so that we always get the right answer:

(B)rackets (O)rder (D)ivision (M)ultiplication (A)ddition (S)ubtraction

According to BODMAS, multiplication should always be done before addition, therefore 17 is actually the correct answer according to BODMAS and will also be the answer which your calculator will give if you type in 2 + 3 x 5 .



为什么它在编程中很有用?不知道,但我想是因为你可以去掉一些括号?我是一个非常防御性的程序员,所以我的台词可以是这样的:
result = (((i + 4) - (a + b)) * MAGIC_NUMBER) - ANOTHER_MAGIC_NUMBER;

使用 BODMAS,您可以更清楚地了解这一点:
result = (i + 4 - (a + b)) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;

我想我仍然会使用第一个变体——更多的括号,但这样我就不必学习另一个规则,而且我忘记它并导致那些奇怪的难以调试的错误的风险更小?

只是猜测那部分。

Mike Stone 编辑:修复了 Gaius 指出的数学问题

关于math - 术语 "BODMAS"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com