[Tutor] Little problem with math module
Tiago Katcipis
katcipis at inf.ufsc.br
Mon Apr 21 20:25:08 CEST 2008
i will change the subject but this one is interessenting.
i have this test code
import funcoes
import bissecao
import falsa_posicao
import falsa_posicaom
import newton_simples
import math
INTERVALO = [0,1]
ERRO_DET = math.pow(10, -16)
ITER = funcoes.calcular_num_iteracoes(INTERVALO[0], INTERVALO[1], ERRO_DET)
print 'Por bissecao: '
print 'Numero de iteracoes necessarios = ', ITER
resultado, erro = bissecao.obter_alpha(INTERVALO,
funcoes.convergente_simples, ERRO_DET, ITER)
print 'Alpha = ', resultado
print 'Erro = ', erro
print ''
and i have this function on bissecao
import math
import funcoes
def obter_alpha(intervalo, funcao, erro_passado = 0.0, limite = 20):
a = intervalo[0]
b = intervalo[1]
fa = funcao(a)
fb = funcao(b)
erro = erro_passado * 10.0
contador = 0
xm = 0.0
if( (fa * fb) < 0 ):
while(contador < limite):
contador += 1
xm = (a + b) / 2.0
fm = funcao(xm)
if(fm == 0):
return xm, 0.0
if( (fm * fa) < 0.0):
b = xm
else:
a = xm
erro = funcoes.calcular_erro(a, b)
if(erro < erro_passado):
return xm, erro
print 'Iteracao ', contador, ' alpha = ', xm
print 'Erro ', contador, ' = ', erro
return xm, erro
else:
print 'Funcao nao eh continua'
my problem is, INSIDE the funcion...the variable erro is correct, but
when i return it to the test...and the test prints it....comes out 0.0.
Its disturbing...i didnt found a way of solving this.
the out of the test is like this
Erro 50 = 1.50914019446e-15
Iteracao 51 alpha = 0.588532743982
Erro 51 = 7.54570097231e-16
Alpha = 0.588532743982
Erro = 0.0
it would print more things but it is desnecessary, inside the function
erro has a value like 7.54570097231e-16, but when it is returned it goes
out like 0.0. What can i do to prevent this from happening?
the whole thing is at
https://svn.inf.ufsc.br/katcipis/python/trunk/Funcoes/src/
just log as user "guest" without a password
ALAN GAULD escreveu:
> On Mon, Apr 21, 2008 at 12:07 AM, Alan Gauld
> <alan.gauld at btinternet.com <mailto:alan.gauld at btinternet.com>> wrote:
>
>
> >>> pow(-20, 0.3333333)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: negative number cannot be raised to a fractional power
> >>> -20**0.3333333
> -2.7144173455393048
> >>>
>
> I think you're confusing the order of operations.
>
> math.pow(-20, (1.0/3.0)) and -20**(1.0/3.0) are not equivalent
>
> Whereas, as john mentioned, -20**(1.0/3.0) is actually
> -(20**(1.0/3.0)), math.pow(-20, (1.0/3.0)) is (-20)**(1.0/3.0)
>
> Yes, quite correct, I posted my reply before the others had
> posted theirs although it showed up afterwards(at least on gmane)
>
> > exponentiation has higher precedence over positive, negative.
>
> Yep, I hadn't realized that, although it does make sense when you
> think about it :-)
>
> /> Note that math.pow is unrelated to the builtin power operator /
> /> and the result of math.pow(0.0, -2.0) will vary by platform. /
>
> /This is interesting, I had assumed that pow simply called **./
> Does anyone know why they made it different?
>
> Alan G.
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list