跳转到内容

std.math.complex.cosh

标准库Zig 0.17.1官方原文
源码std/math/complex/cosh.zig

第 7 行

std.math.complex.cosh.cmath

别名

第 10 行

别名目标 std.math.complex

std.math.complex.cosh.Complex

别名

第 11 行

别名目标 std.math.complex.Complex

std.math.complex.cosh.cosh

函数

第 16 行

返回 z 的反双曲余弦。

pub fn cosh(z: anytype) Complex(@TypeOf(z.re, z.im))

std.math.complex.cosh.cosh32

函数

第 25 行

fn cosh32(z: Complex(f32)) Complex(f32)

参数

z: Complex(f32)

用法示例

test cosh32 {
    const epsilon = math.floatEps(f32);
    const a = Complex(f32).init(5, 3);
    const c = cosh(a);

    try testing.expectApproxEqAbs(-73.467300, c.re, epsilon);
    try testing.expectApproxEqAbs(10.471557, c.im, epsilon);
}

std.math.complex.cosh.cosh64

函数

第 88 行

fn cosh64(z: Complex(f64)) Complex(f64)

参数

z: Complex(f64)

用法示例

test cosh64 {
    const epsilon = math.floatEps(f64);
    const a = Complex(f64).init(5, 3);
    const c = cosh(a);

    try testing.expectApproxEqAbs(-73.46729221264526, c.re, epsilon);
    try testing.expectApproxEqAbs(10.471557674805572, c.im, epsilon);
}

std.math.complex.cosh.ldexp_cexp

别名

第 13 行

别名目标 std.math.complex.ldexp.ldexp_cexp

std.math.complex.cosh.math

别名

第 9 行

别名目标 std.math

std.math.complex.cosh.std

别名

第 7 行

别名目标 std

std.math.complex.cosh.testing

别名

第 8 行

别名目标 std.testing

源码

std/math/complex/cosh.zig

// 移植自采用 MIT 许可证的 musl:
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
//
// https://git.musl-libc.org/cgit/musl/tree/src/complex/ccoshf.c
// https://git.musl-libc.org/cgit/musl/tree/src/complex/ccosh.c

const std = @import("../../std.zig");
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;

const ldexp_cexp = @import("ldexp.zig").ldexp_cexp;

/// 返回 z 的反双曲余弦。
pub fn cosh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
    const T = @TypeOf(z.re, z.im);
    return switch (T) {
        f32 => cosh32(z),
        f64 => cosh64(z),
        else => @compileError("cosh not implemented for " ++ @typeName(z)),
    };
}

fn cosh32(z: Complex(f32)) Complex(f32) {
    const x = z.re;
    const y = z.im;

    const hx: u32 = @bitCast(x);
    const ix = hx & 0x7fffffff;

    const hy: u32 = @bitCast(y);
    const iy = hy & 0x7fffffff;

    if (ix < 0x7f800000 and iy < 0x7f800000) {
        if (iy == 0) {
            return Complex(f32).init(math.cosh(x), x * y);
        }
        // 较小的 x:正常情况
        if (ix < 0x41100000) {
            return Complex(f32).init(math.cosh(x) * @cos(y), math.sinh(x) * @sin(y));
        }

        // |x|>= 9,因此 cosh(x) ~= exp(|x|)
        if (ix < 0x42b17218) {
            // x < 88.7:exp(|x|) 不会溢出
            const h = @exp(@abs(x)) * 0.5;
            return Complex(f32).init(h * @cos(y), math.copysign(h, x) * @sin(y));
        }
        // x < 192.7:缩放以避免溢出
        else if (ix < 0x4340b1e7) {
            const v = Complex(f32).init(@abs(x), y);
            const r = ldexp_cexp(v, -1);
            return Complex(f32).init(r.re, r.im * math.copysign(@as(f32, 1.0), x));
        }
        // x >= 192.7:结果始终溢出
        else {
            const h = 0x1p127 * x;
            return Complex(f32).init(h * h * @cos(y), h * @sin(y));
        }
    }

    if (ix == 0 and iy >= 0x7f800000) {
        return Complex(f32).init(y - y, math.copysign(@as(f32, 0.0), x * (y - y)));
    }

    if (iy == 0 and ix >= 0x7f800000) {
        if (hx & 0x7fffff == 0) {
            return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), x) * y);
        }
        return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), (x + x) * y));
    }

    if (ix < 0x7f800000 and iy >= 0x7f800000) {
        return Complex(f32).init(y - y, x * (y - y));
    }

    if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) {
        if (iy >= 0x7f800000) {
            return Complex(f32).init(x * x, x * (y - y));
        }
        return Complex(f32).init((x * x) * @cos(y), x * @sin(y));
    }

    return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));
}

