C++

Floor Function Examples in C++

The C++ language aids in developing the web browsers, games, banking applications, OS, and many others. It provides many functions that help in writing the code more quickly. The C++ language also provides the “floor()” function. When an input is provided, the C++ floor() function gives the greatest integer number that can be less than or equal to that given value. The “cmath” header file contains this function. We can pass the “float”, “double”, and “long double” data types as the parameter of the “floor()” function. On the other hand, if we pass the integer number as the parameter of this function, a “double” data type is created by type-casting it. Here, we will learn how the “floor()” function works in C++ programming.

Example 1:

In this C++ code, the “iostream” and “cmath” header files are included. The “iostream” header file is to perform the input\output operations by utilizing the cin\cout functions since these functions are defined in the “iostream” header file. The “cmath” header file is added here to perform the mathematical operations on the data. The “namespace std” is placed ahead. Then, the driver code is added which is “main()”. Below this, we utilize the “num” with the “float” data type. The value of “num” that we set here is “4.6”.

Then, we add the “cout()” function that prints the data which we entered in it. First, we display the float number that we previously initialized. Then, we utilize the “floor()” function and pass “num” as the argument of this “floor()” function. We also print the result after applying the “floor()” function.

Code 1:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float num = 4.6;
    cout << "The number is " << num << endl;
        cout << "The floor of this number is: " << floor(num) << endl;
    return 0;
}

Output:

In this output, the number is “4.6”. But when we apply the “floor()” method, it gives the result of “4”. This shows that the “floor()” method returns a number that is less than or equal to the given number.

Example 2:

Here, we include two header files named “iostream” and “cmath”. Then, we place “namespace std” and declare the “main()” function. After this, we declare four variables with “float” data type. These variables are named “num_1”, “num_2”, “num_3”, and “num_4”. We assign “4.9” to “num_1”, “-6.4” to “num_2”, “5.1” to “num_3”, and “8” to “num_4”. Then, we apply the “floor()” function on the “num_1” variable and print the value as well as the result that we got after applying the “floor()” function on this number. In the same way, we print all values and the result of these values that we got from the “floor()” function by placing them into this function as its argument.

Code 2:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float num_1, num_2, num_3, num_4;
        num_1 = 4.9;
        num_2 = -6.4;
        num_3 = 5.1;
        num_4 = 8;
    cout << "The first number is " << num_1 << " and its floor is  " << floor(num_1) <<endl;
    cout << "The second number is " << num_2 << " and its floor is  " << floor(num_2) <<endl;
    cout << "The third number is " << num_3 << " and its floor is  " << floor(num_3) <<endl;
    cout << "The fourth number is " << num_4 << " and its floor is  " << floor(num_4) <<endl;
    return 0;
}

Output:

The “4.9” value returns “4” after applying the “floor()” function. Then, we put “-6.4” into this “floor()” function and it returns “-7” as shown in the following. The result of number “5.1” is “5” after applying the “floor()” method. The same result is shown as “8” returns “8” as the floor value:

Example 3:

Here, we apply the “floor()” function on the integer values. First, we initialize the integer variables named “value_1” and “value_2”. The “value_1” is initialized with “5” and the “value_2” is initialized with “-8”. After this, we place the “cout” where we add the “floor()” function in which we pass the “value_1” in the first “cout” statement. In the next “cout”, we utilize the “floor()” where we pass the “value_2” as the parameter. Now, it applies the “floor()” function on these values and prints them on the screen.

Code 3:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int value_1, value_2;
        value_1 = 5;
        value_2 = -8;
    cout << "The first integer number is " << value_1 << " and its floor is  " << floor(value_1) <<endl;
    cout << "The second integer number is " << value_2 << " and its floor is  " << floor(value_2) <<endl;
    return 0;
}

Output:

This result shows that the value of “5” gives “5” after computing the “floor()” function and “-8” gives “-8” as the value after applying the “floor()” function.

Example 4:

Here, we apply the “floor()” function to the values of the “double” data type. We also include the “iomanip” header file here which aids in using the “setprecision()” function since this function is declared in this header file. Then, we need to utilize this function in our code. Now, we initialize the “d_1”, “d_2”, and “d_3” variables with the values. Then, we have the “cout” in which we type “setprecision()” which helps in getting the exact value of a “double” data type number with the required number of decimal places. We pass “10” here as its parameter. Then, we print the values, apply the “floor()” function to these values, and print them.

Code 4:

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int main()
{

    double d_1 = 4.99986399, d_2 = -6.9612499, d_3 = 9.00320, d_4 = 3.000000;

    cout << setprecision(10) << "The first double value is " << d_1 << " & floor is: " << floor(d_1) << endl;

    cout << setprecision(10) << "The second double value is " << d_2 << " & floor is: " << floor(d_2) << endl;

    cout << setprecision(10) << "The third double value is " << d_3 << " & floor is: " << floor(d_3) << endl;

    cout << setprecision(10) << "The fourth double value is " << d_4 << " & floor is: " << floor(d_4) << endl;

    return 0;

}

Output:

The values that we get after computing the “floor()” function are displayed here.  We applied the “floor()” function to the double data type values in this code:

Example 5:

After including all three header files here, we place “namespace std” and the “main()”. After this, a value of “-0.000” is inserted into the “floor()” function as the parameter. We utilize “cout()” as well. Then, we place “INFINITY” as the parameter of the “floor()” function. Below this, we add “-INFINITY” into the “floor()” function’s parameter. At the end, we insert “NAN” as its parameter. All these “floor()” functions are utilized inside the “cout” statement.

Code 5:

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
    cout << "The value is -0.000 and floor is " << floor(-0.000) << endl;
    cout << "The value is INFINITY and floor is " << floor(INFINITY) << endl;
    cout << "The value is -INFINITY and floor is " << floor(-INFINITY) << endl;
    cout << "The value is NaN and floor is " << floor(NAN) << endl;

    return 0;
}

Output:

The value of “-0.000” returns “-0” after performing the “floor()” function. The “INFINITY” and “-INFINITY” return “inf” and “-inf”, respectively, after performing the “floor()” function. Also, “NAN” returns “nan” after performing the “floor()” function.

Conclusion

The “floor()” function in C++ programming is discussed here thoroughly. We explained that the “floor()” function returns the value that is less than or equal to the number that is given to that function as the parameter. We applied this function on integers, floats, and double-data-typed numbers in this tutorial. All examples are discussed here in detail.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.