C語(yǔ)言中允許我們通過(guò)使用<math.h>頭文件中定義的函數(shù)來(lái)執(zhí)行數(shù)學(xué)運(yùn)算。 <math.h>頭文件包含用于執(zhí)行sqrt(),pow(),ceil(),floor()等數(shù)學(xué)運(yùn)算的各種方法。
math.h頭文件中定義和實(shí)現(xiàn)有各種方法。math.h頭文件的常用函數(shù)如下。
| 序號(hào) | 函數(shù) | 描述 |
|---|---|---|
| 1 | ceil(number) |
舍入給定數(shù)字。它返回大于或等于給定數(shù)字的整數(shù)值。 |
| 2 | floor(number) |
舍入給定數(shù)字。它返回小于或等于給定數(shù)字的整數(shù)值。 |
| 3 | sqrt(number) |
返回給定數(shù)字的平方根。 |
| 4 | pow(base, exponent) |
返回給定數(shù)字的冪值。 |
| 5 | abs(number) |
返回給定數(shù)字的絕對(duì)值。 |
我們來(lái)看一個(gè)使用math.h頭文件中的數(shù)學(xué)函數(shù)的簡(jiǎn)單例子。創(chuàng)建一個(gè)源代碼文件:math-function.c,其代碼實(shí)現(xiàn)如下 -
#include<stdio.h>
#include<math.h>
void main() {
printf("\n%f", ceil(3.6));
printf("\n%f", ceil(3.3));
printf("\n%f", floor(3.6));
printf("\n%f", floor(3.2));
printf("\n%f", sqrt(16));
printf("\n%f", sqrt(7));
printf("\n%f", pow(2, 4));
printf("\n%f", pow(3, 3));
printf("\n%d \n", abs(-12));
}
執(zhí)行上面示例代碼,得到以下結(jié)果 -
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12