C++ 标准库 - <cmath>

C++ std::cos 计算余弦,参数为弧度。0 的余弦为 1。与 sin 相同,大角度应先化约。

原型

double cos(double x);

参数

  • x:弧度。

返回值

余弦值,范围约在 [-1, 1]。

示例

#include <cmath>
#include <iostream>

int main() {
    std::cout << std::cos(0.0) << std::endl;
    std::cout << std::cos(std::acos(-1.0)) << std::endl;
    return 0;
}

输出:

1
-1

注意事项

π 的余弦在浮点下可能略偏离 -1。同时需要正弦与余弦时,可分别调用,或使用实现提供的组合接口(非本批范围)。

下一页介绍hypot:斜边长度。反三角从asin起查阅。