Math Functions in Python
As you learn more about python, you might decide to create a scientific calculator for a project or anything. For that, along with simple mathematical operations, you will have to evaluate some complex mathematical operations as well, like trigonometric operations, logarithmic operations etc. Forget about the calculator, there can be various situations where you might need these functions. Like a software for civil engineers to calculate various parameters of any structure that they are building, or any aerospace software – where they need various kinds of calculations about satellite trajectory, shuttle trajectory and what not. In a nutshell, the complex mathematical operations are used in various real life programs and software, hence you must know about them.
A Function can be described as a piece of code that may or may not take some value(s) as input, process it, and then finally may or may not return any value as output.
As you can see in the figure above, here input x is given to a function f and it is giving some value f(x) as the output. Although in general programming world, depending upon the purpose of the function, input and output are completely optional. But for a mathematical function, it’s very important to have both.
For example, in the trigonometric function sin(x), there must be some value of x in order to calculate and return the answer, and that pretty much establish why mathematical functions have both input and output.
In python, there are two types of pre-defined functions:
• Inbuilt functions: These are the functions which don’t require any other (external) code file (known as, Modules or Library Files). These are a part of the python core and are just built within the Python compiler hence there is no hassle of importing these modules/libraries in our code.
• The second type of functions requires some external files (modules) in order to be used. The process of using these external files in our code is called importing. So all we have to do is import the file into our code and use the functions which are already written in that file.
It’s time to try some of the functions. Let’s begin with power functions.
Power – pow(x, y)
Just consider this one as an alternative way to calculate power.
Since this one is an inbuilt function, you don’t need to import any other library files (or modules), hence it’s pretty easy to implement. Since power function will need two numbers (inputs) to perform the operation, i.e. base and exponent, hence we will have to provide two numbers to the function. Go ahead, open the IDLE and write:
>>> pow (3, 2)
Once the python compiler has ensured that all the syntax (the grammar of programming) is correct, it will look for the implementation of the function pow and use it to find 32. So as you might have expected, the output would be 9
Absolute – abs(x)
Absolute function, also known as Modulus (not to be confused with Modulo), returns the non-negative value of the argument value. Therefore, absolute value of any non-negative number is the same, while for negative numbers, their positive value is returned.
Example: absolute value of -3 will be 3; absolute value of -8.74 will be 8.74 and so on.
Sine – sin(x)
Since we know sine is a trigonometric function, hence it accepts only one value as an argument, i.e. x. Here x should be in radians, so you better not confuse it with a degree. As we mentioned before we won’t be able to use this function directly. If you do, you might get an error, something like this, which will say name sin is not defined. As you can see after math.sin (3.14159) statement, the answer returned was something like 2.653589335273e-6, it might seem a little messy but it is an equivalent representation of 2.653589335273 × 10^-6, or 0.000002653589335273. Because of approximation in the value of π, the answer too just got deviated a little bit from 0, but you can see the value is almost zero. And at the same time, you can see how accurate the results are.