possible bug in mpz_cmp_d

Tom Tromey tom at tromey.com
Tue Jul 10 05:04:57 UTC 2018

While adding bignums to Emacs, I found a case where mpz_cmp_d returns a
value I did not expect.  I don't know if I have done something wrong or
if this is a bug in GMP.

I'm using x86-64 Fedora 26.  I have the system GMP:

    $ rpm -q gmp

Consider this test program:

    #include <gmp.h>
    #include <stdio.h>

    int main ()
      mpz_t val;
      mpz_init (val);
      mpz_set_si (val, -2305843009213693953);
      double d = -2305843009213693953.0;
      int x = mpz_cmp_d (val, d);
      printf ("%d\n", x);

I expect this to print 0, because the numbers are the same, and because
the number can be exactly represented as a double.

However, this program prints -1 for me.

If I convert to double like this:

  double xy = mpz_get_d (val);
  x = 0;
  if (xy < d)
    x = -1;
  else if (xy > d)
    x = 1;
  printf ("%d\n", x);

... I do get 0.


More information about the gmp-bugs mailing list