GMP suggestions

Nic Schraudolph nic at
Tue May 25 19:37:10 CEST 2010

>> In numerical programming one often needs a number that is  
>> guaranteed to be
>> greater than any other of its class (simple example: to initialize a
>> minimum-finding loop). Without an infinity, this becomes impossible
>> in a class with theoretically infinite capacity.
> I must be getting this wrong: initialize with the first element and
> loop through the rest, replacing whenever something smaller is found.
> So no need for +-Inf (?)

Sorry, I oversimplified the example. I have a situation where such a  
simple shortcut is not available, because the bound being maintained  
is not constrained as simply by a single piece of data as in the  
example. Of course one could always use schemas like

mpq_class bound;
bool bound_is_inf = true;
while (...)
  // calculate new_bound...
  if (bound_is_inf || new_bound < bound) {
    bound = new_bound;
    bound_is_inf = false;

but this is cumbersome and becomes error-prone as the numerical  
computations get more complicated. Imagine the calculation of  
"new_bound" above involving "bound" many times in complex ways: each  
reference will then have to be guarded by "if (bound_is_inf)". To keep  
the code sane you'll have to subclass bound's class, make "is_inf" a  
member, and overload all operators that must be guarded... amounting  
to a poor implementation of class "mpq_with_inf".


- nic

More information about the gmp-discuss mailing list