The floor() function in C++ returns the largest possible integer value which is less than or equal to the given argument.
floor() prototype [As of C++ 11 standard]
double floor(double x); float floor(float x); long double floor(long double x); double floor(T x); // For integral type
The floor() function takes a single argument and returns a value of type double, float or long double type. This function is defined in <cmath> header file.
floor() Parameters
The floor() function takes a single argument whose floor value is computed.
floor() Return value
The floor() function returns the largest possible integer value which is less than or equal to the given argument.
Example 1: How floor() works in C++?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 10.25, result;
result = floor(x);
cout << "Floor of " << x << " = " << result << endl;
x = -34.251;
result = floor(x);
cout << "Floor of " << x << " = " << result << endl;
x = 0.71;
result = floor(x);
cout << "Floor of " << x << " = " << result << endl;
return 0;
}
When you run the program, the output will be:
Floor of 10.25 = 10 Floor of -34.251 = -35 Floor of 0.71 = 0
Example 2: floor() function for integral types
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x = 15;
double result;
result = floor(x);
cout << "Floor of " << x << " = " << result << endl;
return 0;
}
When you run the program, the output will be:
Floor of 15 = 15
The floor of an integral value is the integral value itself, so floor function isn't used on integral values in practice.