C library function -ceil()

C standard library - <math.h>

Description

C Library Functionsdouble ceil(double x)Returns greater than or equal toxthe smallest integer value of.

ceil()is the C standard library<math.h>a function used to return the smallest integer value greater than or equal to the argument passed in (i.e., rounding toward positive infinity). This function is often used in mathematics and engineering for rounding up.

Declaration

Below is the declaration of the ceil() function.

#include <math.h>

double ceil(double x);
float ceilf(float x);
long double ceill(long double x);

Parameter

  • x: a floating-point value.

Return Value

  • Returns greater than or equal toxthe smallest integer value (rounded up).

Example

The following example demonstrates the usage of the ceil() 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", ceil(val1));
   printf ("value2 = %.1lf\n", ceil(val2));
   printf ("value3 = %.1lf\n", ceil(val3));
   printf ("value4 = %.1lf\n", ceil(val4));
   
   return(0);
}

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

value1 = 2.0
value2 = 2.0
value3 = 3.0
value4 = 3.0

Rounding up multiple values

The following example demonstrates how to perform ceiling calculations on multiple values:

Example

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

int main() {
    double values[] = {4.2, 5.7, -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 = ceil(x);

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

    return 0;
}

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

ceil(4.200000) = 5.000000
ceil(5.700000) = 6.000000
ceil(-3.300000) = -3.000000
ceil(0.000000) = 0.000000

Code explanation

  • Define an array containing multiple floating-point numbers.values。
  • UsageforLoop through each value and callceil(x)the function to perform the upward rounding calculation.
  • Print the ceiling calculation result for each value.

Use Case

ceil()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 the result is greater than or equal to the original value.
  • Calculating upper bound values in physics and engineering.
  • Performing rounding-up operations in financial calculations.

Summary

ceil()The function is used to return the smallest integer value greater than or equal to the argument passed in (rounding up), and is an important tool for handling mathematical calculations and floating-point rounding. By using it appropriately,ceil()accurate numerical processing can be achieved in scientific computing, engineering applications, and financial calculations.

C standard library - <math.h>

other extensions