C++ 中使用 π 值:方法详解及示例
在 C++ 中,可以使用 cmath 头文件中的 M_PI 常量来表示 π 值。M_PI 是一个 double 类型的常量,它的值近似为 3.14159265358979323846。
使用 π 值的方法如下:
#include <iostream>
#include <cmath>
int main() {
double radius = 5.0;
double area = M_PI * pow(radius, 2);
std::cout << 'The area of the circle is: ' << area << std::endl;
return 0;
}
在上面的示例中,我们使用了 pow 函数来计算圆的面积,其中 M_PI 表示 π 的值。请注意,为了使用 π 值,我们必须包含 cmath 头文件。
原文地址: https://www.cveoy.top/t/topic/o2tG 著作权归作者所有。请勿转载和采集!