温馨提示×

如何在循环中使用c++ atan函数

c++
小樊
81
2024-09-15 17:18:50
栏目: 编程语言

在C++中,你可以使用atan()函数计算一个数值的反正切

#include<iostream>
#include <cmath>
#include<vector>

int main() {
    // 创建一个包含一些数值的向量
    std::vector<double> numbers = {-1.0, 0.0, 1.0, 2.0, 3.0};

    // 遍历向量并计算每个数值的反正切
    for (const auto &number : numbers) {
        double result = std::atan(number);
        std::cout << "The arctangent of "<< number << " is: "<< result<< std::endl;
    }

    return 0;
}

这个示例将会输出:

The arctangent of -1 is: -0.785398
The arctangent of 0 is: 0
The arctangent of 1 is: 0.785398
The arctangent of 2 is: 1.10715
The arctangent of 3 is: 1.24905

请注意,atan()函数接受一个double类型的参数,并返回一个double类型的结果。在上面的示例中,我们使用了C++11的范围for循环来遍历向量。如果你使用的是旧版本的C++,你可能需要使用传统的for循环或迭代器。

0