GMP precision with mpf_set_d

Wes Freeman freeman.wes at gmail.com
Tue Jun 21 20:12:36 CEST 2011


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


More information about the gmp-discuss mailing list