我正在尝试解决此collection的第一个问题
要查找并显示直到第N位的Pi值(作为用户输入受某个最大值限制,我想最大ds可以容纳的小数位数最多,我猜这是长倍吗?)
以下是我的尝试的摘要。
//declarations
typedef numeric_limits< long double > db;
int MAX = db::digits10;
int N = 0; //digit allowance
long double pi_val = M_PI;
string string_pi;
//user input
while(1){
cout << "Enter a value of n < or = " << MAX << endl;
cin >> N;
if(N>18){cout << "Lesser please" << endl;}
else
break;
}
//outputs
cout << "Pi till the " << N << " digit is: ";
cout << setprecision(N+1) << fixed << pi_val << endl;
string_pi = to_string((long double)pi_val * pow(10, N));
cout << string_pi << endl;
cout << "The Nth digit of pi is: " << string_pi[N] << endl;
Ps我知道存在更多新颖的方法来解决该问题,但是由于我是cpp的极端初学者,因此我可以通过使我的想法付诸实践来获得更好的成就感。
现在problem可以正确显示第N个数字,但是尽管有'fixed'和'setprecision()',Pi的输出直到第N个值总是四舍五入,所以我必须显示一个额外的数字以与后一个输出
如果可能的话,如何在不更改我使用的主要逻辑的情况下解决此问题
谢谢