Bin*_*nka 0 c++ pi arbitrary-precision
我开始自学C++直到我的课程在秋季开课.我想知道你是否能够帮我提出一个更好的方法来询问用户他们想要的数字pi数字,然后显示它.我的问题是使用pi = atan(1)*4并不精确到大约10位小数.是否有更好的内置数字,pi至少至少20个小数位?这是我到目前为止,谢谢!
#include <iostream>
#include <string>
#include <iomanip>
#include <ios>
#include <sstream>
using namespace std;
using std::setprecision;
using std::streamsize;
int main()
{
double pi = atan(1)*4;
int input = 0;
while(true)
{
cout << "Please enter how many digits of PI you would like to see (Max 20): ";
cin >> input;
if(input > 0 && input <= 20)
{
break;
}
else
{
cout << "That's not a valid number! Try again." << endl;
}
}
streamsize prec = cout.precision();
cout << setprecision(input);
cout << "Here you go: " << pi <<endl;
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
Jas*_*zun 14
最简单的方法可能只std::string包含你想要的数字("3.14159265358979323846264338327950288419"),然后只打印input小数点以外的第一个数字.