[CLN-list] pow() versus floating-point numbers

Alexei Sheplyakov alexei.sheplyakov at gmail.com
Fri Jan 14 19:04:32 CET 2011


Hello,

On Fri, Jan 14, 2011 at 09:40:23AM +0100, Franck Sabatié wrote:

> I have a hard time seeing how to best implement the following : I have a
> complicated equation of 4 variables that I need evaluated with a certain
> number of digits in precision (30 or so) :
> 
> dummy = (15/4)*(1/(pow(xi,5)*(1+i-k)*(2+i-k)*(3+i-k)*(4+i-k)*(5+i-k)))*(
> ( (3*pow(3+i-k,2)-(1+i-k)*(5+i-k))*pow(xi*xi-xb,2)
> -2*(2+i-k)*(4+i-k)*(1-xi*xi)*(xb*xb-xi*xi) ) * ( pow((xb+xi)/(1+xi),3+i-k)-
> pow((xb-xi)/(1-xi),3+i-k) ) +
> 6*(3+i-k)*xi*(1-xb)*(xi*xi-xb)*(pow((xb+xi)/(1+xi),3+i-k)+
> pow((xb-xi)/(1-xi),3+i-k)) );
> 
> 
> where the variables are i, k, xb and xi. Although most of the equation is
> relatively easy to implement in CLN, I have a hard time figuring out how to
> deal with the "pow".

Replace pow(xi, 5) with expt(xi, 5), and so on.

> There appears to be no such exponant function for floats ?

cl_R expt(const cl_R&, const cl_I&);
cl_N expt(const cl_N&, const cl_I&);
cl_N expt(const cl_N&, const cl_N&);

> Also, it is still unclear to me how to enter this equation painlessly
> into my code. Do I have to put cl_float(1,30) when I use 1 for
> instance? (same for other numbers).

That depends. For instance, it's not necessary to convert (integer) exponents
to floating-point numbers. And it's necessary to do something about (15/4),
i.e. convert it either to floating-point number (cl_float(15, xi)/4),
or rational one (cl_RA(15)/4).

BTW, 30 in cl_float(1, 30) stands for the number of *binary* digits.
I guess this is not what you want. Perhaps you mean cl_float(1, xi),
that is, the floating-point number 1 of the same precision as xi.

> Should I evaluate this in floating precision or with "real numbers" ?

Either leave them as exact numbers (integers or rationals), or convert
them as 

cl_float(integer_coefficient, precision)

Converting to hardware double/float is pointless, as CLN will convert
the everything else in your equation to the same precision (this is
definitely NOT what you want).
Also, don't bother to convert integer exponents, leave them as is.
For instance,

cl_R dummy(const cl_F& xi, int i, int k)
{
	cl_R ret = cl_float(15, xi)/4*(expt(xi, 5)*cl_I(1 + i - k)*cl_I(2 + i - k));
	return ret;
}

> And finally how do I evaluate expressions such as (something)^(something else)
> with the (something else) NOT being an
> integer.

Use the expt function.

Best regards,
	Alexei



More information about the CLN-list mailing list