Unexpected output of a simple GMP program

Jiri Simsa jsimsa at cs.cmu.edu
Sat Jul 27 18:50:07 CEST 2013


Hello,

I am trying to use GMP in my project and to familiarize myself with GMP, I
wrote some simple programs. For one of them, I am seeing unexpected
behavior and I am not sure what am I doing wrong.

Here is the example in question:
#include <gmp.h>
int main() {
  mpf_t x;
  mpf_init(x);
  mpf_set_si(x, 1);
  gmp_printf("x = %F\n", x);
  mpf_set_ui(x, 1);
  gmp_printf("x = %F\n", x);
  mpf_set_d(x, 1.0);
  gmp_printf("x = %F\n", x);
  mpf_clear(x);
  return 0;
}

I compile this program with:
g++ -o test test.c -lgmp

When I run it, I see:
x = 0.000000
x = 0.000000
x = 1.000001

While I would expect to see:
x = 1.000000
x = 1.000000
x = 1.000000

Could someone please let me know why the actual output differs from my
expectation? Thanks.

Best,

--Jiri Simsa


More information about the gmp-discuss mailing list