Bug in C++ interface (comparison between rational and double)

Roberto Bagnara bagnara at cs.unipr.it
Tue Feb 3 09:45:39 CET 2009


Marc Glisse wrote:
> On Tue, 27 Jan 2009, Abramo Bagnara wrote:
> 
>> The comparison between mpz_class and double works fine with +inf and
>> -inf, while the comparison between mpq_class and double gives a floating
>> point exception, due to inappropriate mpq_set_d call in eval specialization.
>>
>> I see two way to fix this:
>>
>> 1) add support for comparison between mpq and double in C interface and
>> use that in gmpxx.h
>>
>> 2) add infinity (and nan) check in comparison eval specializations in
>> gmpxx.h
>>
>> IMHO 1) gives a more complete solution. If you want we're willing to
>> write the (trivial) patch.
> 
> Some advantages of solution 2 (I am not saying I am in favor of 2, just 
> mentionning them for completeness) are that the patch is small 
> (if(isfinite(d)) current code; else return 0<d;), does not introduce any 
> new interface, and has absolutely no impact on the code when compiled with 
> g++ -ffinite-math-only.
> 
> It looks like comparison is the only operation where a Inf or NaN double 
> makes sense in gmpxx (all the others return a gmp type, which can not 
> represent Inf or NaN) (actually division by Inf could make sense too).
> 
> Just curious: what application do you have where it is useful to compare a 
> gmp type to a non-finite double? (I am not questionning the usefulness of 
> the feature request but genuinely interested)

Hi Marc,

in the Parma Polyhedra Library, we provide numerical abstractions that
are parametric with respect to several (extended) number families.
This includes all the native integer and floating point types and
GMP's integers and rationals, possibly augmented with special values
for -inf, +inf and NaN.  On these numerical types, we provide several
operations, including safe (modulo bugs) comparisons.  To implement
those we of course rely on what (we believe) is provided by the
underlying base type.

In this case, we were spoiled by the fact that GMP supports
comparisons between mpz and double via mpz_cmp_d (which can be called
with an infinity).  Without looking at the manual (tsk, tsk, ...)
we assumed the same functionality was provided for mpq.  However,
it seems there is no such a thing as mpq_cmp_d and, as you know,
the implementation of this comparison in gmpxx.h is incorrect.

I don't know if there is any plan to add mpq_cmp_d.  However,
a simple patch to gmpxx.h would fix the problem anyway.
Please let us know if you want us to post the patch.
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


More information about the gmp-bugs mailing list