Karim Belabas on Fri, 19 Mar 2004 16:32:09 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: sqrt(9)


* Bill Allombert [2004-03-19 16:07]:
> On Fri, Mar 19, 2004 at 01:26:59PM -0000, Jon Perry wrote:
> > I would expect 9^(1/2)=3.0000000000000, or even 3.
> 
> It give 3.000000000000000000000000000 with the CVS version.

It is specified (and documented) that 

1) "transcendental functions" return floating point results
2) sqrt is a "transcendental functions"

So "the integer 3" is not a valid answer, although arguably more "natural".
Also both 3.00000000 and 2.9999999 are valid answers (up to the working
realprecision, they in fact agree).

You may use something like this:

  nthroot(x, n) = local(y,z); y = round( z = x^(1/n) ); if (y^n == x, y, z)

Cheers,

    Karim.
-- 
Karim Belabas                     Tel: (+33) (0)1 69 15 57 48
Dep. de Mathematiques, Bat. 425   Fax: (+33) (0)1 69 15 60 19
Universite Paris-Sud              http://www.math.u-psud.fr/~belabas/ 
F-91405 Orsay (France)            http://pari.math.u-bordeaux.fr/  [PARI/GP]