C floor Function

The C floor function is a math function that returns the closest integer value, which is less than or equal to a given floating-point number. For example, if we pass 50.789, it returns 50.0 as the output.

In simple terms, the floor() function returns a downward number. For positive or negative values, it always moves towards negative infinity. It is widely used in financial applications, gaming systems, data simulations, etc.

C floor function syntax

The syntax of the math floor method is as shown below.

double floor(double number);

Parameters

This function accepts a single parameter value, which must be a floating-point number (double or float value).

Return Value

The floor() function returns the largest value less than or equal to the given parameter. The return value is always a floating-point number (double).

Header file required

To use the floor() function, we must include the <math.h> header file.

#include<math.h>

TIP: Use the ceil() function to return the largest number (upwards).

C floor Function with positive and negative numbers

The math floor Function allows you to find the closest integer value, which is less than or equal to a user-given number. In this program, we will find the same and display the output.

Before going into the example, let me explain the closest value returned by the floor() method for positive and negative numbers.

  • Positive Numbers: The result value moves towards zero. For example, 10.9, 10.4, 10.0 returns 10.0.
  • Negative Numbers: The result moves towards negative infinity (away from zero). For example, -10.8, -10.2 returns -10.0.

TIP: For more, please refer to the math functions article in C Programming.

#include <stdio.h>
#include <math.h>

int main()
{
    printf("\n The Floor Value of 0.75    = %.2f ", floor(0.75));
    printf("\n The Floor Value of 12.25   = %.2f ", floor(12.25));    
    printf("\n The Floor Value of 152.50  = %.2f ", floor(152.50));
    
    printf("\n The Floor Value of -12.36  = %.2f ", floor(-12.36)); 
    printf("\n The Floor Value of -27.82  = %.2f ", floor(-27.32));  
    printf("\n The Floor Value of -90.50  = %.2f ", floor(-90.50));
  
    return 0;
}
C Programming math floor Function

TIP: If you pass an integer value as the floor() function parameter, it returns the same result as the output.

User entered value

In this program, we used the math floor function to find the closest number of the user-entered floating-point value.

#include <stdio.h>
#include <math.h>

int main()
{
float number, fValue;

printf("Please Enter any Numeric: ");
scanf("%f", &number);

fValue = floor(number);

printf("\nThe Result of %.2f = %.4f ", number, fValue);

return 0;
}
Please Enter any Numeric:  125.87
The Result of 125.87 = 125.0000

C floor() function with double, floor, and long input

In the following example, we use the floor() function and pass a double data type, a float value, and a long value as parameters.

#include <stdio.h>
#include <math.h>
int main()
{
double a = 20.8;
float b = 100.89f;
long double c = 10000.9898L;

printf("Double: %.2f\n", floor(a));
printf("Float Result: %.2f\n", floor(b));
printf("Long Double Result: %.2Lf\n", floor(c));
return 0;
}
Double: 20.00
Float Result: 100.00
Long Double Result: 10000.00

Use Infinity as a parameter

For both the positive and negative infinity values, the floor() function returns the inf and -inf as the result.

TIP: If you pass the NaN (not a number) to the floor() function, it returns nan as the output. printf(“%f”, floor(NaN)) = nan;

#include <stdio.h>
#include <math.h>
int main()
{
printf("%f", floor(INFINITY));
printf("\n%f", floor(-INFINITY));
return 0;
}
inf
-inf

NOTE: If you replace the %f format specifier with %d, the result becomes 0.

C floor() vs floorf() and floorl()

The math library contains three different variants of the floor() functions, and each accepts and returns different data types.

  • Use floor() for a floating-point number such as double, float, and long.
  • Use floorf() for a float data type.
  • Use floorl() for long double type.

floor() vs trunc()

For positive values, both floor() and trunc() methods return the same result. However, for the negative numbers, the floor() function moves towards negative infinity. On the other hand, the trunc() moves towards zero.

#include <stdio.h>
#include <math.h>
int main()
{
double x = -20.8;

printf("floor Result: %.2f\n", floor(x));
printf("trunc Result: %.2f\n", trunc(x));

return 0;
}
floor Result: -21.00
trunc Result: -20.00

Difference between C floor() function and type casting

By seeing the output of the floor() function, people thought that type casting also provides the same result. However, there is a difference in the behaviour of the floor() and type casting.

  • For positive numbers, they return the same result.
  • For negative numbers, type casting truncates towards zero. On the other hand, the floor() methods move towards negative infinity.
#include <stdio.h>
#include <math.h>
int main()
{
double n = -100.678;

printf("floor(x): %.2f\n", floor(n));
printf("(int)x: %d\n", (int)n);
}
floor(x): -101.00
(int)x: -100