溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++的math函數(shù)如何用

發(fā)布時(shí)間:2022-04-25 10:26:06 來源:億速云 閱讀:355 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下C++的math函數(shù)如何用的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

包含頭文件

#include<cmath>

1、fabs(double x)

對(duì)double型變量取絕對(duì)值

#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)

返回以自然對(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

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

參數(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;
}

7、round(double x)

將double型變量四舍五入取整,返回也是double

以上就是“C++的math函數(shù)如何用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(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)容。

AI