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)文章
C++中vector容器的注意事項(xiàng)總結(jié)
在c++中,vector是一個十分有用的容器,下面這篇文章主要給大家介紹了關(guān)于C++中vector容器的注意事項(xiàng),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12Cocos2d-x學(xué)習(xí)筆記之CCScene、CCLayer、CCSprite的默認(rèn)坐標(biāo)和默認(rèn)錨點(diǎn)實(shí)驗(yàn)
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)筆記之CCScene、CCLayer、CCSprite的默認(rèn)坐標(biāo)和默認(rèn)錨點(diǎn)實(shí)驗(yàn),這是一個非常值得研究的問題,需要的朋友可以參考下2014-09-09C++ API功能設(shè)計(jì)的實(shí)現(xiàn)
C++ API中看似很小的修改,都可能會影響到生成的對象和庫文件的二進(jìn)制表示,如果客戶想替換共享庫使之工作,就不能簡單的替換庫文件了事,而往往需要重新編譯2022-08-08