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 x in python.

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

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


Generally

If n<=X<n+1 Then,(n∈Integer) => [X] =n.This means if X lies in [n, n+1), then 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 are always taking the floor of the values each time. When the intervals are in the form [n, n+1), the greatest integer function has the value n, where n is an integer.

• 0<=x<1 always lies in the interval [0, 0.9), thus the Greatest Integer Function of X is 0.
• 1<=x<2 always lies in the interval [1, 1.9), therefore the Greatest Integer Function of X will be 1.
• 2<=x<3 always lies in the interval [2, 2.9), thus the Greatest Integer Function of X will be 2.

Properties of Greatest Integer Function

• If X is an integer, [X]=X holds true.
• [X+Y]>=[X]+[Y], which indicates that the greatest integer of the sum of X and Y is equal to the GIF of X and GIF of Y.
• If [f(X)]>=I, then f(X) >= I.
• If [f(X)]<=I, then f(X) < I+1.
• [-X]= -[X], If X belongs to Integer.
• [-X]=-[X]-1, If X is not an Integer.

It is often referred to as the stepwise function or the floor of X.

Greatest Integer Function using math.floor() method

The math.floor() method rounds a value DOWN to the closest integer, if necessary, and gives the result.

The math module in Python offers a number of mathematical operations that can be easily done using the module. The math.floor() function produces the greatest integer not greater than x. If the number is already an integer, it returns the same number.

Syntax

math.floor(x)


Parameters

x(required) − the number to be rounded down

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Use the import keyword to import the math module.

• Create a function GreatestInteger() that returns the greatest integer function value of a number by taking the number as an argument.

• Use the floor() function() of the math module to get the floor of a number and convert it to an integer using the int() function(returns an integer from a given object) which is the greatest integer function of a number and return it using return statement.

• Create a variable to store the input number.

• Call the GreatestInteger() function by passing the input number as an argument and print the resultant greatest integer function value of a number returned by it.

Example

The following program returns the greatest integer function value of a number using the math.floor() function −

# importing a math module
import math

# creating a function that calculates the
# greatest integer function value of a number passed
def GreatestInteger(num):

# getting the floor of a number and converting to an integer

# which is the greatest integer function of a number
return int(math.floor(num))

# inpur number
inputNumber = 3.4

# calling the GreatestInteger() function by passing input
# number as an argument
print('The Greatest Integer Less than',inputNumber,'is:',GreatestInteger(inputNumber))


Output

On executing, the above program will generate the following output −

The Greatest Integer Less than 3.4 is: 3


Greatest Integer Function using int() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Check if the number passed is greater than 0 and if it is true then Return the integer format of a number passed using the int() function(returns an integer from a given object). This returns the largest number less than the given argument.

• Else return integer format of the number -1.

• Call the GreatestInteger() function by passing a number as an argument and print the resultant greatest integer function value of a number returned by it.

• Similarly find for the other numbers and observe the changes.

Example

The following program returns the greatest integer function value of a number using the int() function −

# creating a function that returns by taking the given number
# as an argument
def GreatestInteger(n):

# Checking if the number is greater than 0
if(n>=0):
# Return the integer format of the given number
return int(n)
# Else if it is a negative number
# Return the integer format -1
return int(n) -1

# calling the GreatestInteger() function by passing given number as an argument
print('The Greatest Integer Less than 5.2 is:',GreatestInteger(5.2))
print('The Greatest Integer Less than 0.2 is:',GreatestInteger(0.2))

# Negative Number
print('The Greatest Integer Less than 0.2 is:',GreatestInteger(-0.2))


Output

On executing, the above program will generate the following output −

The Greatest Integer Less than 5.2 is: 5
The Greatest Integer Less than 0.2 is: 0
The Greatest Integer Less than 0.2 is: None


Conclusion

In this article, we learned two different Python methods for calculating the largest integer less than x. We also learned how to use the int() function to convert a given number to an integer. In this article, we learned about the greatest Integer Function in depth. We also learned about the floor() function, which is used to calculate the same thing.