欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++詳細(xì)講解常用math函數(shù)的用法

 更新時間:2022年04月24日 17:09:37   作者:編程小鵬  
C++提供了很多實(shí)用的數(shù)學(xué)函數(shù),如果要使用先添加頭文件,當(dāng)然,加頭文件誰都知道,接下來我們一起詳細(xì)看看各個math函數(shù)的實(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論