# GMP precision with mpf_set_d

Jørgen Fulsebakke jorgen.fulsebakke at gmail.com
Tue Jun 21 21:53:13 CEST 2011

```Thanks for answers!

Ok then the way I understand it (I could not find or maybe understand the
answer to this in the manual): if I use mpf_set_d then the mp variable is
equal to the double up to the double's precision and then some random
numbers.

So I can use mpf_set_str to get the desired precision. In the program I'm
writing I need to set mp variables to values I don't know beforehand but
compute by using double arithmetic. I guess I can e.g. compute a cosine
using the exponential definition, with inputs defined using mpf_set_str.

If there is an eeasier way I'd appreciate a tip, otherwise thanks again!

Jørgen

2011/6/21 Wes Freeman <freeman.wes at gmail.com>

> I wrote two tests below with extra printing--you can see what the
> problem is here (after initialization they aren't exactly what you
> think they are):
>
> [wfreeman at www src]\$ cat test.c
> #include <gmp.h>
>
> int main(int argc, char **argv) {
>   mpf_t x, y, z;
>  mpf_init2(x, 1000000);
>  mpf_init2(y, 1000000);
>  mpf_init2(z, 1000000);
>   mpf_set_str(x, "1E0", 10);
>   mpf_set_d(y, 3.3124365842565);
>  mpf_set_d(z, 3.3124365842566);
>   gmp_printf("x: %1.100Ff\n",x);
>  gmp_printf("y: %1.100Ff\n",y);
>  gmp_printf("z: %1.100Ff\n",z);
>   mpf_mul(y, x, y);
>  mpf_mul(z, x, z);
>  mpf_sub(x, y, z);
>   gmp_printf("doing mul/sub\n");
>  gmp_printf("x: %1.100Ff\n",x);
>  gmp_printf("y: %1.100Ff\n",y);
>  gmp_printf("z: %1.100Ff\n",z);
> }
> [wfreeman at www src]\$ gcc -o test -lgmp test.c
> [wfreeman at www src]\$ ./test
> x:
> 1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
> y:
> 3.3124365842565000050967682909686118364334106445312500000000000000000000000000000000000000000000000000
> z:
> 3.3124365842565999251689845550572499632835388183593750000000000000000000000000000000000000000000000000
> doing mul/sub
> x:
> -0.0000000000000999200722162640886381268501281738281250000000000000000000000000000000000000000000000000
> y:
> 3.3124365842565000050967682909686118364334106445312500000000000000000000000000000000000000000000000000
> z:
> 3.3124365842565999251689845550572499632835388183593750000000000000000000000000000000000000000000000000
>
> [wfreeman at www src]\$ cat test.c
> #include <gmp.h>
>
> int main(int argc, char **argv) {
>   mpf_t x, y, z;
>  mpf_init2(x, 1000000);
>  mpf_init2(y, 1000000);
>  mpf_init2(z, 1000000);
>   mpf_set_str(x, "1E0", 10);
>   mpf_set_str(y, "3.3124365842565", 10);
>  mpf_set_str(z, "3.3124365842566", 10);
>   gmp_printf("x: %1.100Ff\n",x);
>  gmp_printf("y: %1.100Ff\n",y);
>  gmp_printf("z: %1.100Ff\n",z);
>   mpf_mul(y, x, y);
>  mpf_mul(z, x, z);
>  mpf_sub(x, y, z);
>   gmp_printf("doing mul/sub\n");
>  gmp_printf("x: %1.100Ff\n",x);
>  gmp_printf("y: %1.100Ff\n",y);
>  gmp_printf("z: %1.100Ff\n",z);
> }
> [wfreeman at www src]\$ gcc -o test -lgmp test.c
> [wfreeman at www src]\$ ./test
> x:
> 1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
> y:
> 3.3124365842565000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
> z:
> 3.3124365842566000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
> doing mul/sub
> x:
> -0.0000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
> y:
> 3.3124365842565000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
> z:
> 3.3124365842566000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>
```