Charles Greathouse on Wed, 24 Apr 2013 06:40:37 +0200


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

Re: Surprising behaviour of sqrt() on squares of integers


Or even:

mysqrt(x)=if(issquare(x,&x),x,sqrt(x))

Charles Greathouse
Analyst/Programmer
Case Western Reserve University


On Tue, Apr 23, 2013 at 7:41 PM, Jack Brennen <jfb@brennen.net> wrote:
By the way, if you want a function that returns an integer when possible
and a real when not possible, you can roll your own in one line (assumes
the input is a non-negative INT or a REAL):

? mysqrt(x) = if(issquare(x),sqrtint(x),sqrt(x))
? for(x=0,10,print([x,mysqrt(x)]))
[0, 0]
[1, 1]
[2, 1.4142135623730950488016887242096980786]
[3, 1.7320508075688772935274463415058723670]
[4, 2]
[5, 2.2360679774997896964091736687312762354]
[6, 2.4494897427831780981972840747058913920]
[7, 2.6457513110645905905016157536392604257]
[8, 2.8284271247461900976033774484193961571]
[9, 3]
[10, 3.1622776601683793319988935444327185337]



On 4/23/2013 4:37 PM, Jack Brennen wrote:
I thought that the behavior was that sqrt() of a non-negative integer or
real returns a real, and sqrtint() of a non-negative integer
returns an integer.  Both of your examples work fine with sqrtint():

? Mod(5,sqrtint(9))
Mod(2, 3)
? sqrtint(2^256)
340282366920938463463374607431768211456
? 2^128
340282366920938463463374607431768211456



On 4/23/2013 4:31 PM, Richard in Reading wrote:
This is just a toy example but there are other real situation where
it's annoying that PARI doesn't recognize when you want to take a
square root of the square of an integer and return an integer.

Is there some justification for this or is it worth me reporting it as
a bug.

  ? Mod(5,sqrt(9))
   ***   at top-level: Mod(5,sqrt(denominat
   ***                 ^--------------------
   *** Mod: forbidden division t_INT % t_REAL.
   ***   Break loop: type 'break' to go back to GP

? sqrt(2^256)
%17 = 3.4028236692093846346337460743176821146 E38
? 2^128
%18 = 340282366920938463463374607431768211456

The above should return the same value in my opinion.

Richard Heylen