- C 标准库
- C 库 - 首页
- C 库 - <assert.h>
- C 库 - <complex.h>
- C 库 - <ctype.h>
- C 库 - <errno.h>
- C 库 - <fenv.h>
- C 库 - <float.h>
- C 库 - <inttypes.h>
- C 库 - <iso646.h>
- C 库 - <limits.h>
- C 库 - <locale.h>
- C 库 - <math.h>
- C 库 - <setjmp.h>
- C 库 - <signal.h>
- C 库 - <stdalign.h>
- C 库 - <stdarg.h>
- C 库 - <stdbool.h>
- C 库 - <stddef.h>
- C 库 - <stdio.h>
- C 库 - <stdlib.h>
- C 库 - <string.h>
- C 库 - <tgmath.h>
- C 库 - <time.h>
- C 库 - <wctype.h>
- C 标准库资源
- C 库 - 快速指南
- C 库 - 有用资源
- C 库 - 讨论
C 库 - ceil() 函数
C 库的 ceil() 函数,类型为 double,接受单个参数 (x),返回大于或等于给定值的最小的整数值。
此方法将值向上舍入到最接近的整数。
语法
以下是 C 库函数 ceil() 的语法:
double ceil(double x)
参数
此函数只接受一个参数:
x - 这是浮点值。
返回值
此函数返回不小于 x 的最小整数值。
示例 1
C 库程序演示了 ceil() 函数的用法。
#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);
}
输出
执行上述代码后,我们将得到以下结果:
value1 = 2.0 value2 = 2.0 value3 = 3.0 value4 = 3.0
示例 2
下面的程序生成一系列正浮点数的上取整整数表。
#include <stdio.h>
#include <math.h>
int main() {
double start = 1.5;
double end = 10.5;
printf("Table of Ceiling Integers:\n");
for (double num = start; num <= end; num += 1.0) {
int result = ceil(num);
printf("Ceil(%.2lf) = %d\n", num, result);
}
return 0;
}
输出
执行代码后,我们将得到以下结果:
Table of Ceiling Integers: Ceil(1.50) = 2 Ceil(2.50) = 3 Ceil(3.50) = 4 Ceil(4.50) = 5 Ceil(5.50) = 6 Ceil(6.50) = 7 Ceil(7.50) = 8 Ceil(8.50) = 9 Ceil(9.50) = 10 Ceil(10.50) = 11
示例 3
在这个程序中,我们将给定值向上舍入到四舍五入的浮点值。
#include <stdio.h>
#include <math.h>
int main() {
double num = 8.33;
int result = ceil(num);
printf("Ceiling integer of %.2lf = %d\n", num, result);
return 0;
}
输出
上述代码产生以下结果:
Ceiling integer of 8.33 = 9
广告