Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
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 standard library. Private Function

tanh32

Private Function
std.math.tanh.tanh32
fn tanh32(x: f32) f32

tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x)) = (exp(2x) - 1) / (exp(2x) - 1 + 2) = (1 - exp(-2x)) / (exp(-2x) - 1 + 2)

Params

  • x: f32

File

math/tanh.zig:32
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
fn tanh32(x: f32) f32 {
    const u = @as(u32, @bitCast(x));
    const ux = u & 0x7FFFFFFF;
    const ax = @as(f32, @bitCast(ux));
    const sign = (u >> 31) != 0;

    var t: f32 = undefined;

    // |x| < log(3) / 2 ~= 0.5493 or nan
    if (ux > 0x3F0C9F54) {
        // |x| > 10
        if (ux > 0x41200000) {
            t = 1.0 + 0 / x;
        } else {
            t = math.expm1(2 * ax);
            t = 1 - 2 / (t + 2);
        }
    }
    // |x| > log(5 / 3) / 2 ~= 0.2554
    else if (ux > 0x3E82C578) {
        t = math.expm1(2 * ax);
        t = t / (t + 2);
    }
    // |x| >= 0x1.0p-126
    else if (ux >= 0x00800000) {
        t = math.expm1(-2 * ax);
        t = -t / (t + 2);
    }
    // |x| is subnormal
    else {
        mem.doNotOptimizeAway(ax * ax);
        t = ax;
    }

    return if (sign) -t else t;
}