C++詳細(xì)講解常用math函數(shù)的用法
包含頭文件
#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;
}
-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("%f\n",d);
return 0;
}
8.000000
4、sqrt(double x)
返回double型變量的算術(shù)平方根
#include<iostream>
using namespace std;
#include<cmath>
int main()
{
double d=sqrt(3.0);
printf("%f\n",d);
return 0;
}
5、log(double x)
返回以自然對數(shù)e為底的對數(shù)
#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;
}
1.000000
1.000000
1.000000
2.397895
2.302585
6、sin(double x)cos(double x) tan(double x)
參數(shù)要求是弧度制
也有對應(yīng)的反函數(shù)
#include<iostream>
using namespace std;
#include<cmath>
const double PI=acos(-1.0);//因?yàn)閏os(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
到此這篇關(guān)于C++詳細(xì)講解常用math函數(shù)的用法的文章就介紹到這了,更多相關(guān)C++math函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++頭文件algorithm中的函數(shù)功能詳解
- 簡單談?wù)凜++ 頭文件系列之(algorithm)
- 詳解C++中的萬能頭文件
- 關(guān)于VS2022不能使用<bits/stdc++.h>的解決方案(萬能頭文件)
- C++ Boost Algorithm算法超詳細(xì)精講
- C++實(shí)現(xiàn)分水嶺算法(Watershed Algorithm)
- C++常用字符串函數(shù)大全(2)
- 詳解C++字符串常用操作函數(shù)(查找、插入、截取、刪除等)
- c++中的string常用函數(shù)用法總結(jié)
- C++常用函數(shù)總結(jié)(algorithm 頭文件)
相關(guān)文章
Qt5+QMediaPlayer實(shí)現(xiàn)音樂播放器的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Qt5和QMediaPlayer實(shí)現(xiàn)簡易的音樂播放器,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-12-12
C語言實(shí)現(xiàn)串的順序存儲(chǔ)表示與基本操作
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)串的順序存儲(chǔ)表示與基本操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

