Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. math.sqrt() / math.ceil() / math.floor()

math.sqrt() / math.ceil() / math.floor()

Since: Python 2(2000)

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

FunctionDescription
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

math_sqrt_ceil_1.py
import math

# Use sqrt() to calculate square roots.
print(math.sqrt(9))
print(math.sqrt(2))
print(math.sqrt(0.25))

# 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}')

Running the code produces the following output:

python3 math_sqrt_ceil_1.py
3.0
1.4142135623730951
0.5
Error: math domain error
math_sqrt_ceil_2.py
import math

# Use ceil() to round up.
print(math.ceil(3.1))
print(math.ceil(3.9))
print(math.ceil(-3.1))

# Use floor() to round down.
print(math.floor(3.9))
print(math.floor(-3.1))

# trunc() truncates toward zero.
print(math.trunc(3.9))
print(math.trunc(-3.9))

Running the code produces the following output:

python3 math_sqrt_ceil_2.py
4
4
-3
3
-4
3
-3
math_sqrt_ceil_3.py
import math

# Use isnan() and isinf() to check for special values.
nan_val = float('nan')
inf_val = float('inf')
print(math.isnan(nan_val))
print(math.isinf(inf_val))
print(math.isinf(-inf_val))

# 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.')

Running the code produces the following output:

python3 math_sqrt_ceil_3.py
True
True
True
3 pages needed.

Common Mistakes

Common Mistake 1: Using the sqrt() result as an integer

math.sqrt() always returns a float. If you need an integer result, convert with int(). Note that for integer square roots in Python 3.8 and later, math.isqrt() is also available and avoids floating-point rounding issues.

mistake1_ng.py
import math

result = math.sqrt(9)
print(result)
print(type(result))

Running the code produces the following output:

python3 mistake1_ng.py
3.0
<class 'float'>
mistake1_ok.py
import math

result = int(math.sqrt(9))
print(result)
print(type(result))

Running the code produces the following output:

python3 mistake1_ok.py
3
<class 'int'>

Common Mistake 2: Misunderstanding ceil() and floor() for negative numbers

For negative numbers, ceil() and floor() may behave differently than expected. ceil(-3.1) returns -3 (toward zero), while floor(-3.1) returns -4 (toward negative infinity).

mistake2_ng.py
import math

print(math.floor(-3.1))

Running the code produces the following output:

python3 mistake2_ng.py
-4
mistake2_ok.py
import math

print(math.ceil(-3.1))
print(math.floor(-3.1))
print(math.trunc(-3.1))

Running the code produces the following output:

python3 mistake2_ok.py
-3
-4
-3

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 .