用c語言 寫一個計算圓周率的程序
用c語言 寫一個計算圓周率的程序
整理自網(wǎng)絡(luò)ChatGPT產(chǎn)生之內(nèi)容,文本內(nèi)容不具備參考意義,程序內(nèi)容及代碼片段有且僅有借鑒意義。
#include
#include // 利用無窮級數(shù)近似計算出圓周率 double pi() { int n = 1; // 計數(shù)器 double pi = 0; double add_num = 1.0; // 分子 while(fabs(add_num) > 1e-6) // 當(dāng)分子的絕對值大于 1e-6 時, 繼續(xù)計算 { pi += add_num; n += 2; add_num *= -1.0 * (1.0 / n); } pi *= 4.0; return pi; } // 測試函數(shù) int main () { printf("pi = %lf\n", pi()); return 0; }
Public @ 2023-02-18 16:23:15 整理自網(wǎng)絡(luò)ChatGPT產(chǎn)生之內(nèi)容,文本內(nèi)容不具備參考意義,程序內(nèi)容有且僅有借鑒意義。