pow() works but sqrt() not!?

S

siggi

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():
---------------------------------------------------387420489

Fine, but sqrt() fails:
-------------------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
 
T

tonisk

you forgot to import math module
3.0

if you want math functions to your current namespace use:
--
Tõnis

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
 
S

siggi

Thank you very much, Tõnis!:

*** 1 ***
you forgot to import math module

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:

What is a "namespace" and what is the difference between ">>>import math"
and ">>>from math import *" ?

Siggi


you forgot to import math module
3.0

if you want math functions to your current namespace use:
--
Tõnis

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
 
F

Fredrik Lundh

siggi said:
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>
 
S

siggi

Thank you Tõnis, both for the link and your patient explanation :)

Siggi


tonisk said:
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)
 
F

Fredrik Lundh

siggi said:
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:
1.6069380442589903e+060
1606938044258990275541962092341162602522202993782792835301376L

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>
 
S

siggi

Thanks for the explanation. I am astonished what an interpreted language is
able to do!
 
B

Boris Borcic

siggi said:
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():
---------------------------------------------------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
 
S

siggi

Thanks for that, too!

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



Boris Borcic said:
siggi said:
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():
---------------------------------------------------
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
 
C

Carl Banks

siggi said:
Now the test for module math with function pow():
---------------------------------------------------
387420489

Fine, but sqrt() fails:
-------------------
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
 
D

Dan Bishop

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:
1000012000066000220000495000792000924000792000495000220000066000012000001L
(73 significant digits, correctly ending in "000001")

math.pow uses floating-point arithmetic (even if you pass it integers),
and so has limited precision:
1000012000066000238472777842004463257260700063242258506335663988477526016
(Only the first 17 digits are correct.)

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

Gabriel Genellina

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top