Using the math module's functions and constants
Python's math
module is built in; therefore, it is always available for import. The mathematical functions contained within it are defined by the C standard, so if something doesn't work, blame the C developers.
Complex numbers are handled by a separate module (cmath
), so the math module can only be used with integers and floating point numbers. This was done on purpose, as dealing with complex numbers requires more effort than most people need for general functions. Unless otherwise indicated, all math arguments can be integers or floats.
How to do it...
- The
ceil(x)
 function returns the smallest integer>= x
. Normal mathematical rounding is not used, soÂ12.3
will be rounded up to13
, rather than rounding up starting at12.5
; any value greater thanx.0
will be rounded up to the next value, as shown in the following screenshot:
- The
copysign(x, y)
 function returns a float value with an absolute value ofx
but with the sign ofÂy
. If the OS supports...