What is the correct syntax for using printf and its cousins sprintf and fprintf to display the value of mpreal-type variables? I have tried the naive casting to double:
printf ("... %g ...", (double) var);
only to receive this error message from g++:
error: invalid cast from type ‘mpfr::mpreal’ to type ‘double’
I had no problem using double-type variables elsewhere on the program.
I heard about the type mpreal as part of this library intended to enable the use of the usual binary operators to perform arbitrary precision arithmetic operations.
The
mprealis arbitrary precision floating-point numerical type.As such,
mprealnumbers might have much more significant digits (even hundreds or thousandths) thandouble. Which means it is pointless to roundmprealtodoublebefore display – you will lose all the original accuracy in this case.It is easy to display
mprealin C++:However you can use it with
printfas well (by converting to string first):Format specification for multiple-precision numbers are the same as for standard with the exception to rounding specification. You can safely use rounding to nearest,
RNas in examples above.More details on mp-formatting are given in MPFR documentation.
(I’m author of
mprealclass aka MPFR C++)