473,787 Members | 2,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3104
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.1032829834638 136
>>0**0
1
>>0.**0.
1.0
>>from decimal import Decimal
Decimal(0)**D ecimal(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
python2.5/decimal.py", line 1755, in __pow__
return context._raise_ error(InvalidOp eration, '0 ** 0')
File "/System/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
python2.5/decimal.py", line 2325, in _raise_error
raise error, explanation
decimal.Invalid Operation: 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.1032829834638 136
>>>
>
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("Pres s <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******@lexic on.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.a r>
wrote:
En Wed, 16 Apr 2008 19:21:18 -0300, John Machin <sjmac...@lexic on.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******** *************** ***********@y18 g2000pre.google groups.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.Invalid Operation: 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.a rwrote:
En Wed, 16 Apr 2008 19:21:18 -0300, John Machin <sjmac...@lexic on.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.e duwrote:
| decimal.Invalid Operation: 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
8707
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 by dot). When importing this file using LOAD DATA LOCAL INFILE the decimals are cut off. I'm using column type FLOAT for the price column in my database. Is there any chance to preserve the decimals when importing the csv file into my database...
3
5649
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
4018
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
1392
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 no more than a certain number of decimals are stored, but I don't want to hardcode them I want that the user can set how many decimals are stored? I cannot use Math.Pow() since it works with double instead of decimal so I
2
2221
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 that it outputs decimals and not just rounded off whole numbers
5
17664
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 15 and decimals to 28 decimals, but that does not help. And the System.Globalization.NumberFormatInfo class does not seem to provide a such function.
20
10742
by: ravi | last post by:
Give a one-line C expression to test whether a number is a power of 2.
3
8602
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 that allows ruby coded interrogation/assignment of its input. It has a class called OtMatrix that seems to be preventing me from applying a negative power to it, by saying that it cannot be coerced to a fixnum. Here is the basic code... elas =...
8
3285
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 = Math.Round(2250.0, 3) txt2.Text = txt1.Text \ d
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.