c/c++语言开发共享C++详细讲解常用math函数的用法

包含头文件#include<cmath>1、fabs(double x)对double型变量取绝对值#include<iostream>using namespace std;

包含头文件

#include<cmath>  

1、fabs(double x)

对double型变量取绝对值

#include<iostream>  using namespace std;  #include<cmath>  int main()  {      double d=-3.14;      printf("%.2fn",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  %.0fn",floor(d1),ceil(d1));      printf("%.0f  %.0fn",floor(d2),ceil(d2));      return 0;  }  

-4 -3

3 4

3、pow(double x,double n)

返回x的n次方

#include<iostream>  using namespace std;  #include<cmath>  int main()  {      double d=pow(2.0,3.0);      printf("%fn",d);      return 0;  }  

8.000000

4、sqrt(double x)

返回double型变量的算术平方根

#include<iostream>  using namespace std;  #include<cmath>  int main()  {      double d=sqrt(3.0);      printf("%fn",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("%fn",d);      double d1=log10(10.0);      printf("%fn",d1);      double d2=log2(2);      printf("%fn",d2);      double d3=log1p(10);//更精确      printf("%fn",d3);      double d4=log(10);      printf("%fn",d4);      return 0;  }  

1.000000

1.000000

1.000000

2.397895

2.302585

6、sin(double x)cos(double x) tan(double x)

参数要求是弧度制

也有对应的反函数

#include<iostream>  using namespace std;  #include<cmath>  const double pi=acos(-1.0);//因为cos(pi)=-1  int main()  {      double d=sin(pi/4);      printf("%fn",d);      double d1=cos(pi/4);      printf("%fn",d1);      double d2=tan(pi/4);      printf("%fn",d2);      double d3=asin(1);      printf("%fn",d3);      double d4=atan(1);      printf("%fn",d4);      return 0;  }

7、round(double x)

将double型变量四舍五入取整,返回也是double

到此这篇关于c++详细讲解常用math函数的用法的文章就介绍到这了,更多相关c++math函数内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享C++详细讲解常用math函数的用法,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1082027.html

(0)
上一篇 2022年4月24日
下一篇 2022年4月24日

精彩推荐