小编Cri*_*dru的帖子

这两个函数的操作顺序是什么

当我遇到问题时,我正在编写一个函数来计算球体的体积,但我不知道为什么如果将 4/3 * PI 更改为 PI * 4/3 我得到了不同的结果。评估的顺序是什么,如果我使用 (4/3) * PI 和 PI * (4/3) 这样的括号,我在这两种情况下都得到了错误的答案。

#define PI 3.141592

float Volume(int radius){
      return (4/3 * PI * pow(radius, 3));
}

float Volume(int radius){
      return (PI * 4/3 * pow(radius, 3)); //the good one
}
Run Code Online (Sandbox Code Playgroud)

c++ floating-point integer integer-arithmetic

4
推荐指数
1
解决办法
62
查看次数

标签 统计

c++ ×1

floating-point ×1

integer ×1

integer-arithmetic ×1