[SciPy-user] Help !!!!

Pearu Peterson pearu at scipy.org
Mon Jun 7 13:38:35 CDT 2004



On Mon, 7 Jun 2004, José A Martín H wrote:

> Why Mr Anderson Why ?????????.
> 
> Please need help
> Can some one explain me what is going on in this two functions ?
> why they are not equal ?
> -----------------------------------------------------------------------
> def Resta(B,N):
>     X=3**(N-1)
>         result=0
>     
>     for i in range(10):
>         result+= (2**i) * X /(3**i)
>     
>     return (3**N)-result
> --------------------------------------------------------------------
> def RestaX(B,N):
>     X=3**(N-1)
>         result=0
>     
>     for i in range(10):
>         result+=  X * ((2**i)/(3**i))
>         
>     
>     return (3**N)-result
> ----------------------------------------------------------------------------
> 
> This is math.
> 
> Then sum( X * a) =  X * sum(a) being X a constant , is it true ????
> 
> Why in python is it not true ?

The above functions do *integer* math and then a*(b/c)==(a*b)/c holds only 
in special cases (when c divides b). If you would write

   result+= float(2**i) * X /(3**i)
or
   result+=  X * (float(2**i)/(3**i))

then the two functions should be (approximately) equal.

Pearu



More information about the SciPy-user mailing list