Specifying precision in vector<mpf_class>?

Ballabio Gerardo - Dip. di Scienza dei Materiali gerardo.ballabio@unimib.it
Wed, 16 Jul 2003 14:22:40 +0200


On 2003.07.16 11:13, Felix E. Klee wrote:
> I just found something that seems to be possible only with template
> parameters:
> 
>     mpf_class<PREC> x[100];

Good point. I believe it can be done with

   unsigned old_prec = mpf_get_default_prec();
   mpf_set_default_prec(PREC);
   mpf_class x[100];
   mpf_set_default_prec(old_prec);

but I acknowledge that your one-liner is considerably more elegant 
(and thread-safe).
However, using C-style arrays should be discouraged in C++, the "true 
C++ way" is to use the appropriate standard container instead.

> BTW, template parameters seem to be supported fine by newer 
> versions of gcc.
> The little program below compiles and executes fine with gcc 3.3

I used to get issues in particular when trying to mix mpf_classes of 
different precision, the problem being that with the template 
approach, you must anticipate the precision of the result, like this:

   template <int N> class c;

   template <int N, int M>
   c<(N>M)?N:M> operator+(const c<N> &c1, const c<M> &c2);

I've just tested this and indeed, it works with g++ 3.0.4, but not 
with 2.95.4 (Debian stable). However Debian is to my knowledge the 
only current distribution still based on a pre-3.0 compiler (and 
"current" and "Debian" may be regarded as mutually exclusive words), 
so this might be fine.

A possible difficulty with templates is that we'd lose the 
possibility to resize an mpf_class, and in particular it would be 
impossible to declare an mpf_class without knowing in advance what we 
want its size to be. But probably this is something that will occur 
very rarely.

Another point is that by design, we wanted mpf_class to be a very 
lightweight wrapper to mpf_t with no extra functionality (for this 
reason we've had a rather strong dispute about whether mpq_class 
should canonicalize its input), and the template approach seems to 
depart from that. (Kevin, Torbjorn, what do you think?)

Anyway, I'll try and look at it, but I'm currently working on a 
couple of other things, so I can't guarantee that you'll see anything 
very soon.

Gerardo