sin / cos / tan 家族

sin(float $num): float — cos(float $num): float — tan(float $num): float — asin(float $num): float …

三角函数族速查:基本三角(sin/cos/tan)、反三角(asin/acos/atan)、双曲(sinh/cosh/tanh)各语言全部内置且同族——参数一律是弧度而非角度,角度入库先 deg2rad。

C C23

C23
#include <math.h>
#include <stdio.h>

int main(void) {
    // 所有三角函数的参数都是弧度,不是角度
    //(M_PI 是 POSIX 扩展而非 ISO C,故写显式 π 字面量)
    const double PI = 3.14159265358979323846;
    printf("%.10f\n", sin(PI / 6));    // 30 度
    printf("%.10f\n", cos(PI / 3));    // 60 度
    printf("%.10f\n", tan(PI / 4));    // 45 度
    printf("%.10f\n", atan(1.0));      // 反三角族:atan(1) 恰为 π/4
    printf("%.10f\n", asin(sin(0.5))); // 反函数往返回到 0.5
    printf("%.10f\n", sinh(1.0));      // 双曲正弦
    printf("%.10f\n", cosh(1.0));      // 双曲余弦
    return 0;
}
输出
0.5000000000
0.5000000000
1.0000000000
0.7853981634
0.5000000000
1.1752011936
1.5430806348

C++ C++26

C++26
#include <cmath>
#include <cstdio>

int main() {
    // C++ 同族函数在 <cmath> 的 std:: 名下
    const double PI = 3.14159265358979323846;
    std::printf("%.10f\n", std::sin(PI / 6));    // 30 度
    std::printf("%.10f\n", std::cos(PI / 3));    // 60 度
    std::printf("%.10f\n", std::tan(PI / 4));    // 45 度
    std::printf("%.10f\n", std::atan(1.0));      // atan(1) 恰为 π/4
    std::printf("%.10f\n", std::asin(std::sin(0.5)));
    std::printf("%.10f\n", std::sinh(1.0));      // 双曲正弦
    std::printf("%.10f\n", std::cosh(1.0));      // 双曲余弦
    return 0;
}
输出
0.5000000000
0.5000000000
1.0000000000
0.7853981634
0.5000000000
1.1752011936
1.5430806348

C# v11.0.100

v11.0.100
// .NET 同族函数在 System.Math 名下(参数均为弧度)
Console.WriteLine(Math.Sin(Math.PI / 6).ToString("F10"));   // 30 度
Console.WriteLine(Math.Cos(Math.PI / 3).ToString("F10"));   // 60 度
Console.WriteLine(Math.Tan(Math.PI / 4).ToString("F10"));   // 45 度
Console.WriteLine(Math.Atan(1).ToString("F10"));            // atan(1) 恰为 π/4
Console.WriteLine(Math.Asin(Math.Sin(0.5)).ToString("F10")); // 往返
Console.WriteLine(Math.Sinh(1).ToString("F10"));            // 双曲正弦
Console.WriteLine(Math.Cosh(1).ToString("F10"));            // 双曲余弦
输出
0.5000000000
0.5000000000
1.0000000000
0.7853981634
0.5000000000
1.1752011936
1.5430806348

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math"
)

func main() {
	// Go 同族函数在 math 包(参数均为弧度)
	fmt.Printf("%.10f\n", math.Sin(math.Pi/6))   // 30 度
	fmt.Printf("%.10f\n", math.Cos(math.Pi/3))   // 60 度
	fmt.Printf("%.10f\n", math.Tan(math.Pi/4))   // 45 度
	fmt.Printf("%.10f\n", math.Atan(1))          // atan(1) 恰为 π/4
	fmt.Printf("%.10f\n", math.Asin(math.Sin(0.5)))
	fmt.Printf("%.10f\n", math.Sinh(1))          // 双曲正弦
	fmt.Printf("%.10f\n", math.Cosh(1))          // 双曲余弦
}
输出
0.5000000000
0.5000000000
1.0000000000
0.7853981634
0.5000000000
1.1752011936
1.5430806348

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // Java 同族函数在 java.lang.Math 名下(参数均为弧度)
        System.out.printf("%.10f%n", Math.sin(Math.PI / 6));   // 30 度
        System.out.printf("%.10f%n", Math.cos(Math.PI / 3));   // 60 度
        System.out.printf("%.10f%n", Math.tan(Math.PI / 4));   // 45 度
        System.out.printf("%.10f%n", Math.atan(1));            // atan(1) 恰为 π/4
        System.out.printf("%.10f%n", Math.asin(Math.sin(0.5)));
        System.out.printf("%.10f%n", Math.sinh(1));            // 双曲正弦
        System.out.printf("%.10f%n", Math.cosh(1));            // 双曲余弦
    }
}
输出
0.5000000000
0.5000000000
1.0000000000
0.7853981634
0.5000000000
1.1752011936
1.5430806348

JavaScript v26.13.1

v26.13.1
// JavaScript 同族函数在全局 Math 对象上(参数均为弧度)
console.log(Math.sin(Math.PI / 6).toFixed(10));    // 30 度
console.log(Math.cos(Math.PI / 3).toFixed(10));    // 60 度
console.log(Math.tan(Math.PI / 4).toFixed(10));    // 45 度
console.log(Math.atan(1).toFixed(10));             // atan(1) 恰为 π/4
console.log(Math.asin(Math.sin(0.5)).toFixed(10)); // 往返
console.log(Math.sinh(1).toFixed(10));             // 双曲正弦
console.log(Math.cosh(1).toFixed(10));             // 双曲余弦
输出
0.5000000000
0.5000000000
1.0000000000
0.7853981634
0.5000000000
1.1752011936
1.5430806348

