math.sqrt() / math.ceil() / math.floor()
Functions provided by the math module for square roots, ceiling, floor, truncation, and special value checks. Use these for precise floating-point calculations.
Syntax
import math # Calculates the square root. math.sqrt(number) # Returns the ceiling (smallest integer greater than or equal to the value). math.ceil(number) # Returns the floor (largest integer less than or equal to the value). math.floor(number) # Returns the value truncated toward zero. math.trunc(number) # Checks whether the value is NaN (Not a Number). math.isnan(number) # Checks whether the value is infinity. math.isinf(number)
Function List
| Function | Description |
|---|---|
| math.sqrt(x) | Returns the square root of x (positive square root) as a float. Raises a ValueError if x is negative. |
| math.ceil(x) | Returns the ceiling of x (the smallest integer greater than or equal to x) as an integer. |
| math.floor(x) | Returns the floor of x (the largest integer less than or equal to x) as an integer. |
| math.trunc(x) | Truncates the fractional part of x and returns the integer part. Always truncates toward zero regardless of sign. |
| math.isnan(x) | Returns True if x is NaN (Not a Number). |
| math.isinf(x) | Returns True if x is positive or negative infinity (inf). |
Sample Code
import math
# Use sqrt() to calculate square roots.
print(math.sqrt(9)) # Outputs '3.0'.
print(math.sqrt(2)) # Outputs '1.4142135623730951'.
print(math.sqrt(0.25)) # Outputs '0.5'.
# The square root of a negative number is imaginary, so it raises an error.
try:
math.sqrt(-1)
except ValueError as e:
print(f'Error: {e}') # Use the cmath module for complex square roots.
# Use ceil() to round up.
print(math.ceil(3.1)) # Outputs '4'.
print(math.ceil(3.9)) # Outputs '4'.
print(math.ceil(-3.1)) # Outputs '-3' (rounds toward positive infinity).
# Use floor() to round down.
print(math.floor(3.9)) # Outputs '3'.
print(math.floor(-3.1)) # Outputs '-4' (rounds toward negative infinity).
# trunc() truncates toward zero.
print(math.trunc(3.9)) # Outputs '3'.
print(math.trunc(-3.9)) # Outputs '-3' (differs from floor()).
# Use isnan() and isinf() to check for special values.
nan_val = float('nan')
inf_val = float('inf')
print(math.isnan(nan_val)) # Outputs 'True'.
print(math.isinf(inf_val)) # Outputs 'True'.
print(math.isinf(-inf_val)) # Outputs 'True'.
# Practical example: calculating page count (ceiling division)
total_items = 25
per_page = 10
pages = math.ceil(total_items / per_page)
print(f'{pages} pages needed.') # Outputs '3 pages needed.'
Notes
math.ceil() and math.floor() round in opposite directions. ceil() always rounds toward positive infinity, while floor() always rounds toward negative infinity. math.trunc(), on the other hand, always truncates toward zero regardless of sign. For positive numbers, floor() and trunc() produce the same result, but they differ for negative numbers.
math.sqrt() always returns a float. If you need an integer square root, convert the result with int(math.sqrt(x)). To find the square root of a negative number, use cmath.sqrt() from the complex math module.
For math constants, logarithms, and trigonometric functions, see math.pi / math.e / math.log() / math.sin().
If you find any errors or copyright issues, please contact us.