Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python math module

Tags:

Whenever I try to use any of the built-in functions of Python's exponentiation and logarithms module, I get an error like this:

NameError: name 'sqrt' is not defined 

I have tried using math.sqrt(4),sqrt(4) and sqrt(4.0), but none of them work. The exception is pow, which works as it's supposed to. This is really strange and I'm not sure what's wrong.

like image 359
user1126849 Avatar asked Jan 09 '12 02:01

user1126849


People also ask

What are Python math modules?

What is math module in Python? The math module is a standard module in Python and is always available. To use mathematical functions under this module, you have to import the module using import math . It gives access to the underlying C library functions.

How do I install Python math module?

As I mentioned earlier, the math module comes packaged with the standard Python installation. So, it is a built-in Python module, and to use it you just need to import it. Printing the type() of math will tell you that it is a module. dir() will give you all available attributes and methods available in math .

Where is the math module in Python?

These modules are not written in Python but in C. You can find them (at least on linux) in a subfolder of the lib-folder called lib-dynload. The math module is then in a file math.cpython-33m.so (on windows probably with .

Is math built-in module in Python?

Python has a built-in module that you can use for mathematical tasks. The math module has a set of methods and constants.


1 Answers

pow is built into the language(not part of the math library). The problem is that you haven't imported math.

Try this:

import math math.sqrt(4) 
like image 149
dave Avatar answered Oct 08 '22 18:10

dave