在看这篇文章之前,先看看#include <math.h>的简介。
这篇文章我来给大家几个math.h的应用。
应用
应用一
#include <iostream>
#include <math.h> //导入math.h库
using namespace std;
double n; //注意:这里必须是一个double类型
int main() {
std::cin >> n;
std::cout << ceil(n); //向上取整
return 0;
}
应用二
#include <iostream>
#include <math.h> //导入math.h库
using namespace std;
double n; //注意:这里必须是一个double类型
int main() {
std::cin >> n;
std::cout << floor(n); //向下取整
return 0;
}
应用三
#include <iostream>
#include <math.h>
using namespace std;
double n; //注意:这里必须是一个double类型
int main() {
std::cin >> n;
std::cout << sqrt(n); //开n的平方根
return 0;
}
应用四
#include <iostream>
#include <math.h>
using namespace std;
double n; //注意:这里必须是一个double类型
int main() {
std::cin >> n;
std::cout << exp(n); //求取自然数n的幂
return 0;
}
综合应用
#include <iostream>
#include <math.h>
#include <windows.h>
using namespace std;
double n, m;
int main() {
std::cin >> n >> m;
std::cout << "三角函数:\n";
std::cout << "正弦:" << sin(n) << std::endl;
std::cout << "余弦:" << cos(n) << std::endl;
std::cout << "正切:" << tan(n) << std::endl;
system("pause");
std::cout << "反三角函数:\n";
std::cout << "结果介于[-PI/2,PI/2]:" << asin(n) << std::endl;
std::cout << "结果介于[0,PI]:" << acos(n) << std::endl;
std::cout << "反正切(主值),结果介于[-PI/2,PI/2]:" << atan(n) << std::endl;
std::cout << "反正切(整圆值),结果介于[-PI,PI]:" << atan2(n, m) << std::endl;
system("pause");
std::cout << "双曲三角函数\n";
std::cout << "双曲正弦:" << sinh(n) << std::endl;
std::cout << "双曲余弦:" << cosh(n) << std::endl;
std::cout << "双曲正切:" << tanh(n) << std::endl;
return 0;
}
//做完之后好有成就感,以后三角函数题不用手算了(^-^)
如果想要更多,那就给我私信~