C++ 如何找到从3.14159开始的第一个pi
为了确定第一次获取以3.14159开头的pi需要多少个术语,我编写了以下程序,该程序将术语计算为C++ 如何找到从3.14159开始的第一个pi,c++,string,pi,C++,String,Pi,为了确定第一次获取以3.14159开头的pi需要多少个术语,我编写了以下程序,该程序将术语计算为(pi=4-4/3+4/5-4/7+…) 我的问题是,结果我达到了146063个学期,但当我检查时,有许多法律专业人士在这之前也有类似的开始 //piEstimation.cpp //estima mathematical pi and detrmin when //to get a value beganing with 3.14159 #include <iostream> #in
(pi=4-4/3+4/5-4/7+…)
我的问题是,结果我达到了146063个学期,但当我检查时,有许多法律专业人士在这之前也有类似的开始
//piEstimation.cpp
//estima mathematical pi and detrmin when
//to get a value beganing with 3.14159
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main(){
//initialize vars
double denominator{1.0};
double pi{0};
string piString;
double desiredPi;
int terms;
int firstDesiredTerm;
//format output floating point numbers to show 10 digits after
// decimal poin
cout << setprecision (10) <<fixed;
for (terms = 1; ; terms++){
if(0 == terms % 2){ //if term is even
pi -= 4/denominator;
}
else{ //if term is odd
pi += 4/denominator;
}
// draw table
cout << terms << "\t" << pi << endl;
//determin first time the pi begains with 3.14159
piString = to_string(pi).substr(0,7);
if(piString == "3.14159"){
firstDesiredTerm = terms;
desiredPi = pi;
break;
}
denominator += 2;
}//end for
cout << "The first time that pi value begans with 3.14159 "
<< "the number of terms are " << firstDesiredTerm << " and pi value is "<< desiredPi <<endl;
}//end main
//piEstimation.cpp
//当
//获取以3.14159开头的值
#包括
#包括
#包括
使用名称空间std;
int main(){
//初始化变量
双分母{1.0};
双π{0};
弦管;
双重愿望;
国际术语;
int firstDesiredTerm;
//设置输出浮点数的格式,以在输出后显示10位数字
//小数点
cout如果x>=3.14159&&x<3.1416
,则数字x
以3.14159开头。无需使用字符串和比较字符。to_string
必须使用某种舍入运算。如果没有字符串,算法将在136121步后查找结果
#include <iostream>
#include <iomanip>
int main(){
//initialize vars
double denominator{1.0};
double pi{0};
double desiredPi;
int terms;
int firstDesiredTerm;
//format output floating point numbers to show 10 digits after
// decimal poin
std::cout << std::setprecision (20) << std::fixed;
for (terms = 1; ; terms++){
if(0 == terms % 2){ //if term is even
pi -= 4/denominator;
}
else{ //if term is odd
pi += 4/denominator;
}
// draw table
std::cout << terms << "\t" << pi << std::endl;
if(pi >= 3.14159 && pi < 3.1416){
firstDesiredTerm = terms;
desiredPi = pi;
break;
}
denominator += 2;
}//end for
std::cout << "The first time that pi value begans with 3.14159 "
<< "the number of terms are " << firstDesiredTerm
<< " and pi value is "<< desiredPi << std::endl;
}
在这里,您可以看到如何对字符串进行四舍五入:
#include <iostream>
#include <iomanip>
#include <string>
int main(){
std::cout << std::setprecision (20) << std::fixed;
std::cout << std::to_string(3.14159999999478589672) << '\n';
}
你可以继续读下去
std::string to_string(double value);
将浮点值转换为与std::sprintf(buf,“%f”,value)
为足够大的buf
生成的内容相同的字符串
你可以继续读下去
f
f
Precision指定小数点字符后显示的精确位数。默认精度为6
这意味着std::to_string
在6位数字后舍入。如果x>=3.14159&&x<3.1416
,则数字x
以3.14159开头。无需使用字符串和比较字符。to_string
必须使用某种舍入操作。您知道数字是如何舍入的吗?谢谢您的回答。还有一件事,为什么在条件if语句中,pi<3.1416
是必不可少的?当我省略此语句时,术语会增加,结果会出错。@Farshid,因为4>=3.14159
,但它不是以3.14159
开头的。以3.14159
开头的最小数字是3.14159
和最小的大于3.14159
且不以3.14159
开头的数字是3.1416
@Farshid,我不明白您的意思。当我在if条件下删除pi<3.1416
时,输出是“pi值第一次以3.14159开头时,项数是1,pi值是4.00000000000000000000”.4显然比3.14159
大,但它不是以3.14159
开头的。你的问题是什么?哦,亲爱的托马斯。这是真的。我在这件事上有点困惑
#include <iostream>
#include <iomanip>
#include <string>
int main(){
std::cout << std::setprecision (20) << std::fixed;
std::cout << std::to_string(3.14159999999478589672) << '\n';
}
3.141600