问题描述:c++调用大数库GMP输出mpf_class类型
本人用的是VS2019,回答之前麻烦装一下GMP的环境,然后调试通过,谢谢~
解决问题者,不限于采纳的10元酬金,可私下支付两倍~
安装GMP的教程:
https://blog.csdn.net/a675115471/article/details/104425406
要求:
(1)必须使用c++的mpf_class类型
(2)输出30位有效数字,输出不能有乱码
(3)不能手动数有效数字位数
源代码如下:
#include<iostream>
#include<fstream>
#include<gmp.h>
#include <gmpxx.h>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
mp_bitcnt_t prec = 11512;
mpf_class x(1.200000100000012345645645454545454544540000000002 ,prec);
mpf_class y(1.2000001, prec);
mp_exp_t exp;
size_t n=25;
string x_str = x.get_str(exp, 10, n);
string y_str = y.get_str(exp, 10, n);
x_str.insert(exp, ".");
cout << "x的值:" << x_str << endl;
y_str.insert(exp, ".");
cout << "y的值:" << y_str << endl;
}
代码结果如下: