Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c++;无小数点限制 < >我有一个C++程序,它计算PI,但是它的值在小数点后被切断,因为皮存储的浮点的小数点限制。_C++_Precision_Numerical Methods_Pi - Fatal编程技术网

如何在c++;无小数点限制 < >我有一个C++程序,它计算PI,但是它的值在小数点后被切断,因为皮存储的浮点的小数点限制。

如何在c++;无小数点限制 < >我有一个C++程序,它计算PI,但是它的值在小数点后被切断,因为皮存储的浮点的小数点限制。,c++,precision,numerical-methods,pi,C++,Precision,Numerical Methods,Pi,我已经尝试使用不同的数据类型来代替float,但是它们仍然有一个小的小数点限制 #include <iostream> #include <fstream> #include <sstream> using namespace std; int main(){ cout << "Calculate Pi using the Nilakantha series" << endl << endl; unsig

我已经尝试使用不同的数据类型来代替float,但是它们仍然有一个小的小数点限制

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main(){
    cout << "Calculate Pi using the Nilakantha series" << endl << endl;

    unsigned long int iterations = 0;
    unsigned long int x = 0;
    unsigned long int y = 2;
    char z = 0;
    string fileName = "";
    int64_t Pi = {3.0f};

    cout << "Input the number of loop iterations(maximum of 4,294,967,295): ";
    cin >> iterations;
    cout << "Input the name of the file you want to write to: ";
    cin >> fileName;

    for(x = 0; x < iterations; x++){
        Pi += 4.0f / (y * (y + 1.0f) * (y + 2.0f));
        y += 2;
        Pi -= 4.0f / (y * (y + 1.0f) * (y + 2.0f));
    y += 2;
    }

    ofstream file;
    file.open(fileName + ".txt");
    file << Pi;
    file.close();

    cout << endl << "Calculation complete, check the file " << fileName << ".txt";

    cin >> z;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
cout(我忽略了@JAntonioPerez在其精细回答中指出的一些不太基本的问题,并将重点放在我发现的这个问题的核心。)

如果您只需要更多的小数位数,请使用
double
long double
而不是
float

如果您需要更高的精度,或者可以指定为运行时或编译时任意值的精度,则需要其他值。或者:

  • 使用任意精度的数字数据类型;下面是一组提供此类类型的库
  • 使用迭代来确定输出中单个数字/字符的值,而不是对整个数字进行迭代;并将获得的数字一次写入一个(或一次写入几个)输出文件

  • 当然,这两种选择都相当复杂,可能不适合“玩具计划”就像你在写一样。

    这里有几个大问题。第一个问题是你正在使用
    std::cout
    打印值,默认情况下
    std::cout
    只打印6位数字。第二个问题是你声明Pi为
    int64_t
    。这意味着你最多可以得到1位精度,因为Pi是整数。你需要d将其作为浮点类型

    那么每种类型你能得到多少位数

    • 来自
      float
    • 双精度
      中的15-16位
    • clang和gcc上的长双精度20位,但在Visual Studio中只有15-16位
    • GCC的
      \uuuu float128
      中大约有30个数字,尽管这个数字只适用于GCC
    如果你想获得更高的精度,你就必须使用一个高精度的算术库,通过模拟它来提供更多的数字。它不会那么快,但它可以完成这项工作

    您可以使用此代码对每种类型的精度进行调整。我在类型上设置了
    calculatePi
    ,因此您只需插入
    float
    double
    long double
    、或
    \uuuuFloat128
    。它将打印20位。此代码应正确打印
    \uuuuufloat128的所有20位数字e> 

    #include <cstdio>
    using namespace std;
    
    template<class Float>
    Float calculatePi(size_t iterations) {
        Float y = 2;
        Float Pi = 3;
        const Float two = 2;
        const Float four = 4;
        const Float one = 1;
    
        for(size_t i = 0; i < iterations; i++) {
            Pi += four / (y * (y + one) * (y + two));
            y += two;
            Pi -= four / (y * (y + one) * (y + two));
            y += two;
        }
        return Pi; 
    }
    int main(){
        long double result = calculatePi<long double>(1000000); 
    
        printf("Expected: 3.14159265358979323846\n"); 
        printf("Actual:   %.20Lf", result); 
    }
    
    #包括
    使用名称空间std;
    模板
    浮点计算EPI(大小迭代){
    浮动y=2;
    浮点数Pi=3;
    常数浮点2=2;
    常量浮点四=4;
    常量浮点1=1;
    对于(大小i=0;i
    使用double或long double。我试过了,但我想存储很多(可能超过1000)位数的圆周率,这似乎不起作用。然后你必须使用一个bignumber库。例如,选项2可能是重复的:
    inta=10000,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5;for(;d=0,g=c*2;c-=14,printf(%.4d),e+d/a),e=d%a)for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);}
    @MosheGottlieb:我会称之为“相当复杂”-(