1、fabs(double x)
2、floor(double x)ceil(double x)
3、pow(double x,double n)
4、sqrt(double x)
5、log(double x)
6、sin(double x)cos(double x) tan(double x)
7、round(double x)
包含头文件
#include<cmath>
1、fabs(double x)
对double型变量取绝对值
#include<iostream>
using namespace std;
#include<cmath>
int main()
{
double d=-3.14;
printf("%.2f\n",fabs(d));
return 0;
}
2、floor(double x)ceil(double x)
用于double型变量,返回类型也为double
向下取整:floor
向上取整:ceil
#include<iostream>
using namespace std;
#include<cmath>
int main()
{
double d1=-3.14;
double d2=3.14;
printf("%.0f %.0f\n",floor(d1),ceil(d1));
printf("%.0f %.0f\n",floor(d2),ceil(d2));
return 0;
}
3、pow(double x,double n)-4 -3
3 4
返回x的n次方
#include<iostream>
using namespace std;
#include<cmath>
int main()
{
double d=pow(2.0,3.0);
printf("%f\n",d);
return 0;
}
4、sqrt(double x)8.000000
返回double型变量的算术平方根
#include<iostream>
using namespace std;
#include<cmath>
int main()
{
double d=sqrt(3.0);
printf("%f\n",d);
return 0;
}
5、log(double x)
返回以自然对数e为底的对数
#include<iostream>
using namespace std;
#include<cmath>
int main()
{
double d=log(exp(1));//exp(1)表示e
printf("%f\n",d);
double d1=log10(10.0);
printf("%f\n",d1);
double d2=log2(2);
printf("%f\n",d2);
double d3=log1p(10);//更精确
printf("%f\n",d3);
double d4=log(10);
printf("%f\n",d4);
return 0;
}
6、sin(double x)cos(double x) tan(double x)1.000000
1.000000
1.000000
2.397895
2.302585
参数要求是弧度制
也有对应的反函数
#include<iostream>
using namespace std;
#include<cmath>
const double PI=acos(-1.0);//因为cos(pi)=-1
int main()
{
double d=sin(PI/4);
printf("%f\n",d);
double d1=cos(PI/4);
printf("%f\n",d1);
double d2=tan(PI/4);
printf("%f\n",d2);
double d3=asin(1);
printf("%f\n",d3);
double d4=atan(1);
printf("%f\n",d4);
return 0;
}
7、round(double x)
将double型变量四舍五入取整,返回也是double
到此这篇关于C++详细讲解常用math函数的用法的文章就介绍到这了,更多相关C++math函数内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!