c++ - Print to file from MPFR -


i want print result of calculation using mpfr file don't know how. mpfr used floating point operations high accuracy. print mpfr_t number use function:

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

i guess problem don't understand file* objects , how related fstream objects.

if change my_file in mpfr_out_str line stdout number print screen i'd hoped don't know how file.

#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(); } 

it possible use std::ostream methods mpfr functions mpfr_as_printf or mpfr_get_str. requires additional string allocation.

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

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -