GMP 浮动值 - 以 10 为基数以给定精度打印它们

GMP floating values - print them with given precision in base 10

本文关键字:精度 打印 GMP      更新时间:2023-10-16

假设我有一个

mpf_t number;

等于 1.25。我想以给定的精度打印它,例如,即使会有尾随零

1.2500000

当我尝试使用 mpf_get_str 函数打印它时,它会以科学 (e( 格式打印,

mpf_out_str(stdout,10,7,number); //prints 0.125e1

当我使用 c++ 的 cout 时,mpf_t<<过载,

cout << number << endl; //prints 1.25

当我尝试通过在 cout 中调用 setprecision(给定精度(来打印它时,我仍然得到 1.25

cout << setprecision(7) << number << endl; //prints 1.25

那么,我怎样才能按照自己的意愿打印数字呢?

您需要将 std::fixed 发送到 std::cout。

#include <iostream>
#include <gmpxx.h>
int main(int argc,char* argv[])
{
  mpf_t mpft;
  mpf_init(mpft);
  mpf_set_d(mpft,1.25);
  std::cout.precision(7);
  std::cout << std::fixed;
  std::cout << mpft << std::endl;
  return 0;
}