Karim BELABAS on Fri, 12 Nov 1999 20:48:21 +0100 (MET)

 Re: sqr() bug?

[Michael Somos:]
> Bill Allombert has pointed out something that I missed before. Namely:
>
> gp> 5+O(2^10)
> %1 = 1 + 2^2 + O(2^10)
> gp> %1^2
> %2 = 1 + 2^3 + 2^4 + O(2^11)
> gp> %1*%1
> %3 = 1 + 2^3 + 2^4 + O(2^11)
> gp> (5+O(2^10))*(5+O(2^10))
> %4 = 1 + 2^3 + 2^4 + O(2^10)
>
> This reminds me of a similar kind of phenomenon in interval arithmetic
> which might be closely related. Since this is obvious in hindsight, I
> may suggest that a few more sentences in the documentation could point
> out this phenomenon very explicitly. As it is now written, documentation
> seems incorrect. That is, *both* x^2 and x*x give exactly the same result
> as sqr(x). What gives a different result is the product of two p-adic
> numbers which are not identical but which have the same value. Thus :
>
> gp> x=5+O(2^10)
> %5 = 1 + 2^2 + O(2^10)
> gp> x^2
> %6 = 1 + 2^3 + 2^4 + O(2^11)
> gp> x*x
> %7 = 1 + 2^3 + 2^4 + O(2^11)
> gp> sqr(x)
> %8 = 1 + 2^3 + 2^4 + O(2^11)
> gp> y=x
> %9 = 1 + 2^2 + O(2^10)
> gp> x*y
> %10 = 1 + 2^3 + 2^4 + O(2^10)
>
> This is a subtle point which I think needs to be clearly desribed.

I modified the documentation:

sqr(x):

square of x. This operation is not completely straightforward
(i.e. identical to x * x),  since it can often be computed more efficiently.
Also, squaring a 2-adic number increases its precision. For example,

? (1 + O(2^4))^2
%1 = 1 + O(2^5)
? (1 + O(2^4)) * (1 + O(2^4))
%2 = 1 + O(2^4)

Note that this function is also called whenever one multiplies two objects
which  are  known  to  be  identical,   e.g. they  are  the value of the same
variable, or we are computing a power.

? x = (1 + O(2^4)); x * x
%3 = 1 + O(2^5)
? (1 + O(2^4))^4
%4 = 1 + O(2^6)

(note the difference between %2 and %3 above).  No higher powers receive a
special treatment.  Raising a 3-adic to the third power does not increase its
precision for instance.

The library syntax is gsqr(x).

Karim.
__
Karim Belabas                    email: Karim.Belabas@math.u-psud.fr
Dep. de Mathematiques, Bat. 425
Universite Paris-Sud             Tel: (00 33) 1 69 15 57 48
F-91405 Orsay (France)           Fax: (00 33) 1 69 15 60 19
--