C/C++中使用PI(π)

2两方法:

  1. 使用math中的宏定义M_PI;
  2. 利用arccos(-1) = π,来计算π值;
#include <iostream>
#include <iomanip>
#define _USE_MATH_DEFINES // 使用math.h中的M_PI宏定义需要
#include <math.h>

using namespace std;
int main()
{
	double pi1 = M_PI;
	cout << setprecision(30) << "pi1 = "  << pi1 << endl;

	double pi2 = acos(-1);
	cout <<  "pi2 = " << pi2 << endl;
        return 0;
}

运行结果:

pi1 = 3.141592653589793115997963468544185161590576171875
pi2 = 3.141592653589793115997963468544185161590576171875
posted @ 2021-10-25 00:00  明明1109  阅读(7619)  评论(0编辑  收藏  举报