資訊學奧賽系列教程:常用的數學函式
阿新 • • 發佈:2018-11-30
C++語言中常用的數學函式:
1、求絕對值 abs()
2、求平方根sqrt()
3、求指數pow()
4、向下取整floor()
5、向上取整ceil()
使用這些數學函式,需要引用cmath標頭檔案,上述函式中,只有pow需要兩個引數,其餘只需要一個引數
程式碼如下:
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int a=-5; cout<<"a的絕對值:"<<abs(a)<<endl; int b=16; cout<<"b的平方根:"<<sqrt(b)<<endl; float b1=8.52; cout<<"b1的平方根:"<<sqrt(b1)<<endl; int c=3,d=2; cout<<c<<"的"<<d<<"次方:"<<pow(c,d)<<endl; double pi =3.144654357; cout<<"pi向下取整是:"<<floor(pi)<<endl; cout<<"pi向上取整是:"<<ceil(pi)<<endl; cout<<fixed<<setw(10)<<setprecision(2)<<pi<<endl; return 0; }
常用數學函式練習:
程式碼:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a=27,b=34;
int c,d;
float e;
float f,g;
c = a*b;
d = pow((a-b),3);
e = (a-b)/(a+b);
f = sqrt(a+b);
g = c+d-e+f;
cout<<g<<endl;
return 0;
}