"bug" in gmp_scanf et al.?

Ronald Bruck bruck at usc.edu
Wed Mar 10 00:39:28 CET 2010


This doesn't qualify as a bug, as far as I'm concerned, except that it's unexpected behavior.

gmp_scanf, gmp_fscanf, and gmp_sscanf all consider a real number of the form 1.23e to be malformed.  I'm inclined to agree; but that's not what seems to be the C standard.  At least I deduce that from this program, compiled with icc.  (I haven't c checked to see if the compiler's behavior is correct.)

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

int main (int argc, char **argv)
{
  char buf[1024];
  double f;
  mpf_t t;

  mpf_init (t);

  if (argc == 1) {
    fprintf (stderr, "Usage:  test string\n");
    exit (-1);
  }

  /* First print as a string. */
  sscanf (argv[1], "%s", buf);
  printf ("string = \"%s\"\n", buf);

  /* Then print using C's sscanf/printf */
  sscanf (buf, "%le", &f);
  printf ("scanf = %le\n", f);

  /* Finally, print using GMP's sscanf/printf */
  gmp_sscanf (buf, "%Fe", t);
  gmp_printf ("gmpscanf = %Fe\n", t);
}


Compiling this as program "test" and executing

    test 1.23e7

results in an output of

  string = "1.23e7"
  scanf = 1.230000e+07
  gmpscanf = 1.230000e+07

whereas executing

  test 1.23e

results in

  string = "1.23e"
  scanf = 1.230000e+00
  gmpscanf = 0.000000e+00

This behavior is also true of hexadecimal reals, such as  0.abcd@   That is, gmp considers it malformed; standard C doesn't recognize it at all.

-- Ron Bruck





More information about the gmp-bugs mailing list