nth root

Dan Goodman dg.gmane at thesamovar.net
Sat Jan 31 17:48:31 CET 2009


Mark Dickinson wrote:
> I'd also be a bit worried about accuracy.   Is it important to you
> that the
> integer part of the result is *exactly* right, or is it okay if
> (n**13)**(1./13) sometimes comes out as slightly less than n, or if
> (n**13-1)**(1./13) sometimes comes out as n?

I don't think accuracy is too big a problem here actually (at least for 
13th roots). I just tested it with several hundred thousand random 100 
digit numbers and it never made a mistake. The precision of double ought 
to easily guarantee a correct result. If you let x=int(1e100**(1./13)) 
then ((x+1)**13-x**13)/x**13=2.6e-7 so you only need around the first 8 
or 9 digits of the 100 digit number to compute the 13th root exactly 
(well within the accuracy of a double).

OTOH, suppose you were doing cube roots instead then you would need the 
first 35 digits of the 100 digit number and this is more accurate than a 
double. So for example int(1e100**(1./3)) is a long way from being the 
integer part of the true cube root (it's between 10**18 and 10**19 away).

Dan




More information about the Python-list mailing list