def power, problem when raising power to decimals

S

skanemupp

how do i solve power(5,1.3)?

def power(nbr, po):
if po==0:
return 1
if po>0:
return nbr*power(nbr, po-1)
if po<0:
return 1/power(nbr, -1*po)


also i found a link which states 0^0 isnt 1 even though every
calculator ive tried says it is.
it doesnt say what it is but i presume 0 then.
but it seems the dude is wrong and it is 1?


dont run the code with decimals, it will never leave the function, u
have to restart the shell(if using the standard python ide)
 
M

Mark Dickinson

how do i solve power(5,1.3)?
[...]

also i found a link which states 0^0 isnt 1 even though every
calculator ive tried says it is.
it doesnt say what it is but i presume 0 then.
but it seems the dude is wrong and it is 1?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/decimal.py", line 1755, in __pow__
return context._raise_error(InvalidOperation, '0 ** 0')
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/decimal.py", line 2325, in _raise_error
raise error, explanation
decimal.InvalidOperation: 0 ** 0

Most mathematicians consider 0**0 to be either 1 or undefined. Which
answer you get depends on who you ask (or in Python, whether you're
working with floats or Decimals, as you see above).

Mark
 
J

John Machin

how do i solve power(5,1.3)?

Is this a trick question? OK, I'll bite:
def power(nbr, po):
if po==0:
return 1
if po>0:
return nbr*power(nbr, po-1)
if po<0:
return 1/power(nbr, -1*po)


also i found a link which states 0^0 isnt 1 even though every
calculator ive tried says it is.
it doesnt say what it is but i presume 0 then.
but it seems the dude is wrong and it is 1?

Of the possible results of 0 ** 0 (i.e. 1, 0, and NaN), 1 seems to be
the least implausible. It allows X ** 0 to be 1 for all X.
dont run the code with decimals, it will never leave the function, u
have to restart the shell(if using the standard python ide)

I presume that by "decimals", you mean "numbers that are not integers".

So you've got it into an infinite loop. Have you tried tracing through
the first 5 or 6 gyrations? This can be done by
(a) putting a debugger breakpoint just after the start of the function
(b) using something like:
print "power(%.6f, %.6f)" % (nbr, po))
junk = raw_input("Press <Enter> to continue -> ")
(c) using a pencil and a piece of scrap paper, write down what is
happening as a typical function call is executed e.g.

power(5, 1.3) => 5 * power(5, 0.3)
power(5, 0.3) => 5 * power(5, -0.7)
power(5, -0.7) => 1 / power (5, 0.7)
etc

Then work out what extra condition you would have to test to stop it
doing that. Then work out how to calculate the return value when that
condition is true.

HTH,
John
 
G

Gabriel Genellina

(e-mail address removed) wrote:

Of the possible results of 0 ** 0 (i.e. 1, 0, and NaN), 1 seems to be
the least implausible. It allows X ** 0 to be 1 for all X.

But a result of 0 would allow 0 ** X to be 0 for all X. (btw, this is the
reason lim(x**x) for x->0 does not exist)
 
M

Mensanator

But a result of 0 would allow 0 ** X to be 0 for all X. (btw, this is the  
reason lim(x**x) for x->0 does not exist)

Where this has come up in my research, X**0 being a multiplicative
identity is far more important than 0**X being an additive identity.
 
T

Terry Reedy

how do i solve power(5,1.3)?
[...]

also i found a link which states 0^0 isnt 1 even though every
calculator ive tried says it is.
it doesnt say what it is but i presume 0 then.
but it seems the dude is wrong and it is 1?

Define a**b as 1 multiplied by a b times. Then a**0 is clearly 1,
regardless of a.

But some do disagree.

| decimal.InvalidOperation: 0 ** 0

I would think of this as a bug unless the standard Decimal follows demands
this.

tjr
 
C

colas.francis

But a result of 0 would allow 0 ** X to be 0 for all X. (btw, this is the
reason lim(x**x) for x->0 does not exist)

lim(x**x) for x->0+ is well defined, exists and equals 1. [1]
As x**x is continuous in 0+, it is widely customary to have: 0**0:=1

[1] Recall that x**x := exp(x*log(x))
The limit of x*log(x) for x->0 is 0 [2] therefore lim(x**x) for x->0
is 1.


[2] Let y = 1/x; x*log(x)= -log(y)/y and the limit of log(y)/y for y->
+inf is 0.
 
M

Mark Dickinson

| decimal.InvalidOperation: 0 ** 0

I would think of this as a bug unless the standard Decimal follows demands
this.

It does. From http://www2.hursley.ibm.com/decimal/daops.html#refpower
:

"If both operands are zero, or if the left-hand operand is less than
zero and the right-hand operand does not have an integral value[7] or
is infinite, an Invalid operation condition is raised, the result is
[0,qNaN], and the following rules do not apply."

I'm hoping that this will change with the next update of the standard.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top