C++ Math floor
It approximates the number to the closest whole number that is less than or equal to the provided value.
For example:
floor(8.2)=8.0;
floor(-8.8)=-9.0;
Syntax
Suppose a number is 'x'. Syntax would be :
double floor(double x);
Parameter
x : It represents a value that is rounded to the closest whole number.
Return value
It returns the value of x rounded down to the nearest integer.
Example 1
Let's examine a basic illustration using a non-negative number.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=7.8;
std::cout << "Initial value of x is : " << x<<std::endl;
cout<<"Now, the value of x is :"<<floor(x);
return 0;
}
Output:
Initial value of x is : 7.8
Now, the value of x is :7
Example 2
Let's explore a basic illustration using a negative value.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=-10.2;
std::cout << "Initial value of x is : " << x<<std::endl;
cout<<"Now, the value of x is :"<<floor(x);
return 0;
}
Output:
Initial value of x is : -10.2
Now, the value of x is :-11