Let’s talk about some of the other mathematical functions that are available, when working with int and float types. As we’ve already seen, these types support basic mathematical operators like multiplication, division, addition, and subtraction. If you use the math module, however, there’s many other functions that become available, as well as a couple of contents. We have assigned x a value of 5.0, a float value, and Y – 10. And we can see those values printed out. Notice that the math module is imported. And a local variable, pi, is used to be assigned the value for math.pi, and the local variable e is used to assign the value of math.e – the two constants that are provided by the math module. One built-in function that was not mentioned earlier was that called round, which you can see in the output of printing out pi, without any rounding, shows a number of digits. Whereas printing out math.pi, with round (pi, 4) as parameters, rounds it to four significant digits.
x, y = 5.0, 10 print('x =', x, ',', 'y =', y) import math # The math module provides a couple of constants pi = math.pi e = math.e print('The value of pi is:',pi) print('The rounded value of pi is:', round(pi, 4)) # The float class allows creation of special numbers pos_inf = float('inf') neg_inf =float('-inf') not_a_num = float('nan') # The math module provides functions to detect these numbers print('math.isinf(pos_inf) =',math.isinf(pos_inf)) print('math.isinf(neg_inf) =', math.isinf(neg_inf)) print('math.isnan(not_a_num) =', math.isnan(not_a_num)) # Beware that these special numbers propagate with errors print('pos_inf * x =',pos_inf * x) print('neg_inf / y =', neg_inf / y) print('pos_inf +neg_inf =', pos_inf + neg_inf) print('not_a_num - y =', not_a_num - y) #A nan value is never equal to another nan value print('not_a_num ==not_a_num =', not_a_num == not_a_num) # The math module provides many other functions print('math.factorial(5) =', math.factorial(5)) #logarithmic and power functions print('math.log(x) =', math.log(x)) print('math.log10(x) =', math.log10(x)) print('math.exp(x) =',math.exp(x)) print('math.pow(x, x) =', math.pow(x, x)) print('math.sqrt(25) =', math.sqrt(25)) # trigonometric functions print('math.cos(x) =', math.cos(x)) print('math.acos(0.284) =',math.acos(0.284)) # angular conversion functions print('math.degrees(x)=', math.degrees(x)) print('math.radians(286.5) =', math.radians(286.5)) # hyperbolic functions print('math.acosh(x) =', math.acosh(x)) print('math.asinh(x) =', math.asinh(x)) The Output : x = 5.0 , y = 10 The value of pi is:3.0141592653589793 The rounded value of pi is: 3.1416 math.isinf(pos_inf) = True math.isinf(neg_inf) = True math.isnan(not_a_num) = True pos_inf * x = inf neg_inf / y = -inf pos_inf + neg_inf = nan not_a_num – y = nan not_a_num == not_a_num =False math.log(x) = 1.6094379124341003 math.log10(x) =0.6989700043360189 math.exp(x) = 148.4131591025766 math.pow(x, x) =3125.0 math.sqrt(25) = 5.0 math.cox(x) = 0.28366218546322625 math.acos(0.284) = 1.2828330039201725 math.degrees(x) =286.4788975654116 math.radians(286.5) = 5.000368305953754 math.acos(x) =2.2924316695611777 math.asinh(x) = 2.3124383412727525
The float class allows you to create special numbers to represent infinity, and negative infinity, as well as something called not a number. Within the math module, there’s functions to detect these. You can see that we can detect positive infinity and negative infinity with the math.isinf function, and math.isnan will detect something that is not a number. Beware if you use these special numbers in doing any kind of mathematical operation – like multiplication, division, addition, or subtraction – that they will propagate without any errors. So if you multiply by infinity, you get infinity. If you take infinity, or negative…if you take negative infinity, and you divide it by a number, you still get negative infinity. If you try to do something like add negative infinity and positive infinity together, you get something that is not a number. In fact, any operation involving not a number generally yields not a number. An interesting fact that is true for Python is that even a instance of not a number is not equal to itself.