Floor of x

The floor function, denoted as ( \lfloor x \rfloor ), gives the greatest integer that is less than or equal to ( x ). In simpler terms, it rounds down the number to the nearest integer.

Java

1
2
3
public static int floorOfX(double x) {
    return (int)Math.floor(x);
}

In Java, the Math class provides a built-in method called floor() which takes a double as an argument and returns the floor value as a double. We cast it to an int for integer representation.

C++

1
2
3
4
5
6
7
#include <cmath>
#include <iostream>
using namespace std;

int floorOfX(double x) {
    return (int)floor(x);
}

In C++, the <cmath> library has a function called floor() that works in a similar way to Java’s Math.floor(). It returns the floor value which we cast to an int.

Python

1
2
3
4
import math

def floor_of_x(x):
    return math.floor(x)

In Python, the math library provides a floor() function. No type casting is required as the function returns an integer.

Each implementation uses the language’s standard library to get the floor value of ( x ), rounding down to the nearest integer.