4.7 math Module Functions

  • The math module defines functions for performing various common mathematical calculations.
In [1]:
import math
In [2]:
math.sqrt(900)
Out[2]:
30.0
In [3]:
math.fabs(-10)
Out[3]:
10.0
Function           Description Example                       
ceil(x) Rounds x to the smallest integer not less than x ceil(9.2) is 10.0, ceil(-9.8) is -9.0
floor(x) Rounds x to the largest integer not greater than x floor(9.2) is 9.0, floor(-9.8) is -10.0
sin(x) Trigonometric sine of x (x in radians) sin(0.0) is 0.0
cos(x) Trigonometric cosine of x (x in radians) cos(0.0) is 1.0
tan(x) Trigonometric tangent of x (x in radians) tan(0.0) is 0.0
exp(x) Exponential function ex exp(1.0) is 2.718282, exp(2.0) is 7.389056
log(x) Natural logarithm of x (base e) log(2.718282) is 1.0, log(7.389056) is 2.0
log10(x) Logarithm of x (base 10) log10(10.0) is 1.0, log10(100.0) is 2.0
pow(x, y) x raised to power y (xy) pow(2.0, 7.0) is 128.0, pow(9.0, .5) is 3.0
sqrt(x) square root of x sqrt(900.0) is 30.0, sqrt(9.0) is 3.0
fabs(x) Absolute value of x—always returns a float. Python also has the built-in function abs, which returns an int or a float, based on its argument. fabs(5.1) is 5.1, fabs(-5.1) is 5.1
fmod(x, y) Remainder of x/y as a floating-point number fmod(9.8, 4.0) is 1.8

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 4 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.