C Math 函数s
Math 函数
还有一个可用的 math 数学函数列表,可以让您对数字执行数学任务。
要使用它们,你必须在程序中包含 math.h 头文件:
#include <math.h>
平方根
要查找数字的平方根,请使用 sqrt() 函数:
实例
#include <stdio.h>#include <math.h>int main() {printf("%f", sqrt(16));return 0;}
四舍五入
ceil() 函数的作用是:将一个数字向上舍入到最接近的整数,floor() 方法的作用是:将一个数字向下舍入到最接近的整数,然后返回结果:
实例
#include <stdio.h>#include <math.h>int main() {printf("%f\n", ceil(1.4));printf("%f\n", floor(1.4));return 0;}
求幂
pow() 函数将 x 的值返回为 y(xy)的幂::
实例
#include <stdio.h>#include <math.h>int main() {printf("%f", pow(4, 3));return 0;}
其他 Math 函数
下表列出了其他流行的 Math 数学函数(来自 <math.h> 库):
| 函数 | 描述 |
|---|---|
| abs(x) | 返回 x 的绝对值 |
| acos(x) | 返回 x 的反余弦值 |
| asin(x) | 返回 x 的反正弦值 |
| atan(x) | 返回 x 的反正切值 |
| cbrt(x) | 返回 x 的立方根 |
| cos(x) | 返回 x 的余弦值 |
| exp(x) | 返回 Ex 的值 |
| sin(x) | 返回 x 的正弦值(x 以弧度为单位) |
| tan(x) | 返回角度的切线 |