Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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++ 在浮点数组上使用mpf设置ui会停止输出(GMP c+;+;)_C++_Arrays_Gmp - Fatal编程技术网

C++ 在浮点数组上使用mpf设置ui会停止输出(GMP c+;+;)

C++ 在浮点数组上使用mpf设置ui会停止输出(GMP c+;+;),c++,arrays,gmp,C++,Arrays,Gmp,对于一个学校项目,我必须做一些涉及大量数据的计算,这就是我选择使用GMP的原因。在我的主程序中遇到奇怪的bug之后,我开始在另一个程序中进行实验。以下代码显示了出现的问题: mpf_set_default_prec(512); mpf_t t[5]; mpf_init(t[5]); cout << "This does appear." << endl; mpf_set_ui(t[4],9); cout << mpf_get_d(t[4]) <<

对于一个学校项目,我必须做一些涉及大量数据的计算,这就是我选择使用GMP的原因。在我的主程序中遇到奇怪的bug之后,我开始在另一个程序中进行实验。以下代码显示了出现的问题:

mpf_set_default_prec(512);
mpf_t t[5];
mpf_init(t[5]);
cout << "This does appear." << endl;
mpf_set_ui(t[4],9);
cout << mpf_get_d(t[4]) << endl;
cout << "This does not, neither is the number 9 printed." << endl;
mpf_clear(t[5]);
mpf\u set\u default\u prec(512);
强积金[5];
mpf_init(t[5]);

cout您可能应该如下更改代码

mpf_t t[5];
for(int i = 0; i < 5; ++i) {
    mpf_init(t[i]);
}

mpf_set_ui(t[4],9);
cout << mpf_get_d(t[4]) << endl;

for(int i = 0; i < 5; ++i) {
    mpf_clear(t[i]);
}
强积金[5];
对于(int i=0;i<5;++i){
mpf_init(t[i]);
}
强积金集合(t[4],9),;

cout
mpf_init(t[5])
这将访问有效边界以外的数组(
0-4
)!谢谢,这就解决了!(编辑:为清晰起见,必须将mpf_init(t[5])更改为mpf_init(t[4])。。。不要忘记其他出现的
t[5]
。您可能需要一个(<代码>())代码循环来正确初始化和处理所有数组元素。因为问题是标记[C++ ],所以我建议C++包装器:对于大浮动,在新项目中,使用mpfr比gmp更好,后者专注于整数和有理数。