您好,登錄后才能下訂單哦!
今天小編給大家分享一下C++的math函數(shù)如何用的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
包含頭文件
#include<cmath>
對(duì)double型變量取絕對(duì)值
#include<iostream> using namespace std; #include<cmath> int main() { double d=-3.14; printf("%.2f\n",fabs(d)); return 0; }
用于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
返回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
返回double型變量的算術(shù)平方根
#include<iostream> using namespace std; #include<cmath> int main() { double d=sqrt(3.0); printf("%f\n",d); return 0; }
返回以自然對(duì)數(shù)e為底的對(duì)數(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
參數(shù)要求是弧度制
也有對(duì)應(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; }
將double型變量四舍五入取整,返回也是double
以上就是“C++的math函數(shù)如何用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。