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. Public Namespace

sinh

Public Namespace

Namespaces

Functions

pub fn expect(ok: bool) !void

This function is intended to be used only in tests. When ok is false, returns a test failure error.

pub fn expo2(x: anytype) @TypeOf(x)

Returns exp(x) / 2 for x >= log(maxFloat(T)).

pub fn maxInt(comptime T: type) comptime_int

Returns the maximum value of integer type T.

pub fn sinh(x: anytype) @TypeOf(x)

Returns the hyperbolic sine of x.

Special Cases:

  • sinh(+-0) = +-0
  • sinh(+-inf) = +-inf
  • sinh(nan) = nan
fn sinh32(x: f32) f32

sinh(x) = (exp(x) - 1 / exp(x)) / 2 = (exp(x) - 1 + (exp(x) - 1) / exp(x)) / 2 = x + x^3 / 6 + o(x^5)

fn sinh64(x: f64) f64

File

math/sinh.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Ported from musl, which is licensed under the MIT license:
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
//
// https://git.musl-libc.org/cgit/musl/tree/src/math/sinhf.c
// https://git.musl-libc.org/cgit/musl/tree/src/math/sinh.c

const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;
const expo2 = @import("expo2.zig").expo2;
const maxInt = std.math.maxInt;

/// Returns the hyperbolic sine of x.
///
/// Special Cases:
///  - sinh(+-0)   = +-0
///  - sinh(+-inf) = +-inf
///  - sinh(nan)   = nan
pub fn sinh(x: anytype) @TypeOf(x) {
    const T = @TypeOf(x);
    return switch (T) {
        f32 => sinh32(x),
        f64 => sinh64(x),
        else => @compileError("sinh not implemented for " ++ @typeName(T)),
    };
}

// sinh(x) = (exp(x) - 1 / exp(x)) / 2
//         = (exp(x) - 1 + (exp(x) - 1) / exp(x)) / 2
//         = x + x^3 / 6 + o(x^5)
fn sinh32(x: f32) f32 {
    const u = @as(u32, @bitCast(x));
    const ux = u & 0x7FFFFFFF;
    const ax = @as(f32, @bitCast(ux));

    if (x == 0.0 or math.isNan(x)) {
        return x;
    }

    var h: f32 = 0.5;
    if (u >> 31 != 0) {
        h = -h;
    }

    // |x| < log(FLT_MAX)
    if (ux < 0x42B17217) {
        const t = math.expm1(ax);
        if (ux < 0x3F800000) {
            if (ux < 0x3F800000 - (12 << 23)) {
                return x;
            } else {
                return h * (2 * t - t * t / (t + 1));
            }
        }
        return h * (t + t / (t + 1));
    }

    // |x| > log(FLT_MAX) or nan
    return 2 * h * expo2(ax);
}

fn sinh64(x: f64) f64 {
    const u = @as(u64, @bitCast(x));
    const w = @as(u32, @intCast(u >> 32)) & (maxInt(u32) >> 1);
    const ax = @as(f64, @bitCast(u & (maxInt(u64) >> 1)));

    if (x == 0.0 or math.isNan(x)) {
        return x;
    }

    var h: f32 = 0.5;
    if (u >> 63 != 0) {
        h = -h;
    }

    // |x| < log(FLT_MAX)
    if (w < 0x40862E42) {
        const t = math.expm1(ax);
        if (w < 0x3FF00000) {
            if (w < 0x3FF00000 - (26 << 20)) {
                return x;
            } else {
                return h * (2 * t - t * t / (t + 1));
            }
        }
        // NOTE: |x| > log(0x1p26) + eps could be h * exp(x)
        return h * (t + t / (t + 1));
    }

    // |x| > log(DBL_MAX) or nan
    return 2 * h * expo2(ax);
}

test sinh {
    try expect(sinh(@as(f32, 1.5)) == sinh32(1.5));
    try expect(sinh(@as(f64, 1.5)) == sinh64(1.5));
}

test sinh32 {
    const epsilon = 0.000001;

    try expect(math.approxEqAbs(f32, sinh32(0.0), 0.0, epsilon));
    try expect(math.approxEqAbs(f32, sinh32(0.2), 0.201336, epsilon));
    try expect(math.approxEqAbs(f32, sinh32(0.8923), 1.015512, epsilon));
    try expect(math.approxEqAbs(f32, sinh32(1.5), 2.129279, epsilon));
    try expect(math.approxEqAbs(f32, sinh32(-0.0), -0.0, epsilon));
    try expect(math.approxEqAbs(f32, sinh32(-0.2), -0.201336, epsilon));
    try expect(math.approxEqAbs(f32, sinh32(-0.8923), -1.015512, epsilon));
    try expect(math.approxEqAbs(f32, sinh32(-1.5), -2.129279, epsilon));
}

test sinh64 {
    const epsilon = 0.000001;

    try expect(math.approxEqAbs(f64, sinh64(0.0), 0.0, epsilon));
    try expect(math.approxEqAbs(f64, sinh64(0.2), 0.201336, epsilon));
    try expect(math.approxEqAbs(f64, sinh64(0.8923), 1.015512, epsilon));
    try expect(math.approxEqAbs(f64, sinh64(1.5), 2.129279, epsilon));
    try expect(math.approxEqAbs(f64, sinh64(-0.0), -0.0, epsilon));
    try expect(math.approxEqAbs(f64, sinh64(-0.2), -0.201336, epsilon));
    try expect(math.approxEqAbs(f64, sinh64(-0.8923), -1.015512, epsilon));
    try expect(math.approxEqAbs(f64, sinh64(-1.5), -2.129279, epsilon));
}

test "sinh32.special" {
    try expect(math.isPositiveZero(sinh32(0.0)));
    try expect(math.isNegativeZero(sinh32(-0.0)));
    try expect(math.isPositiveInf(sinh32(math.inf(f32))));
    try expect(math.isNegativeInf(sinh32(-math.inf(f32))));
    try expect(math.isNan(sinh32(math.nan(f32))));
}

test "sinh64.special" {
    try expect(math.isPositiveZero(sinh64(0.0)));
    try expect(math.isNegativeZero(sinh64(-0.0)));
    try expect(math.isPositiveInf(sinh64(math.inf(f64))));
    try expect(math.isNegativeInf(sinh64(-math.inf(f64))));
    try expect(math.isNan(sinh64(math.nan(f64))));
}