Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

tanh64

tanh.tanh64
fn tanh64(x: f64) f64

File

lib/std/math/tanh.zig:69

Code

fn tanh64(x: f64) f64 {
    const u = @as(u64, @bitCast(x));
    const ux = u & 0x7FFFFFFFFFFFFFFF;
    const w = @as(u32, @intCast(ux >> 32));
    const ax = @as(f64, @bitCast(ux));
    const sign = (u >> 63) != 0;

    var t: f64 = undefined;

    // |x| < log(3) / 2 ~= 0.5493 or nan
    if (w > 0x3FE193EA) {
        // |x| > 20 or nan
        if (w > 0x40340000) {
            t = 1.0 - 0 / ax;
        } else {
            t = math.expm1(2 * ax);
            t = 1 - 2 / (t + 2);
        }
    }
    // |x| > log(5 / 3) / 2 ~= 0.2554
    else if (w > 0x3FD058AE) {
        t = math.expm1(2 * ax);
        t = t / (t + 2);
    }
    // |x| >= 0x1.0p-1022
    else if (w >= 0x00100000) {
        t = math.expm1(-2 * ax);
        t = -t / (t + 2);
    }
    // |x| is subnormal
    else {
        mem.doNotOptimizeAway(@as(f32, @floatCast(ax)));
        t = ax;
    }

    return if (sign) -t else t;
}