critical bug in mpf_set_str

Paul Zimmermann Paul.Zimmermann at loria.fr
Mon Feb 16 20:23:59 CET 2004


There is a critical bug in gmp-4.1.2: two equal (up to trailing zeros) 
strings are converted into different floating-point numbers:

mermoz% gcc gmp_bug.c -lgmp
mermoz% ./a.out 
x=0.111101010111e15 and
y=0.111101010111e15 equal
x=0.111101010111e15 and
y=0.111101010111e15 differ

mermoz% uname -a
Linux mermoz.loria.fr 2.4.22-1.2088.nptl #1 Thu Oct 9 20:21:24 EDT 2003 i686 i686 i386 GNU/Linux
mermoz% gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.3.1/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.3.1 20030930 (Red Hat Linux 3.3.1-6)

Paul Zimmermann

##############################################################################

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

int
main()
{
  mpf_t x, y;

  mpf_init2 (x, 10);
  mpf_init2 (y, 10);

  mpf_set_str (x, "3.1416E4", 10);
  mpf_set_str (y, "3.14160000000000000000000000000000000000000000000E4", 10);
  printf ("x="); mpf_out_str (stdout, 2, 0, x);
  printf (" and\ny="); mpf_out_str (stdout, 2, 0, y);
  if (mpf_cmp (x, y))
    printf (" differ\n");
  else
    printf (" equal\n");

  mpf_set_str (x, "3.1416E4", 10);
  mpf_set_str (y, "3.141600000000000000000000000000000000000000000000E4", 10);
  printf ("x="); mpf_out_str (stdout, 2, 0, x);
  printf (" and\ny="); mpf_out_str (stdout, 2, 0, y);
  if (mpf_cmp (x, y))
    printf (" differ\n");
  else
    printf (" are equal\n");

  mpf_clear (x);
  mpf_clear (y);
}


More information about the gmp-bugs mailing list