提示:这些函数都是平常经常会用到的,所有这边做个笔记记录、总结整理,可能有些许遗漏可以提醒我补充。后续我也会争取完善。
前言
这些函数都非常非常有用
math ( c ) /cmath( c++ )
向上向下取整(ceil/floor):
#include<math.h>
float t=5.6;
int n1=ceil(t);//n1=6 向上取整
int n2=floor(t);//n2=5 向下取整
四舍五入(round):
round函数返回按照指定的小数位进行四舍五入运算的结果。
round(number,digits)
参数 number:要四舍五入的数,digits是要小数点后保留的位数,如果digits>0,则四舍五入到指定小数位; 如果digits=0,则四舍五入到最接近的整数; 如果digits<0,则在小数点左侧进行四舍五入; 如果round函数只有参数number,等同于digits等于0。 返回值:四舍五入后的值。
int a=round(3.1415926);//3
int b=round(3.1415926,1);//3.1
int c=round(3.1415926,2);//3.14
取绝对值(abs):
abs()是对整数取绝对值,
函数原型:int abs(int x)
//添加cmath.h头文件
#include<cmath.h>
//abs取绝对值
int tmp=-10;
tmp=abs(tmp);//tmp=10
fabs()是对浮点数取绝对值,
函数原型:double fabs(double x)
double db=-3.14;
db=fabs(db);//3.14
求a的b次方(pow):
#include <math.h>
int a,b,result;
result = pow(a,b);//计算a的b次方
算数平方根(sqrt)
函数原型:double sqrt(double x);
#include<math.h>
double tmp;
tmp=sqrt(36);
对数(log)
函数原型:double = log(double x)
以e(2.71828)为底
#include <math.h>
#include <stdio.h>
double result;
double x=800.6872;
result=log(x);
三角函数(sin/cos/tan…)
函数原型:
double __cdecl sin(double _X);
double __cdecl cos(double _X);
double __cdecl tan(double _X);
double __cdecl asin(double _X);
double __cdecl acos(double _X);
double __cdecl atan(double _X);
double __cdecl atan2(double _Y,double _X);
举例实现:
#include <stdio.h>