fn cosh64(z: Complex(f64)) Complex(f64) {
    const x = z.re;
    const y = z.im;

    const fx: u64 = @bitCast(x);
    const hx: u32 = @intCast(fx >> 32);
    const lx: u32 = @truncate(fx);
    const ix = hx & 0x7fffffff;

    const fy: u64 = @bitCast(y);
    const hy: u32 = @intCast(fy >> 32);
    const ly: u32 = @truncate(fy);
    const iy = hy & 0x7fffffff;

    // x、y 均为有限数的常见非异常情况
    if (ix < 0x7ff00000 and iy < 0x7ff00000) {
        if (iy | ly == 0) {
            return Complex(f64).init(math.cosh(x), x * y);
        }
        // 较小的 x:正常情况
        if (ix < 0x40360000) {
            return Complex(f64).init(math.cosh(x) * @cos(y), math.sinh(x) * @sin(y));
        }

        // |x|>= 22,因此 cosh(x) ~= exp(|x|)
        if (ix < 0x40862e42) {
            // x < 710:exp(|x|) 不会溢出
            const h = @exp(@abs(x)) * 0.5;
            return Complex(f64).init(h * @cos(y), math.copysign(h, x) * @sin(y));
        }
        // x < 1455:缩放以避免溢出
        else if (ix < 0x4096bbaa) {
            const v = Complex(f64).init(@abs(x), y);
            const r = ldexp_cexp(v, -1);
            return Complex(f64).init(r.re, r.im * math.copysign(@as(f64, 1.0), x));
        }
        // x >= 1455:结果始终溢出
        else {
            const h = 0x1p1023 * x;
            return Complex(f64).init(h * h * @cos(y), h * @sin(y));
        }
    }

    if (ix | lx == 0 and iy >= 0x7ff00000) {
        return Complex(f64).init(y - y, math.copysign(@as(f64, 0.0), x * (y - y)));
    }

    if (iy | ly == 0 and ix >= 0x7ff00000) {
        if ((hx & 0xfffff) | lx == 0) {
            return Complex(f64).init(x * x, math.copysign(@as(f64, 0.0), x) * y);
        }
        return Complex(f64).init(x * x, math.copysign(@as(f64, 0.0), (x + x) * y));
    }

    if (ix < 0x7ff00000 and iy >= 0x7ff00000) {
        return Complex(f64).init(y - y, x * (y - y));
    }

    if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) {
        if (iy >= 0x7ff00000) {
            return Complex(f64).init(x * x, x * (y - y));
        }
        return Complex(f64).init(x * x * @cos(y), x * @sin(y));
    }

    return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
}

test cosh32 {
    const epsilon = math.floatEps(f32);
    const a = Complex(f32).init(5, 3);
    const c = cosh(a);

    try testing.expectApproxEqAbs(-73.467300, c.re, epsilon);
    try testing.expectApproxEqAbs(10.471557, c.im, epsilon);
}

test cosh64 {
    const epsilon = math.floatEps(f64);
    const a = Complex(f64).init(5, 3);
    const c = cosh(a);

    try testing.expectApproxEqAbs(-73.46729221264526, c.re, epsilon);
    try testing.expectApproxEqAbs(10.471557674805572, c.im, epsilon);
}

test "cosh64 musl" {
    const epsilon = math.floatEps(f64);
    const a = Complex(f64).init(7.44648873421389e17, 1.6008058402057622e19);
    const c = cosh(a);

    try testing.expectApproxEqAbs(std.math.inf(f64), c.re, epsilon);
    try testing.expectApproxEqAbs(std.math.inf(f64), c.im, epsilon);
}