473,387 Members | 1,542 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,387 software developers and data experts.

pow() works but sqrt() not!?

Hi all,

this is a newbie question on :
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on
win32
PC with WinXP

In
http://www.python.org/doc/2.3.5/lib/module-math.html
I read:

"sqrt( x) Return the square root of x."

Now the test for module math with function pow():
---------------------------------------------------
>>pow(9,9)
387420489

Fine, but sqrt() fails:
-------------------
>>sqrt(9)
I get this error message

'Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
sqrt(9)
NameError: name 'sqrt' is not defined'

Same for sin() and cos(). ">>Import math" does not help. Will I have to
define the sqrt() function first? If so, how?

Please help!

Thank you,

Siggi




Jan 4 '07 #1
13 4932
you forgot to import math module
>>import math
math.sqrt(9)
3.0

if you want math functions to your current namespace use:
>>from math import *
--
Tõnis

On Jan 4, 10:13 am, "siggi" <smusnmrNOS...@yahoo.comwrote:
Hi all,

this is a newbie question on :
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]on
win32
PC with WinXP

Inhttp://www.python.org/doc/2.3.5/lib/module-math.html
I read:

"sqrt( x) Return the square root of x."

Now the test for module math with function pow():
--------------------------------------------------->>pow(9,9)387420489

Fine, but sqrt() fails:
------------------->>sqrt(9)I get this error message

'Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
sqrt(9)
NameError: name 'sqrt' is not defined'

Same for sin() and cos(). ">>Import math" does not help. Will I have to
define the sqrt() function first? If so, how?

Please help!

Thank you,

Siggi
Jan 4 '07 #2
Thank you very much, Tõnis!:

*** 1 ***
>you forgot to import math module
>>import math
Nope, I did not! But I used sqrt(9), and not math.sqrt(9). The latter works
excellent, thank you! From now on, I will use "import math" and
"math.fuction()" for EVERY mathematical function, even for pow() etc. just
to be on the safe side!

*** 2 ***
>if you want math functions to your current namespace use:
>>from math import *
What is a "namespace" and what is the difference between ">>>import math"
and ">>>from math import *" ?

Siggi
"tonisk" <me*******@gmail.comschrieb im Newsbeitrag
news:11*********************@31g2000cwt.googlegrou ps.com...
you forgot to import math module
>>import math
math.sqrt(9)
3.0

if you want math functions to your current namespace use:
>>from math import *
--
Tõnis

On Jan 4, 10:13 am, "siggi" <smusnmrNOS...@yahoo.comwrote:
Hi all,

this is a newbie question on :
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
on
win32
PC with WinXP

Inhttp://www.python.org/doc/2.3.5/lib/module-math.html
I read:

"sqrt( x) Return the square root of x."

Now the test for module math with function pow():
--------------------------------------------------->>pow(9,9)387420489

Fine, but sqrt() fails:
------------------->>sqrt(9)I get this error message

'Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
sqrt(9)
NameError: name 'sqrt' is not defined'

Same for sin() and cos(). ">>Import math" does not help. Will I have to
define the sqrt() function first? If so, how?

Please help!

Thank you,

Siggi

Jan 4 '07 #3
siggi wrote:
What is a "namespace" and what is the difference between ">>>import math"
and ">>>from math import *" ?
http://preview.tinyurl.com/t4pxq

for more on this, *please* read the relevant sections in the tutorial.
Python's all about namespaces, and trial and error is not a very good
way to figure how they work.

</F>

Jan 4 '07 #4
if you want math functions to your current namespace use:
>from math import *What is a "namespace" and what is the difference between ">>>import math"
and ">>>from math import *" ?
for namespaces read this
http://www.network-theory.co.uk/docs/pytut/tut_68.html

import math creates new namespace "math" for names in that module, so
you can acess them by prefixing them with "math", like math.sqrt(9).
from math import * imports all names to your local scope, so you do not
have to prefix them, sqrt(9)

--
Tõnis

Jan 4 '07 #5
Thank you Tõnis, both for the link and your patient explanation :-)

Siggi
"tonisk" <me*******@gmail.comschrieb im Newsbeitrag
news:11**********************@51g2000cwl.googlegro ups.com...
if you want math functions to your current namespace use:
>from math import *What is a "namespace" and what is the difference
between ">>>import math"
and ">>>from math import *" ?
for namespaces read this
http://www.network-theory.co.uk/docs/pytut/tut_68.html

import math creates new namespace "math" for names in that module, so
you can acess them by prefixing them with "math", like math.sqrt(9).
from math import * imports all names to your local scope, so you do not
have to prefix them, sqrt(9)

--
Tõnis
Jan 4 '07 #6
"siggi" wrote:
Nope, I did not! But I used sqrt(9), and not math.sqrt(9). The latter works
excellent, thank you! From now on, I will use "import math" and
"math.fuction()" for EVERY mathematical function, even for pow() etc. just
to be on the safe side!
pow and math.pow are two slightly different things, though. pow() works on
any type that supports power-of operations (via the __pow__ hook), while
math.pow treats everything as a 64-bit float:
>>math.pow(2, 200)
1.6069380442589903e+060
>>pow(2, 200)
16069380442589902755419620923411626025222029937827 92835301376L

pow also takes a third modulo argument (pow(x,y,z) is equivalent to pow(x,y) % z,
but can be implemented more efficiently for certain data types).

</F>

Jan 4 '07 #7
Thanks for the explanation. I am astonished what an interpreted language is
able to do!

