【发布时间】:2018-10-18 06:45:28
【问题描述】:
我正在计算一个球体的体积,经过大量研究后我发现我无法使用:
float sphereRadius = 2.33;
float volSphere = 0;
volSphere = (4/3) * (M_PI) * std::pow(sphereRadius, 3);
但必须添加3.0 才能获得正确答案。
volSphere = (4/3.0) * (M_PI) * std::pow(sphereRadius, 3);
为什么必须添加小数才能得到正确的计算?
【问题讨论】:
-
查找整数除法...必须是数百个重复项
-
仅涉及整数的表达式被执行为整数运算并具有整数结果。
标签: c++ division integer-division