跳转到内容

std.math.complex.atan

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

第 7 行

std.math.complex.atan.atan

函数

第 14 行

返回 z 的反正切。

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

std.math.complex.atan.atan32

函数

第 39 行

fn atan32(z: Complex(f32)) Complex(f32)

参数

z: Complex(f32)

用法示例

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

    try testing.expectApproxEqAbs(1.423679, c.re, epsilon);
    try testing.expectApproxEqAbs(0.086569, c.im, epsilon);
}

std.math.complex.atan.atan64

函数

第 73 行

fn atan64(z: Complex(f64)) Complex(f64)

参数

z: Complex(f64)

用法示例

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

    try testing.expectApproxEqAbs(1.4236790442393028, c.re, epsilon);
    try testing.expectApproxEqAbs(0.08656905917945844, c.im, epsilon);
}

std.math.complex.atan.cmath

别名

第 10 行

别名目标 std.math.complex

std.math.complex.atan.Complex

别名

第 11 行

别名目标 std.math.complex.Complex

std.math.complex.atan.math

别名

第 9 行

别名目标 std.math

std.math.complex.atan.redupif32

函数

第 23 行

fn redupif32(x: f32) f32

参数

x: f32

std.math.complex.atan.redupif64

函数

第 57 行

fn redupif64(x: f64) f64

参数

x: f64

std.math.complex.atan.std

别名

第 7 行

别名目标 std

std.math.complex.atan.testing

别名

第 8 行

别名目标 std.testing

源码

std/math/complex/atan.zig

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

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

/// 返回 z 的反正切。
pub fn atan(z: anytype) Complex(@TypeOf(z.re, z.im)) {
    const T = @TypeOf(z.re, z.im);
    return switch (T) {
        f32 => atan32(z),
        f64 => atan64(z),
        else => @compileError("atan not implemented for " ++ @typeName(z)),
    };
}

fn redupif32(x: f32) f32 {
    const DP1 = 3.140625;
    const DP2 = 9.67502593994140625e-4;
    const DP3 = 1.509957990978376432e-7;

    var t = x / math.pi;
    if (t >= 0.0) {
        t += 0.5;
    } else {
        t -= 0.5;
    }

    const u: f32 = @trunc(t);
    return ((x - u * DP1) - u * DP2) - u * DP3;
}

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

    const x2 = x * x;
    var a = 1.0 - x2 - (y * y);

    var t = 0.5 * math.atan2(2.0 * x, a);
    const w = redupif32(t);

    t = y - 1.0;
    a = x2 + t * t;

    t = y + 1.0;
    a = (x2 + (t * t)) / a;
    return Complex(f32).init(w, 0.25 * @log(a));
}

fn redupif64(x: f64) f64 {
    const DP1 = 3.14159265160560607910;
    const DP2 = 1.98418714791870343106e-9;
    const DP3 = 1.14423774522196636802e-17;

    var t = x / math.pi;
    if (t >= 0.0) {
        t += 0.5;
    } else {
        t -= 0.5;
    }

    const u: f64 = @trunc(t);
    return ((x - u * DP1) - u * DP2) - u * DP3;
}

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

    const x2 = x * x;
    var a = 1.0 - x2 - (y * y);

    var t = 0.5 * math.atan2(2.0 * x, a);
    const w = redupif64(t);

    t = y - 1.0;
    a = x2 + t * t;

    t = y + 1.0;
    a = (x2 + (t * t)) / a;
    return Complex(f64).init(w, 0.25 * @log(a));
}

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

    try testing.expectApproxEqAbs(1.423679, c.re, epsilon);
    try testing.expectApproxEqAbs(0.086569, c.im, epsilon);
}

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

    try testing.expectApproxEqAbs(1.4236790442393028, c.re, epsilon);
    try testing.expectApproxEqAbs(0.08656905917945844, c.im, epsilon);
}