English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ floor() function usage and example

C++ Library Function <cmath>

C ++The floor() function in returns the maximum possible integer value, which is less than or equal to the given parameter.

floor() prototype [from C ++ 11[Standard]

double floor(double x);
float floor(float x);
long double floor(long double x);
double floor(T x); //for integer

The floor() function takes a single parameter and returns a value of type double, float, or long double. This function starts with<cmath>defined in the header file.

floor() parameter

The floor() function takes one parameter, whose base value is calculated.

floor() return value

The floor() function returns the maximum possible integer value less than or equal to the given parameter.

Example1:floor() in C ++How does it work 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 running the program, the output is:

Floor of 10.25 = 10
Floor of -34.251 = -35
Floor of 0.71 = 0

Example2:Integer type floor() function

#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 running the program, the output is:

Floor of 15 = 15

The lower bound of an integer value is the integer value itself, so the lower bound function is not used for integer values in practice.

C++ Library Function <cmath>