[PPL-devel] Bug in C++ interface (comparison between rational and double)

Abramo Bagnara abramo.bagnara at gmail.com
Fri Feb 6 13:58:41 CET 2009


Marc Glisse ha scritto:
> 
> Oh, so you extended gmp types with +-inf and NaN, that is interesting.
> Out of curiosity, how do you do that?

For mpz we overload the _mp_size field using MININT for -inf, MAXINT for
+inf and MININT+1 for NaN.

For mpq we use -N/0 for -inf, +N/0 for +inf and 0/0 for NaN.

For both to check for special values is very fast as it can be done only
testing the sizes and the overwriting of such special values is always
safe inside gmplib.


> Note that you still need to handle NaN separately (you could consider it
> another gmpxx bug that comparison (<,>,<=,>=) with NaN may do something
> other than return false).

Yes you're right, I was missing that.

> You could say that the interaction between gmpxx types and double is
> only guaranteed to work in the finite case and the bug is in the missing
> documentation of this feature. It feels unsatisfactory to be able to say
> z<d (for an infinite d) but have (z-d) crash the program... This would
> argue in favor of pushing the appropriate code to a library that
> augments the gmpxx types with +-Inf and NaN. Which doesn't mean this
> won't be done in gmp...

Here I see three design alternatives:

1) gmplib/gmpxx is designed to return correct results only passing
floating point finite values

2) gmplib/gmpxx is designed to store only finite values, but, when
possible, it handles correctly inf and NaN

3) gmplib/gmpx can store finite/infinite and NaN values and handles them
correctly. mpq is a superset of IEC559 floating point values domain.

Currently we are in a strange situation that we may call 1.5 where some
times infinite values are taken, checked and correctly handled and other
times don't.

IMHO a performance critical application like gmplib should reduce the
handling of special cases in unavoidable paths to the minimum (as
probably they are not needed for many users). We have done the same
thing in the PPL adding a templatic policy argument to our numeric
classes where it's specified if the user code expects special values
treatment or not.

Notwithstanding that, I still think that currently gmplib has a bug (in
code or documentation depending on intended design choices).

-- 
Abramo Bagnara

Opera Unica                          Phone: +39.0546.656023
Via Borghesi, 16
48014 Castel Bolognese (RA) - Italy



More information about the PPL-devel mailing list