C++ 标准库 - <cmath>

C++ std::sin 计算正弦,参数为弧度。角度转弧度:弧度 = 角度 × π / 180。π 可用 std::acos(-1.0) 得到,勿依赖非标准宏 M_PI(部分实现才提供)。

原型

double sin(double x);

参数

  • x:弧度。

返回值

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

示例

#include <cmath>
#include <iostream>

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

输出:

0
1

注意事项

π/2 的正弦在浮点下可能略小于 1,本示例通常显示为 1。大角度应先化约到主值区间,否则精度变差。反函数见asin。

下一页介绍cos:余弦。