计算输出值错误

发布于 2024-12-11 08:50:09 字数 192 浏览 0 评论 0 原文

以下代码输出“3”。我期待“1”。

echo $resultado."\n"; // show 2
$valor = $resultado * ($resultado - 1 / 2);

echo $valor."\n"; // show 3, and should be 1

为什么会出现这种情况?

The following code outputs "3". I was expecting "1".

echo $resultado."\n"; // show 2
$valor = $resultado * ($resultado - 1 / 2);

echo $valor."\n"; // show 3, and should be 1

Why does this happen?

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

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

发布评论

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

评论(7

饮惑 2024-12-18 08:50:09

因为除法 1 / 2 在运算顺序上优先。所以你确实有这个表达式:

$resultado * ($resaltudo - (1 / 2))

你应该添加括号为:

$resultado * (($resaltudo - 1) / 2)

以获得你想要的答案。

Because the division 1 / 2 takes precedence in the order of operations. So you have really have this expression:

$resultado * ($resaltudo - (1 / 2))

You should add parenthesis to be:

$resultado * (($resaltudo - 1) / 2)

to get the answer you want.

这样的小城市 2024-12-18 08:50:09

不,你错了。 / 优先于 - 所以你的行是这样的:

$valor = $resultado * ($resultado - (1 / 2));

那就是:

$valor = 2 * (2 - 0.5); // and so $valor = 3

No, you're wrong. The / has priority on - and so your line is like:

$valor = $resultado * ($resultado - (1 / 2));

and that is:

$valor = 2 * (2 - 0.5); // and so $valor = 3
冬天旳寂寞 2024-12-18 08:50:09

这是因为除法运算符 (/) 的优先级高于减法运算符 (-)。

您的表达式按顺序变为:

1 / 2 = 0.5 // Executed first since it's the highest precedence operation inside ()
$resultado - 0.5 = 1.5 // Still in the ()
$resultado * 1.5 = 3 // Final result

要纠正您的表达式,请在减法周围插入括号,如下所示:

$resultado * (($resultado - 1) / 2);

That's because the division operator (/) has a higher precedence than the subtraction operator (-).

Your expression becomes, in order:

1 / 2 = 0.5 // Executed first since it's the highest precedence operation inside ()
$resultado - 0.5 = 1.5 // Still in the ()
$resultado * 1.5 = 3 // Final result

To correct your expression, insert parethesis around the subtraction, like this:

$resultado * (($resultado - 1) / 2);
柒夜笙歌凉 2024-12-18 08:50:09

/ 优先于 + 或 -
要得到 1 作为结果,你需要使用

$resultado * (($resultado - 1) / 2)

The / takes precedence over + or -
To get 1 as a result you need to use

$resultado * (($resultado - 1) / 2)
高速公鹿 2024-12-18 08:50:09

替换表达式中的 $resultado ,您将得到:

$valor = 2 * (2 - 1 / 2);

2 - 1 / 2 = 1.5
2 * 1.5 = 3

我的建议是复习基本数学;)

Replacing $resultado in the expression, you get:

$valor = 2 * (2 - 1 / 2);

2 - 1 / 2 = 1.5
2 * 1.5 = 3

My suggestion is review basic math ;)

婴鹅 2024-12-18 08:50:09

将其更改为:

echo $resultado."\n";
$valor = $resultado * (($resultado - 1) / 2);

echo $valor."\n";

您实际上正在执行 2 * (2 - (1 / 2) = 2 * 1.5 = 3

Change it to:

echo $resultado."\n";
$valor = $resultado * (($resultado - 1) / 2);

echo $valor."\n";

You were effectively doing 2 * (2 - (1 / 2) = 2 * 1.5 = 3

孤者何惧 2024-12-18 08:50:09

2*(2-1/2)

除法运算符的优先级高于减号,因此计算机会这样计算:
2*(2-(1/2)) = 2 * 1.5 = 3

充分使用括号。

2*(2-1/2)

The dividing operator has higher order operator precedence than the minus sign, so the computer will calculate it like this:
2*(2-(1/2)) = 2 * 1.5 = 3

Use parentheses liberally.

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