在浮点数组上使用mpf_set_ui会停止输出(GMP c++)

Using mpf_set_ui on a float array stops output (GMP c++)

本文关键字:输出 c++ GMP ui set 数组 mpf      更新时间:2023-10-16

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

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_set_ui之后停止。如果我在没有数组的情况下尝试此操作,那么t[5]和t[4]就变成了t,一切都如预期的那样工作。我做错了什么?GMP中是否允许使用阵列?

您可能应该按照以下更改代码

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]);
}