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
141
142
143
144
145
146
147
148
149
150
151
|
// 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/tanhf.c
// https://git.musl-libc.org/cgit/musl/tree/src/math/tanh.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 tangent of x.
///
/// Special Cases:
/// - sinh(+-0) = +-0
/// - sinh(+-inf) = +-1
/// - sinh(nan) = nan
pub fn tanh(x: anytype) @TypeOf(x) {
const T = @TypeOf(x);
return switch (T) {
f32 => tanh32(x),
f64 => tanh64(x),
else => @compileError("tanh not implemented for " ++ @typeName(T)),
};
}
// tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
// = (exp(2x) - 1) / (exp(2x) - 1 + 2)
// = (1 - exp(-2x)) / (exp(-2x) - 1 + 2)
fn tanh32(x: f32) f32 {
const u = @bitCast(u32, x);
const ux = u & 0x7FFFFFFF;
const ax = @bitCast(f32, 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 {
math.doNotOptimizeAway(ax * ax);
t = ax;
}
return if (sign) -t else t;
}
fn tanh64(x: f64) f64 {
const u = @bitCast(u64, x);
const ux = u & 0x7FFFFFFFFFFFFFFF;
const w = @intCast(u32, ux >> 32);
const ax = @bitCast(f64, 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 {
math.doNotOptimizeAway(@floatCast(f32, ax));
t = ax;
}
return if (sign) -t else t;
}
test "math.tanh" {
try expect(tanh(@as(f32, 1.5)) == tanh32(1.5));
try expect(tanh(@as(f64, 1.5)) == tanh64(1.5));
}
test "math.tanh32" {
const epsilon = 0.000001;
try expect(math.approxEqAbs(f32, tanh32(0.0), 0.0, epsilon));
try expect(math.approxEqAbs(f32, tanh32(0.2), 0.197375, epsilon));
try expect(math.approxEqAbs(f32, tanh32(0.8923), 0.712528, epsilon));
try expect(math.approxEqAbs(f32, tanh32(1.5), 0.905148, epsilon));
try expect(math.approxEqAbs(f32, tanh32(37.45), 1.0, epsilon));
try expect(math.approxEqAbs(f32, tanh32(-0.8923), -0.712528, epsilon));
try expect(math.approxEqAbs(f32, tanh32(-1.5), -0.905148, epsilon));
try expect(math.approxEqAbs(f32, tanh32(-37.45), -1.0, epsilon));
}
test "math.tanh64" {
const epsilon = 0.000001;
try expect(math.approxEqAbs(f64, tanh64(0.0), 0.0, epsilon));
try expect(math.approxEqAbs(f64, tanh64(0.2), 0.197375, epsilon));
try expect(math.approxEqAbs(f64, tanh64(0.8923), 0.712528, epsilon));
try expect(math.approxEqAbs(f64, tanh64(1.5), 0.905148, epsilon));
try expect(math.approxEqAbs(f64, tanh64(37.45), 1.0, epsilon));
try expect(math.approxEqAbs(f64, tanh64(-0.8923), -0.712528, epsilon));
try expect(math.approxEqAbs(f64, tanh64(-1.5), -0.905148, epsilon));
try expect(math.approxEqAbs(f64, tanh64(-37.45), -1.0, epsilon));
}
test "math.tanh32.special" {
try expect(tanh32(0.0) == 0.0);
try expect(tanh32(-0.0) == -0.0);
try expect(tanh32(math.inf(f32)) == 1.0);
try expect(tanh32(-math.inf(f32)) == -1.0);
try expect(math.isNan(tanh32(math.nan(f32))));
}
test "math.tanh64.special" {
try expect(tanh64(0.0) == 0.0);
try expect(tanh64(-0.0) == -0.0);
try expect(tanh64(math.inf(f64)) == 1.0);
try expect(tanh64(-math.inf(f64)) == -1.0);
try expect(math.isNan(tanh64(math.nan(f64))));
}
|