PHP v8.6.0

v8.6.0
// PHP 三角族:sin/cos/tan、asin/acos/atan、sinh/cosh/tanh
// 参数一律弧度——角度先经 deg2rad() 换算
printf("%.10f\n", sin(M_PI / 6));    // 30 度
printf("%.10f\n", cos(M_PI / 3));    // 60 度
printf("%.10f\n", tan(M_PI / 4));    // 45 度
printf("%.10f\n", atan(1));          // atan(1) 恰为 π/4
printf("%.10f\n", asin(sin(0.5)));   // 反函数往返回到 0.5
printf("%.10f\n", sinh(1));          // 双曲正弦
printf("%.10f\n", cosh(1));          // 双曲余弦
输出
0.5000000000
0.5000000000
1.0000000000
0.7853981634
0.5000000000
1.1752011936
1.5430806348

Python v3.15.2

v3.15.2
import math

# Python 同族函数在 math 模块(参数均为弧度)
print(f"{math.sin(math.pi / 6):.10f}")    # 30 度
print(f"{math.cos(math.pi / 3):.10f}")    # 60 度
print(f"{math.tan(math.pi / 4):.10f}")    # 45 度
print(f"{math.atan(1):.10f}")             # atan(1) 恰为 π/4
print(f"{math.asin(math.sin(0.5)):.10f}") # 往返
print(f"{math.sinh(1):.10f}")             # 双曲正弦
print(f"{math.cosh(1):.10f}")             # 双曲余弦
输出
0.5000000000
0.5000000000
1.0000000000
0.7853981634
0.5000000000
1.1752011936
1.5430806348

Rust v1.99.1

v1.99.1
fn main() {
    // Rust 以 f64 方法暴露同族函数(参数均为弧度)
    let pi = std::f64::consts::PI;
    println!("{:.10}", (pi / 6.0).sin());    // 30 度
    println!("{:.10}", (pi / 3.0).cos());    // 60 度
    println!("{:.10}", (pi / 4.0).tan());    // 45 度
    println!("{:.10}", 1.0_f64.atan());      // atan(1) 恰为 π/4
    println!("{:.10}", 0.5_f64.sin().asin()); // 往返
    println!("{:.10}", 1.0_f64.sinh());      // 双曲正弦
    println!("{:.10}", 1.0_f64.cosh());      // 双曲余弦
}
输出
0.5000000000
0.5000000000
1.0000000000
0.7853981634
0.5000000000
1.1752011936
1.5430806348

TypeScript v26.13.1

v26.13.1
// TypeScript 与 JavaScript 共用 Math 同族函数(参数均为弧度)
console.log(Math.sin(Math.PI / 6).toFixed(10));    // 30 度
console.log(Math.cos(Math.PI / 3).toFixed(10));    // 60 度
console.log(Math.tan(Math.PI / 4).toFixed(10));    // 45 度
console.log(Math.atan(1).toFixed(10));             // atan(1) 恰为 π/4
console.log(Math.asin(Math.sin(0.5)).toFixed(10)); // 往返
console.log(Math.sinh(1).toFixed(10));             // 双曲正弦
console.log(Math.cosh(1).toFixed(10));             // 双曲余弦
输出
0.5000000000
0.5000000000
1.0000000000
0.7853981634
0.5000000000
1.1752011936
1.5430806348

语言差异说明

PHP:基本三角 sin/cos/tan、反三角 asin/acos/atan、双曲 sinh/cosh/tanh 全内置;参数一律弧度——角度入库先 deg2rad()(见 deg2rad_rad2deg 条目),否则 30 会被当成 30 弧度。

弧度教学:π/6 弧度 = 30°,sin(π/6) = 0.5;atan(1) = π/4 ≈ 0.7853981634——演示取固定弧度值 + %.10f 格式化断言,避开 ULP 字面量差异。

全语言同族对照:C/C++ <math.h>/std::、Java Math.、C# Math.、Go math.、Rust f64 方法、Python math、JS/TS Math——函数名映射各语言大小写惯例,全部原生内置、语义一致,「无差异」即本条对照结论。

反三角定义域:asin/acos 参数必须在 [-1, 1],越界返回 NAN(本机实测 PHP asin(2) 静默返回 NAN——生产代码先钳制);atan 无此限制。

往返断言:asin(sin(0.5)) 回到 0.5,%.10f 下 10 门语言全一致;更长的往返链(如 sin→asin→sin)可能差 1 ulp,限精度输出即环境无关。

双曲族:sinh(1) ≈ 1.1752011936、cosh(1) ≈ 1.5430806348,可用恒等式 cosh²x − sinh²x = 1 自查(本例两值第 10 位小数下仍满足)。

演示不取 tanh(1):其值 0.7615941559… 的第 10 位小数恰在舍入边界,个别实现对最后一位有 1 ulp 分歧——教学条目只采用远离舍入边界的断言值。

联动:双曲反函数 asinh/acosh/atanh 各语言均有(本条未演示);π 常数见 pi 条目。