2019-04-30 23:15:57 -07:00
|
|
|
// Ported from musl, which is licensed under the MIT license:
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
|
2017-06-20 04:01:04 -07:00
|
|
|
//
|
2019-04-30 23:15:57 -07:00
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/tanhf.c
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/tanh.c
|
2017-06-20 04:01:04 -07:00
|
|
|
|
2017-10-14 23:04:21 -07:00
|
|
|
const builtin = @import("builtin");
|
2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("../std.zig");
|
2017-12-23 19:08:53 -08:00
|
|
|
const math = std.math;
|
2019-02-08 15:18:47 -08:00
|
|
|
const expect = std.testing.expect;
|
2017-06-20 04:01:04 -07:00
|
|
|
const expo2 = @import("expo2.zig").expo2;
|
2018-10-26 11:59:58 -07:00
|
|
|
const maxInt = std.math.maxInt;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the hyperbolic tangent of x.
|
|
|
|
///
|
|
|
|
/// Special Cases:
|
|
|
|
/// - sinh(+-0) = +-0
|
|
|
|
/// - sinh(+-inf) = +-1
|
|
|
|
/// - sinh(nan) = nan
|
2019-12-09 12:56:19 -08:00
|
|
|
pub fn tanh(x: var) @TypeOf(x) {
|
|
|
|
const T = @TypeOf(x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (T) {
|
2017-12-22 10:14:07 -08:00
|
|
|
f32 => tanh32(x),
|
|
|
|
f64 => tanh64(x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("tanh not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
|
|
|
|
// = (exp(2x) - 1) / (exp(2x) - 1 + 2)
|
|
|
|
// = (1 - exp(-2x)) / (exp(-2x) - 1 + 2)
|
2018-01-25 01:10:11 -08:00
|
|
|
fn tanh32(x: f32) f32 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const u = @bitCast(u32, x);
|
|
|
|
const ux = u & 0x7FFFFFFF;
|
|
|
|
const ax = @bitCast(f32, ux);
|
|
|
|
|
|
|
|
var t: f32 = undefined;
|
|
|
|
|
2017-06-20 23:21:11 -07:00
|
|
|
if (x == 0.0 or math.isNan(x)) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2017-06-16 01:26:10 -07:00
|
|
|
// |x| < log(3) / 2 ~= 0.5493 or nan
|
|
|
|
if (ux > 0x3F0C9F54) {
|
|
|
|
// |x| > 10
|
|
|
|
if (ux > 0x41200000) {
|
2017-06-20 23:21:11 -07:00
|
|
|
t = 1.0;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
|
|
|
t = math.expm1(2 * x);
|
|
|
|
t = 1 - 2 / (t + 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// |x| > log(5 / 3) / 2 ~= 0.2554
|
|
|
|
else if (ux > 0x3E82C578) {
|
|
|
|
t = math.expm1(2 * x);
|
|
|
|
t = t / (t + 2);
|
|
|
|
}
|
|
|
|
// |x| >= 0x1.0p-126
|
|
|
|
else if (ux >= 0x00800000) {
|
|
|
|
t = math.expm1(-2 * x);
|
|
|
|
t = -t / (t + 2);
|
|
|
|
}
|
|
|
|
// |x| is subnormal
|
|
|
|
else {
|
|
|
|
math.forceEval(x * x);
|
|
|
|
t = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u >> 31 != 0) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return -t;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
2017-12-21 21:50:30 -08:00
|
|
|
return t;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn tanh64(x: f64) f64 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const u = @bitCast(u64, x);
|
2018-06-16 23:57:07 -07:00
|
|
|
const w = @intCast(u32, u >> 32);
|
2018-10-26 11:59:58 -07:00
|
|
|
const ax = @bitCast(f64, u & (maxInt(u64) >> 1));
|
2017-06-16 01:26:10 -07:00
|
|
|
|
|
|
|
var t: f64 = undefined;
|
|
|
|
|
2017-06-20 04:01:04 -07:00
|
|
|
// TODO: Shouldn't need these checks.
|
2017-06-20 23:21:11 -07:00
|
|
|
if (x == 0.0 or math.isNan(x)) {
|
2017-06-20 04:01:04 -07:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2017-06-16 01:26:10 -07:00
|
|
|
// |x| < log(3) / 2 ~= 0.5493 or nan
|
2017-06-20 04:01:04 -07:00
|
|
|
if (w > 0x3FE193EA) {
|
2017-06-16 01:26:10 -07:00
|
|
|
// |x| > 20 or nan
|
|
|
|
if (w > 0x40340000) {
|
2017-06-20 23:21:11 -07:00
|
|
|
t = 1.0;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
|
|
|
t = math.expm1(2 * x);
|
|
|
|
t = 1 - 2 / (t + 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// |x| > log(5 / 3) / 2 ~= 0.2554
|
|
|
|
else if (w > 0x3FD058AE) {
|
|
|
|
t = math.expm1(2 * x);
|
|
|
|
t = t / (t + 2);
|
|
|
|
}
|
|
|
|
// |x| >= 0x1.0p-1022
|
|
|
|
else if (w >= 0x00100000) {
|
|
|
|
t = math.expm1(-2 * x);
|
|
|
|
t = -t / (t + 2);
|
|
|
|
}
|
|
|
|
// |x| is subnormal
|
|
|
|
else {
|
2018-06-16 23:57:07 -07:00
|
|
|
math.forceEval(@floatCast(f32, x));
|
2017-06-16 01:26:10 -07:00
|
|
|
t = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u >> 63 != 0) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return -t;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
2017-12-21 21:50:30 -08:00
|
|
|
return t;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.tanh" {
|
2019-11-06 20:25:57 -08:00
|
|
|
expect(tanh(@as(f32, 1.5)) == tanh32(1.5));
|
|
|
|
expect(tanh(@as(f64, 1.5)) == tanh64(1.5));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.tanh32" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(math.approxEq(f32, tanh32(0.0), 0.0, epsilon));
|
|
|
|
expect(math.approxEq(f32, tanh32(0.2), 0.197375, epsilon));
|
|
|
|
expect(math.approxEq(f32, tanh32(0.8923), 0.712528, epsilon));
|
|
|
|
expect(math.approxEq(f32, tanh32(1.5), 0.905148, epsilon));
|
|
|
|
expect(math.approxEq(f32, tanh32(37.45), 1.0, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.tanh64" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(math.approxEq(f64, tanh64(0.0), 0.0, epsilon));
|
|
|
|
expect(math.approxEq(f64, tanh64(0.2), 0.197375, epsilon));
|
|
|
|
expect(math.approxEq(f64, tanh64(0.8923), 0.712528, epsilon));
|
|
|
|
expect(math.approxEq(f64, tanh64(1.5), 0.905148, epsilon));
|
|
|
|
expect(math.approxEq(f64, tanh64(37.45), 1.0, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.tanh32.special" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(tanh32(0.0) == 0.0);
|
|
|
|
expect(tanh32(-0.0) == -0.0);
|
|
|
|
expect(tanh32(math.inf(f32)) == 1.0);
|
|
|
|
expect(tanh32(-math.inf(f32)) == -1.0);
|
|
|
|
expect(math.isNan(tanh32(math.nan(f32))));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "math.tanh64.special" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(tanh64(0.0) == 0.0);
|
|
|
|
expect(tanh64(-0.0) == -0.0);
|
|
|
|
expect(tanh64(math.inf(f64)) == 1.0);
|
|
|
|
expect(tanh64(-math.inf(f64)) == -1.0);
|
|
|
|
expect(math.isNan(tanh64(math.nan(f64))));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|