GMP vaguaries when converting strings to integers

Roberto Bagnara bagnara at cs.unipr.it
Tue Oct 19 07:58:00 CEST 2004


The following program shows that the behavior
of GMP when converting strings to integers is,
at best, questionable.  While some aspects can
be somehow defended, there are others that are,
in my opinion, serious bugs.

1) Allowing blank spaces in the middle of numerals
    is simply a recipe for troubles.

2) Silent and thus undetectable failures of C++
    constructors is a recipe for disaster.  In C++
    a constructor either succeeds in doing what
    it has to do, or it must throw an exception.
    Or call abort() if you still believe that exceptions
    are evil... whatever: silently masking the error
    is the worst thing that can be done.

All the best,

     Roberto

-- 
Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
http://www.cs.unipr.it/~bagnara/
mailto:bagnara at cs.unipr.it


#include <gmpxx.h>
#include <iostream>

using namespace std;

void
test(const char* s) {
   mpz_class x = -1;
   x = s;
   cout << "x(\"" << s << "\") --> " << x << endl;
}

int main() {

   // Questionable success.
   test(" 1234");

   // Questionable success.
   test("1234 ");

   // Highly questionable success.
   test("12 34");

   // Highly questionable success.
   test("12\n34");

   // Expected silent, undetectable failure.
   test(" ");

   // Expected silent, undetectable failure.
   test("1234a");

   // OK.
   test("-1234");

   // Unexpected silent, undetectable failure.
   test("+1234");

   // Questionable silent, undetectable failure.
   test("12.34");

   // Questionable silent, undetectable failure.
   test("1e3");
}




More information about the gmp-devel mailing list