I installed gmp and mpfr on my environment. Now I can successfully
#include <gmpxx.h>
#include <mpfr.h>
#include <mpf2mpfr.h>
Now, say that I initialize an mpf_class with some value:
mpf_class x = 0.73;
How can I use mpfr to get the sin of this number? I just need an mpf_class in, an mpf_class out. Something like:
mpf_class y = sin(x)
Which obviously doesn't work. I noticed that there is a mpfr_sin function, which I called like this:
mpfr_sin(x, y, MPFR_RNDN);
But that didn't work as well. So what am I supposed to do? Am I doing something wrong?
Thank you
mpf2mpfr.h is probably not what you want. It contains plenty of #define to replace mpf names with mpfr names in everything that follows. If you wanted a chance of having it work in your case, you would have to include mpf2mpfr.h before gmpxx.h. However, the file does not translate everything. The following lets it compile in C++03 (as long as you don't convert to mpq_class):
#include <mpfr.h>
#include <mpf2mpfr.h>
void mpfr_get_q (mpq_ptr, mpfr_srcptr);
#undef mpq_set_f
#define mpq_set_f(x,y) mpfr_get_q(x,y)
#include <gmpxx.h>
int main(){
mpf_class x=.73;
mpf_class y;
mpfr_sin(y.get_mpf_t(),x.get_mpf_t(),MPFR_RNDN);
}
but trying to print with operator<< will print a pointer instead of the number, for instance. Extra functions provided in C++11 would require more tweaking, it is easier to disable them: #define __GMPXX_USE_CXX11 0 before including gmpxx.h.
There are mostly two ways to solve this and both start with removing mpf2mpfr.h. The first one is to create a temporary mpfr_t:
mpf_class x=.73;
mpf_class y;
mpfr_t xx;
mpfr_t yy;
mpfr_init_set_f(xx, x.get_mpf_t(), MPFR_RNDN);
mpfr_init(yy);
mpfr_sin(yy, xx, MPFR_RNDN);
mpfr_get_f(y.get_mpf_t(), yy, MPFR_RNDN);
The second one is to drop mpf completely and use only mpfr. Its webpage lists 6 C++ wrappers for it, several of which are still maintained.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With