Syntax of the floor() in C++
To use the floor() function in C++, you need to follow this syntax:
double floor(double x);
Here, `x` is the floating-point number you want to round down. The function returns a value of type `double`, which represents the largest integer less than or equal to `x`.
Although the parameter & return type are specified as `double`, you can also pass other floating-point types like `float` or `long double` to the floor() function. C++ will automatically convert these types to `double` before performing the operation.
For example:
#include <iostream>
#include <cmath>
int main() {
double num = 3.7;
double result = floor(num);
std::cout << "The floor of " << num << " is " << result << std::endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:
The floor of 3.7 is 3
Parameters of the floor() in C++
The floor() function in C++ takes a single parameter:
1. `x`: The floating-point number that you want to round down to the nearest integer.
The `x` parameter can be of type `double`, `float`, or `long double`. If you pass an argument of a different floating-point type, C++ will implicitly convert it to a `double` before passing it to the floor() function.
Let’s look at a few examples of passing different types of parameters to floor():
double result1 = floor(3.7); // passing a double
float num2 = 2.1f;
double result2 = floor(num2); // passing a float
long double num3 = 1.5L;
double result3 = floor(num3); // passing a long double
In each case, the argument is converted to a `double` before being passed to floor(), and the result is also returned as a `double`.
Note: One thing to keep in mind is that you cannot pass an integer type (like `int` or `long`) directly to floor(). If you need to find the floor of an integer value, you must first convert it to a floating-point type.
Return Value of floor() in C++
The floor() function in C++ returns a value of type `double`, which represents the largest integer that is less than or equal to the input parameter.
Few examples of the return values of floor() are:
double result1 = floor(3.7); // returns 3.0
double result2 = floor(-2.1); // returns -3.0
double result3 = floor(5.0); // returns 5.0
Note that even though the return type is `double`, the value returned by floor() will always be a whole number (an integer). However, it is still represented as a floating-point type.
If the input to floor() is already an integer value (e.g., 5.0), the function will return that same value as a `double`.
Note: Always remember that the return value of floor() is always less than or equal to the input value. This means that for negative numbers, the returned integer will be less than or equal to the input, not greater. For example, floor(-2.1) returns -3.0, not -2.0.
Floor for Integer Values in C++
When working with integer values in C++, you might wonder what happens if you pass an integer to the floor() function. The answer is that you cannot directly pass an integer to floor() as it expects a floating-point argument.
However, you can easily convert an integer to a floating-point type before passing it to floor().
For example:
int num = 5;
double result = floor(static_cast<double>(num));
std::cout << "The floor of " << num << " is " << result << std::endl;
Output:
The floor of 5 is 5
In this code, we use `static_cast<double>(num)` to convert the integer `num` to a `double` before passing it to floor(). The function then returns the same value as a `double`, since the input is already an integer.
Note: Using floor() with integer values might not always be necessary, as integers are already whole numbers. However, if you are working with a mix of integers and floating-point numbers in your code, using floor() consistently can make your code more readable and maintainable.
Floor for Double and Float Values in C++
The floor () function proves more useful when you work with floating-point values like `double` and `float`. It helps you to round down these values to the nearest integer.
Let’s take a look at a few examples:
double num1 = 3.7;
double result1 = floor(num1);
std::cout << "The floor of " << num1 << " is " << result1 << std::endl;
float num2 = -2.1f;
double result2 = floor(num2);
std::cout << "The floor of " << num2 << " is " << result2 << std::endl;
Output:
The floor of 3.7 is 3
The floor of -2.1 is -3
In the first example, we pass a `double` value of 3.7 to floor(), which returns 3.0 as a result. This is because 3 is the largest integer less than or equal to 3.7.
In the second example, we pass a `float` value of -2.1 to floor(). The function returns -3.0 because -3 is the largest integer less than or equal to -2.1.
Make sure that even though we pass a `float` to floor() in the second example, the function still returns a `double`. This is because floor() always returns a `double` value, regardless of the input type.
Note: When you are working with floating-point numbers, floor() can be very useful for tasks like rounding down to the nearest whole number or finding the integer part of a number.
Special Cases for floor() in C++
There are a few special cases to keep in mind whenever we are using the floor() function in C++, like:
1. Positive Infinity: If the input to floor() is positive infinity, the function will return positive infinity.
double result = floor(INFINITY);
std::cout << "The floor of infinity is " << result << std::endl;
Output:
The floor of infinity is inf
2. Negative Infinity: If the input to floor() is negative infinity, the function will return negative infinity.
double result = floor(-INFINITY);
std::cout << "The floor of -infinity is " << result << std::endl;
Output:
The floor of -infinity is -inf
3. NaN (Not a Number): If the input to floor() is NaN, the function will return NaN.
double result = floor(NAN);
std::cout << "The floor of NaN is " << result << std::endl;
Output:
The floor of NaN is nan
4. Exact Integers: If the input to floor() is already an exact integer, the function will return that same integer value.
double result = floor(5.0);
std::cout << "The floor of 5.0 is " << result << std::endl;
Output:
The floor of 5.0 is 5
Frequently Asked Questions
Can floor() be used with integer types?
No, floor() requires a floating-point argument. Convert integers to a floating-point type before use.
Does floor() always return a value less than or equal to the input?
Yes, floor() returns the largest integer less than or equal to the given number.
What happens if the input to floor() is infinity or NaN?
floor() returns the same infinity value for infinite inputs & NaN for NaN inputs.
Conclusion
In this article, we discussed the floor() function in C++, which rounds down floating-point numbers to the nearest integer. We covered its syntax, parameters, return value, and usage with different data types. We also discussed special cases like infinity and NaN.
You can also check out our other blogs on Code360.