473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

def power, problem when raising power to decimals

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)
Jun 27 '08 #1
8 3073
On Apr 16, 4:19*pm, skanem...@yahoo.se wrote:
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?
>>5**1.3
8.1032829834638136
>>0**0
1
>>0.**0.
1.0
>>from decimal import Decimal
Decimal(0)**Decimal(0)
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
Jun 27 '08 #2
sk*******@yahoo.se wrote:
how do i solve power(5,1.3)?
Is this a trick question? OK, I'll bite:
>>5 ** 1.3
8.1032829834638136
>>>
>
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 <Enterto 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

Jun 27 '08 #3
En Wed, 16 Apr 2008 19:21:18 -0300, John Machin <sj******@lexicon.net>
escribió:
sk*******@yahoo.se wrote:
>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.
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)

--
Gabriel Genellina

Jun 27 '08 #4
On Apr 16, 5:49*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 16 Apr 2008 19:21:18 -0300, John Machin <sjmac...@lexicon.net*
escribió:
skanem...@yahoo.se wrote:
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.

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.

>
--
Gabriel Genellina
Jun 27 '08 #5

"Mark Dickinson" <di******@gmail.comwrote in message
news:fc**********************************@y18g2000 pre.googlegroups.com...
On Apr 16, 4:19 pm, skanem...@yahoo.se wrote:
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

Jun 27 '08 #6
actually that 0**0 statement was wrong. 0**0 = 1 and should be.
Jun 27 '08 #7
On 17 avr, 00:49, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
En Wed, 16 Apr 2008 19:21:18 -0300, John Machin <sjmac...@lexicon.net>
escribió:
skanem...@yahoo.se wrote:
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.

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.
Jun 27 '08 #8
On Apr 16, 11:03*pm, "Terry Reedy" <tjre...@udel.eduwrote:
| 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
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andreas Emmert | last post by:
Hi there, I'm trying to import a csv file into my MySQL database. Unfortunately the number format for the price column is formatted in German style, i.e. XX,XX (decimals separated by comma not...
3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
6
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
3
by: cody | last post by:
I want to do some validation of data field something like the following: System.Diagnostics.Debug.Assert(decimal.Truncate(this.GesamtPreis*100)==this ..GesamtPreis*100); so that Iam sure that...
2
by: westjon64 | last post by:
i am fairly new to c++, i need to know how to make it so you can input decimals into a file using the cin >> a command instead of just being able to input whole numbers and also how to make it so...
5
by: Dennis Myrén | last post by:
Hi. Is there a way to make sure that float, double and decimal data types never will be presented in a scientific notation? I have tried to round(Math.Round) float's to 7 decimals, double's to...
20
by: ravi | last post by:
Give a one-line C expression to test whether a number is a power of 2.
3
by: gingerphil | last post by:
I am having problems raising a matrix to a negative power. I am relatively new to Ruby, so please forgive me if I have missed something obvious. The software I am using is basically a GIS-type...
8
by: =?Utf-8?B?a2FyaW0=?= | last post by:
Hello All, why is this code is not showing the result in this format: 0.00 and showing it as only 0 Private Sub btn1_Click Debug.Print(Format$(Rnd() * 100, "0.00")) Dim d As Double =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.