math.h库详解


sin(double)

cos(double)

tan(double)

 

分别返回正弦,余弦,正切

#include<iostream>
#include<math.h>
using namespace std;
int main(){
  cout<<sin(3.14)<<' '<<cos(3.14)<<' '<<tan(3.14)<<endl;
}

 

注意参数是以弧度而并非角度为单位

 

log(double)

log10(double)

分别返回底数为e和10的对数

 

pow(a,b)返回a的b次方

 

sqrt(x)返回x的平主根

#include<iostream>
#include<math.h>
using namespace std;
int main(){
  cout<<sqrt(2)<<endl;
}

 

 

ceil(double),floor(double)向上和向下取整

#include<iostream>
#include<math.h>
using namespace std;
int main(){
  cout<<floor(3.14)<<' '<<ceil(3.14)<<endl;
}

输出:3 4

#include<iostream>
#include<math.h>
using namespace std;
int main(){
  cout<<floor(-3.14)<<' '<<ceil(-3.14)<<endl;
}

输出:-4 -3

 

fmod函数用于求浮点数的余数,注意%运算符只能计算整数的余数

#include<iostream>
#include<math.h>
using namespace std;
int main(){
  cout<<fmod(3.3,1.6)<<endl;
}

 

 

 

通过acos(-1)可以求出圆周率的值

#include<iostream>
#include<math.h>
using namespace std;
int main(){
  printf("%.40lf",acos(-1));
}

输出:3.1415926535897931159979634685441851615906

 

可以看到到了第16位之后就有偏差

 

abs()用于求整数的绝对值

fabs()用于求浮点数的绝对值

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM