0

計算結果MPFRをファイルに出力したいのですが、方法がわかりません。MPFR高精度の浮動小数点演算を行うために使用されます。数値を印刷するmpfr_tには、次の関数を使用します。

size_t mpfr_out_str (FILE *stream, int base, size t n, mpfr t op, mp rnd t rnd)

私の問題は、FILE*オブジェクトと、それらがオブジェクトにどのように関連しているかを理解していないことだと思いfstreamます。

行を変更my_fileすると、期待どおりに番号が画面に出力されますが、ファイルに取得する方法がわかりません。mpfr_out_strstdout

#include <mpfr.h>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
   mpfr_t x;
   mpfr_init(x);
   mpfr_set_d(x, 1, MPFR_RNDN);

   ofstream my_file;
   my_file.open("output.txt");
   mpfr_out_str(my_file, 2, 0, x, MPFR_RNDN);
   my_file.close();
}
4

2 に答える 2

1

mpfr_as_printf や mpfr_get_str などの mpfr 関数で std::ostream メソッドを使用することができます。ただし、追加の文字列割り当てが必要です。

  #include <mpfr.h>
  #include <iostream>
  #include <fstream>
  using namespace std;
  int main() {
     mpfr_t x;
     mpfr_init(x);
     mpfr_set_d(x, 1, MPFR_RNDN);

     ofstream my_file;
     my_file.open("output.txt");

     char* outString = NULL;
     mpfr_asprintf(&outString, "%RNb", x);
     my_file << outString;
     mpfr_free_str(outString);
     my_file.close();

     mpfr_clear(x);
  }
于 2016-08-08T13:50:09.453 に答える