"Fredrik Lundh" <fr*****@pythonware.comschrieb im Newsbeitrag
news:ma***************************************@pyt hon.org...
"siggi" wrote:
>Nope, I did not! But I used sqrt(9), and not math.sqrt(9). The latter
works
excellent, thank you! From now on, I will use "import math" and
"math.fuction()" for EVERY mathematical function, even for pow() etc.
just
to be on the safe side!

pow and math.pow are two slightly different things, though. pow() works
on
any type that supports power-of operations (via the __pow__ hook), while
math.pow treats everything as a 64-bit float:
>>>math.pow(2, 200)
1.6069380442589903e+060
>>>pow(2, 200)
16069380442589902755419620923411626025222029937827 92835301376L

pow also takes a third modulo argument (pow(x,y,z) is equivalent to
pow(x,y) % z,
but can be implemented more efficiently for certain data types).

</F>


Jan 4 '07 #8
siggi wrote:
Hi all,

this is a newbie question on :
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on
win32
PC with WinXP

In
http://www.python.org/doc/2.3.5/lib/module-math.html
I read:

"sqrt( x) Return the square root of x."

Now the test for module math with function pow():
---------------------------------------------------
>>>pow(9,9)
387420489

Fine, but sqrt() fails:
-------------------
BTW note that (of course) you can write pow(x,.5) or x**.5 for sqrt(x)
without any preliminary import statement
Jan 4 '07 #9
Thanks for that, too!

Would be interesting to learn how these different algorithms influence the
precision of the result!?

"Boris Borcic" <bb*****@gmail.comschrieb im Newsbeitrag
news:45********@news.bluewin.ch...
siggi wrote:
>Hi all,

this is a newbie question on :
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
on
win32
PC with WinXP

In
http://www.python.org/doc/2.3.5/lib/module-math.html
I read:

"sqrt( x) Return the square root of x."

Now the test for module math with function pow():
---------------------------------------------------
>>>>pow(9,9)
387420489

Fine, but sqrt() fails:
-------------------

BTW note that (of course) you can write pow(x,.5) or x**.5 for sqrt(x)
without any preliminary import statement

Jan 4 '07 #10
siggi wrote:
Now the test for module math with function pow():
---------------------------------------------------
>pow(9,9)
387420489

Fine, but sqrt() fails:
-------------------
>sqrt(9)
I get this error message

'Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
sqrt(9)
NameError: name 'sqrt' is not defined'
The third argument to the builtin pow is a special usage for
cryptography, and something specific like that needs no representation
in the builtin namespace. The ** operator covers all other uses.
Carl Banks

Jan 4 '07 #11
On Jan 4, 10:00 am, "siggi" <smusnmrNOS...@yahoo.comwrote:
Thanks for that, too!

Would be interesting to learn how these different algorithms [for pow] influence the
precision of the result!?
For an integer (i.e., int or long) x and a nonnegative integer y, x**y
is exact:
>>1000001 ** 12
10000120000660002200004950007920009240007920004950 00220000066000012000001L
(73 significant digits, correctly ending in "000001")

math.pow uses floating-point arithmetic (even if you pass it integers),
and so has limited precision:
>>print '%.73f' % math.pow(1000001, 12)
10000120000660002384727778420044632572607000632422 58506335663988477526016
(Only the first 17 digits are correct.)

For floats, the ** operator does the same thing math.pow does.

Jan 5 '07 #12
At Thursday 4/1/2007 10:12, siggi wrote:
>Thanks for the explanation. I am astonished what an interpreted language is
able to do!
Python is as interpreted as Java. Its numerical capabilities are more
or less independent of this fact, I'd say.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 5 '07 #13
On 2007-01-05, Gabriel Genellina <ga******@yahoo.com.arwrote:
At Thursday 4/1/2007 10:12, siggi wrote:
>>Thanks for the explanation. I am astonished what an interpreted
language is able to do!

Python is as interpreted as Java.
But it's far more interactive -- at least when compared with my
limited experience with Java.
Its numerical capabilities are more or less independent of
this fact, I'd say.
--
Grant Edwards grante Yow! What PROGRAM are
at they watching?
visi.com
Jan 5 '07 #14

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

Similar topics

5
by: Magix | last post by:
#include <math.h> double pow( double base, double exp ); will pow (2, -30) works fine ? (exp is negative value). If not, what are the workaround for exp to be negative?
13
by: Shaobo Hou | last post by:
Can anyone tell me why pow(-8.0, 1.0 / 3.0) (cubic root of -8) returns nan (in linux) and negative infinity or something (in devcpp in windows), instead of -2? The problem seems to be that pow...
52
by: Michel Rouzic | last post by:
I obtain an unwanted behavior from the pow() function : when performing pow(2, 0.5), i obtain 1.414214 when performing pow(2, 1/2), i obtain 1.000000 when performing a=0.5; pow(2, a), i obtain...
13
by: Michael McNeil Forbes | last post by:
I would like to write a module that provides some mathematical functions on top of those defined in math, cmath etc. but I would like to make it work with "any" type that overloads the math...
11
by: Russ | last post by:
I have a couple of questions for the number crunchers out there: Does "pow(x,2)" simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)?...
42
by: John Smith | last post by:
In a C program I need to do exponentiation where the base is negative and the exponent is a fraction. In standard C this would be something like t = pow(-11.5, .333), but with this combination of...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
18
by: Peng Yu | last post by:
Hi, I'm wondering if there is any general guideline on when to using something like std::pow(x, n) rather than x * x * x * ... * x (n x's). Thanks, Peng
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.