Python Math Functions: Rounding, Logarithms, Factorial
Working with numbers in Python is essential for both applied tasks and scientific computing. Whether you're analyzing data, building financial applications, or diving into machine learning, knowing Python's math functions will significantly streamline your development. These functions also help improve calculation accuracy.
In this article, we'll break down key mathematical operations in Python. We'll explore different ways to work with numbers and walk through practical examples of using these functions.
The math Module: Your Go-To for Mathematical Computations
Python's built-in math module provides a wide range of functions for performing mathematical calculations. It includes all the essential tools for number operations.
import math
Key Features of the math Module
The math module includes many useful functions:
- Trigonometric functions (sin, cos, tan)
- Hyperbolic functions (sinh, cosh, tanh)
- Rounding functions (floor, ceil, trunc)
- Logarithmic functions (log, log2, log10)
- Power functions (pow, sqrt)
- Mathematical constants (pi, e)
Rounding Numbers in Python
The round() Function for Basic Rounding
The round() function is used to round a number to a specified number of decimal places. It's the simplest way to round in Python.
number = 3.14159265
rounded = round(number, 2)
print(rounded) # Output: 3.14
The round() function syntax:
round(number, ndigits)
Parameters:
- number — the number to round
- ndigits — number of decimal places (default is 0)
How round() Works: Banker's Rounding
The round() function uses "banker's rounding" (round half to even). When the digit is exactly 0.5, it rounds to the nearest even number:
print(round(0.5)) # Output: 0
print(round(1.5)) # Output: 2
print(round(2.5)) # Output: 2
Directed Rounding with math.floor() and math.ceil()
The math.floor() and math.ceil() functions provide directed rounding:
- math.floor() — rounds down (toward negative infinity)
- math.ceil() — rounds up (toward positive infinity)
import math
print(math.floor(3.7)) # Output: 3
print(math.ceil(3.7)) # Output: 4
print(math.floor(-3.7)) # Output: -4
print(math.ceil(-3.7)) # Output: -3
The math.trunc() Function for Truncation
The math.trunc() function truncates the fractional part of a number without rounding:
print(math.trunc(3.7)) # Output: 3
print(math.trunc(-3.7)) # Output: -3
High-Precision Rounding with the decimal Module
For precise financial calculations, use the decimal module. It gives you full control over calculation precision and eliminates floating-point rounding errors.
from decimal import Decimal, ROUND_HALF_UP
value = Decimal('3.4567')
rounded_value = value.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
print(rounded_value) # Output: 3.46
The decimal module offers various rounding modes:
- ROUND_HALF_UP — rounds away from zero when the digit is 0.5
- ROUND_HALF_DOWN — rounds toward zero when the digit is 0.5
- ROUND_HALF_EVEN — rounds to the nearest even number
- ROUND_UP — always rounds up
- ROUND_DOWN — always rounds down