
When we installed GMP 4.2.1 on some machines, the regression testing procedure we use for the Parma Polyhedra Library started signaling a problem. It boils down to the following test program:
#include <gmpxx.h> #include <iostream> #include <cstdio> #include <fenv.h>
int main() { // Rounding upward. fesetround(FE_UPWARD);
mpz_class num("137171200400403985"); mpz_class den("1125899906842624"); mpq_class rational(num, den); rational.canonicalize(); std::cout << "real_coeff = " << rational << "\n";
double floating = rational.get_d(); printf("float = %.20g\n", floating);
return 0; }
The results are different depending on whether it is linked to GMP 4.1.4 or with GMP 4.2.1. For example, on my Fedora 7, x86_64 machine where I have GMP 4.1.4 in /usr/lib64 and GMP 4.2.1 in /usr/local/lib, I use the script
g++ -I/usr/local/include -o bug.o -c bug.cc g++ -static -o bug-gmp-4.2.1 bug.o -L/usr/local/lib -lm -lgmpxx -lgmp g++ -static -o bug-gmp-4.1.4 bug.o -L/usr/lib64 -lm -lgmpxx -lgmp ./bug-gmp-4.2.1 ./bug-gmp-4.1.4
This prints
real_coeff = 137171200400403985/1125899906842624 float = 121.83249999999999602 real_coeff = 137171200400403985/1125899906842624 float = 121.83250000000001023
The mismatch does not happen if one does not touch the rounding mode or if one sets it to FE_TONEAREST, FE_TOWARDZERO, FE_DOWNWARD. So, on this example, while GMP 4.1.4 results do not depend on the rounding mode, with GMP 4.2.1 the rounding mode matters. Perhaps the answer is simply that GMP provides no guarantee when invoked with the rounding mode set to anything different from FE_TONEAREST, but I have not found that in the documentation. Instead, the documentation says, e.g., that
double mpq_get_d (mpq_t op) Convert op to a double, truncating if necessary (ie.: rounding towards zero).
We are uncertain about what to do: for our computations we need to set the rounding mode; for efficiency we would like to avoid setting it back and forth; and, on the other hand, we now have code that behaves differently depending on the version of GMP it is linked to. Any advice? All the best,
Roberto