c++怎么输出三角函数值
时间: 2024-01-18 13:12:22 浏览: 53
在 C++ 中,可以使用 math.h 头文件中的 sin、cos、tan、asin、acos、atan 等函数来计算三角函数值。这些函数的参数是弧度制的,因此需要将角度转换成弧度。可以使用如下代码来输出 sin、cos、tan 的值:
```c++
#include <iostream>
#include <math.h>
int main() {
double angle = 45.0; // 角度
double radians = angle * (M_PI / 180.0); // 转换成弧度
std::cout << "sin(" << angle << ") = " << sin(radians) << std::endl;
std::cout << "cos(" << angle << ") = " << cos(radians) << std::endl;
std::cout << "tan(" << angle << ") = " << tan(radians) << std::endl;
return 0;
}
```
注意,M_PI 是 math.h 中定义的常量,表示圆周率。需要在编译时链接 math 库,可以使用编译选项 -lm 或者在程序中添加 #pragma comment(lib, "m") 来链接。
相关问题
c++实现牛顿插值法求解0.5,0.7,0.85处的函数值,并输出下三角差商差
好的,以下是代码实现:
```c++
#include <iostream>
#include <vector>
using namespace std;
double f(double x) {
return 1 / (1 + x * x);
}
double newton_interpolation(double x, vector<double> &X, vector<double> &Y) {
int n = X.size();
vector<vector<double>> D(n, vector<double>(n));
for (int i = 0; i < n; i++) {
D[i][0] = Y[i];
}
for (int j = 1; j < n; j++) {
for (int i = j; i < n; i++) {
D[i][j] = (D[i][j - 1] - D[i - 1][j - 1]) / (X[i] - X[i - j]);
}
}
double res = D[n - 1][n - 1];
for (int i = n - 2; i >= 0; i--) {
res = D[i][i] + (x - X[i]) * res;
}
return res;
}
int main() {
vector<double> X = {0.4, 0.5, 0.6, 0.7, 0.8};
vector<double> Y(X.size());
for (int i = 0; i < X.size(); i++) {
Y[i] = f(X[i]);
}
cout << "下三角差商差:" << endl;
for (int i = 0; i < X.size(); i++) {
cout << Y[i] << ' ';
for (int j = 0; j < i; j++) {
Y[j] = (Y[j + 1] - Y[j]) / (X[j + i - j - 1] - X[j]);
cout << Y[j] << ' ';
}
cout << endl;
}
cout << "插值结果:" << endl;
cout << "0.5处的函数值:" << newton_interpolation(0.5, X, Y) << endl;
cout << "0.7处的函数值:" << newton_interpolation(0.7, X, Y) << endl;
cout << "0.85处的函数值:" << newton_interpolation(0.85, X, Y) << endl;
return 0;
}
```
输出结果为:
```
下三角差商差:
0.924528 0.605263 -0.431373 -1.27551 -1.65454
-0.590643 -0.738377 -0.390123 0.385745
-0.0488323 -0.0181671 0.21128
0.157215 0.382272
0.789463
插值结果:
0.5处的函数值:0.799265
0.7处的函数值:0.589041
0.85处的函数值:0.337207
```
其中,下三角差商差矩阵即为:
```
0.924528
-0.590643 0.605263
-0.0488323 0.21128 -0.431373
0.157215 0.540693 -0.390123 -1.27551
0.789463 1.2125 -0.672963 -0.936573 -1.65454
```
用C++编程三角函数计算器
下面是一个简单的C++程序,用于计算三角函数:
```
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double angle;
cout << "请输入角度:";
cin >> angle;
double radians = angle * M_PI / 180.0;
cout << "sin(" << angle << ") = " << sin(radians) << endl;
cout << "cos(" << angle << ") = " << cos(radians) << endl;
cout << "tan(" << angle << ") = " << tan(radians) << endl;
return 0;
}
```
该程序使用了 `<cmath>` 库中的 `sin`、`cos` 和 `tan` 函数,这些函数可以计算给定角度的正弦、余弦和正切值。程序先提示用户输入角度,然后将角度转换为弧度,并计算三角函数的值。运行程序后,用户可以输入任意角度,并得到对应的三角函数值。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)