C++ floor Function: Purpose & Examples

In C++, the floor function is a mathematical function used to obtain the largest integer value of a floating-point number that is less than or equal to the number. In other words, it rounds the floating point number down to the nearest integer.

Specifically, the floor function returns a double value that is the maximum integer value less than or equal to the input parameter. For example, the result of floor(4.7) is 4 and the result of floor(-3.9) is -4.

The floor function is very useful when dealing with floating-point numbers, especially when you need to get the largest integer that is less than a certain value. For example, you can use the floor function to calculate index values in arrays or vectors, or to compute the integer part of a fraction.

It’s important to note that the return type of the floor function is double, not an integer type. Therefore, if an integer result is needed, you can use type conversion to convert it to the desired integer type.

bannerAds