How do I find the largest integer less than x in Python?

In this article, we will show you how to find the largest integer less than or equal to x in Python using the floor function.

The Greatest Integer Function [x] denotes the integral part of a real number x that is the closest and smallest integer to x. It's also called the floor function.

[x] = the largest integer less than or equal to x

Understanding the Floor Function

If n ? x < n+1 where n is an integer, then [x] = n. This means if x lies in the interval [n, n+1), the Greatest Integer Function of x will be n.

x [x]
0 ? x < 1 0
1 ? x < 2 1
2 ? x < 3 2

In the given table, we take the floor of the values each time. When intervals are in the form [n, n+1), the greatest integer function has the value n, where n is an integer.

Using math.floor() Method

The math.floor() method rounds a value down to the closest integer and returns the result.

Syntax

math.floor(x)

Parameters

x (required) ? the number to be rounded down

Example

The following program finds the greatest integer function value using math.floor() ?

import math

def greatest_integer(num):
    """Returns the greatest integer less than or equal to num"""
    return math.floor(num)

# Test with different numbers
numbers = [3.4, 5.2, -2.7, 4.0, -1.1]

for num in numbers:
    result = greatest_integer(num)
    print(f"Greatest integer ? {num} is: {result}")
Greatest integer ? 3.4 is: 3
Greatest integer ? 5.2 is: 5
Greatest integer ? -2.7 is: -3
Greatest integer ? 4.0 is: 4
Greatest integer ? -1.1 is: -2

Using int() Function (for Positive Numbers)

For positive numbers, the int() function can be used, but it behaves differently for negative numbers ?

def greatest_integer_int(n):
    """Returns greatest integer using int() function"""
    if n >= 0:
        return int(n)
    else:
        # For negative numbers, int() truncates towards zero
        # but floor function rounds down, so we subtract 1
        return int(n) - 1 if n != int(n) else int(n)

# Test with different numbers
test_numbers = [5.2, 0.2, -0.2, -2.7, 4.0]

for num in test_numbers:
    result = greatest_integer_int(num)
    print(f"Greatest integer ? {num} is: {result}")
Greatest integer ? 5.2 is: 5
Greatest integer ? 0.2 is: 0
Greatest integer ? -0.2 is: -1
Greatest integer ? -2.7 is: -3
Greatest integer ? 4.0 is: 4

Comparison of Methods

Method Works for Negatives? Best For
math.floor() Yes All real numbers
int() No (truncates) Positive numbers only

Key Properties

  • If x is an integer, [x] = x
  • For negative non-integers, the floor is one less than the truncated value
  • math.floor() always rounds down, while int() truncates towards zero

Conclusion

Use math.floor() for finding the greatest integer less than or equal to any real number. For positive numbers only, int() can work, but math.floor() is the standard and most reliable approach.

Updated on: 2026-03-24T20:19:29+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements