C Library Functions -floor()

C standard library - <math.h>

Description

C Library Functionsdouble floor(double x)Returns the largest integer less than or equal toxthe largest integer value.

floor()is the C standard library<math.h>A function that returns the largest integer value less than or equal to the passed parameter (i.e., rounding toward negative infinity). This function is commonly used for rounding down in mathematics and engineering.

Declaration

Below is the declaration of the floor() function.

#include <math.h>

double floor(double x);
float floorf(float x);
long double floorl(long double x);

Parameter

  • x: a floating-point value.

Return Value

  • Returns the largest integer less than or equal toxthe largest integer value (rounded down).

Example

The following example demonstrates the use of the floor() function.

Example

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

int main ()
{
   float val1, val2, val3, val4;

   val1 = 1.6;
   val2 = 1.2;
   val3 = 2.8;
   val4 = 2.3;

   printf("Value1 = %.1lf\n", floor(val1));
   printf("Value2 = %.1lf\n", floor(val2));
   printf("Value3 = %.1lf\n", floor(val3));
   printf("Value4 = %.1lf\n", floor(val4));
   
   return(0);
}

Let us compile and run the above program, which will produce the following result:

Value1 = 1.0
Value2 = 1.0
Value3 = 2.0
Value4 = 2.0

Flooring multiple values

The following example demonstrates how to handle floor calculation of multiple values:

Example

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

int main() {
    double values[] = {4.7, 5.1, -3.3, 0.0};
    int num_values = sizeof(values) / sizeof(values[0]);

    for (int i = 0; i < num_values; i++) {
        double x = values[i];
        double result = floor(x);

        printf("floor(%f) = %f\n", x, result);
    }

    return 0;
}

Let us compile and run the above program, which will produce the following result:

floor(4.700000) = 4.000000
floor(5.100000) = 5.000000
floor(-3.300000) = -4.000000
floor(0.000000) = 0.000000

Code explanation

  • Define an array containing multiple floating-point numbersvalues。
  • UsageforLoop through each value and callfloor(x)the function to perform the floor calculation.
  • Print the floor calculation result for each value.

Use Case

floor()The function has a wide range of uses in many applications, including but not limited to:

  • Perform a rounding operation on floating-point numbers, ensuring that the result is less than or equal to the original value.
  • Calculating lower bound values in physics and engineering.
  • Performing floor operations in financial calculations.

Summary

floor()The function is used to return the largest integer value less than or equal to the passed parameter (floor rounding), and is an important tool for handling mathematical calculations and floating-point rounding. Through reasonable usefloor(), accurate handling of numerical values can be achieved in scientific computing, engineering applications, and financial calculations.

C standard library - <math.h>

other extensions