1 Star 0 Fork 3

m-RNA / WFloat

forked from Morphlng / WFloat 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
calculate_pi.cpp 1.26 KB
一键复制 编辑 原始数据 按行查看 历史
#include "WFloat.hpp"
#include <iostream>
#include <chrono>
WFloat pi(int iter, int accuracy = -1)
{
if (accuracy != -1)
WFloat::setAccuracy(accuracy);
WFloat pi = 0;
WFloat sign = 1;
WFloat denom = 1;
WFloat term = 1;
int i = 0;
while (term != 0)
{
pi += term;
sign *= -1;
denom += 2;
term = sign / denom;
++i;
if (i == iter)
break;
}
return pi * 4;
}
int main()
{
std::cout << "This is a program to calculate pi using the Leibniz formula.\n"
"Press Ctrl+C to exit.\n\n";
while (true)
{
std::cout << "Enter the number of iterations: ";
int iter;
std::cin >> iter;
std::cout << "\nEnter the accuracy of float number (default 100): ";
int accuracy;
std::cin >> accuracy;
auto start = std::chrono::high_resolution_clock::now();
std::cout << "\nThe value of pi is: " << pi(iter, accuracy) << "\n\n";
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "Elapsed time: " << elapsed.count() << "s\n\n";
std::cout << "----------------------------------------\n\n";
}
return 0;
}
C++
1
https://gitee.com/chenjjian/wfloat.git
git@gitee.com:chenjjian/wfloat.git
chenjjian
wfloat
WFloat
master

搜索帮助