inf & nan for mpq

Nic Schraudolph nic at
Sat May 29 00:42:40 CEST 2010

Hello again,

as per my recent suggestion I've now implemented inf & nan handling  
for mpq, using the canonical representations 1/0 for inf, -1/0 for - 
inf, and 0/0 for nan. It's efficient since inf/nan checking is done at  
the top level (often by keying into already existing checks for  
special cases) while leaving the underlying integer representation  
(and hence all mpz_* and mpn_* routines) unaffected.

The implementation is complete, covering all mpq_* functions,  
including mpq_set_* and mpq_get_*. The predicates mpq_equal, mpq_sgn,  
and mpq_cmp always return zero if any argument is nan, with the sole  
exception that mpq_equal(nan, nan) = 1. This is in keeping with  
standard floating-point implementations in which all boolean  
predicates except is_nan() return false if any argument is nan. (The  
alternative would be to raise an exception such as  

I benchmarked this on two programs: the big chunk of C++ code I've  
been converting to use mpq_class, and a simple little C program  
(bbp.c, attached) that uses the BBP formula (naively) to calculate pi.  
I cannot detect any significant difference in speed in either case.

I am attaching my implementation as a set of patches against gmp-5.0.1/ 
mpq. What would be the approved way to submit a contribution like this  
to the main development branch?


- nic

-------------- next part --------------
A non-text attachment was scrubbed...
Name: mpq_nan_inf_patch
Type: application/octet-stream
Size: 9952 bytes
Desc: not available
URL: <>
-------------- next part --------------

-------------- next part --------------
A non-text attachment was scrubbed...
Name: bbp.c
Type: application/octet-stream
Size: 746 bytes
Desc: not available
URL: <>
-------------- next part --------------

More information about the gmp-discuss mailing list