2017-06-20 04:01:04 -07:00
|
|
|
// Special Cases:
|
|
|
|
//
|
|
|
|
// - sinh(+-0) = +-0
|
|
|
|
// - sinh(+-inf) = +-inf
|
|
|
|
// - sinh(nan) = nan
|
|
|
|
|
2017-10-14 23:04:21 -07:00
|
|
|
const builtin = @import("builtin");
|
2017-12-23 19:08:53 -08:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const math = std.math;
|
|
|
|
const assert = std.debug.assert;
|
2017-06-20 04:01:04 -07:00
|
|
|
const expo2 = @import("expo2.zig").expo2;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn sinh(x: var) @typeOf(x) {
|
2017-06-16 01:26:10 -07:00
|
|
|
const T = @typeOf(x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (T) {
|
2017-12-22 10:14:07 -08:00
|
|
|
f32 => sinh32(x),
|
|
|
|
f64 => sinh64(x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("sinh not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// sinh(x) = (exp(x) - 1 / exp(x)) / 2
|
|
|
|
// = (exp(x) - 1 + (exp(x) - 1) / exp(x)) / 2
|
|
|
|
// = x + x^3 / 6 + o(x^5)
|
2018-01-25 01:10:11 -08:00
|
|
|
fn sinh32(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);
|
|
|
|
|
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
|
|
|
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
|
2017-12-21 21:50:30 -08:00
|
|
|
return 2 * h * expo2(ax);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn sinh64(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);
|
2017-06-16 01:26:10 -07:00
|
|
|
const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
|
|
|
|
|
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
|
|
|
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
|
2017-12-21 21:50:30 -08:00
|
|
|
return 2 * h * expo2(ax);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.sinh" {
|
|
|
|
assert(sinh(f32(1.5)) == sinh32(1.5));
|
|
|
|
assert(sinh(f64(1.5)) == sinh64(1.5));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.sinh32" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
assert(math.approxEq(f32, sinh32(0.0), 0.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, sinh32(0.2), 0.201336, epsilon));
|
|
|
|
assert(math.approxEq(f32, sinh32(0.8923), 1.015512, epsilon));
|
|
|
|
assert(math.approxEq(f32, sinh32(1.5), 2.129279, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.sinh64" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
assert(math.approxEq(f64, sinh64(0.0), 0.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, sinh64(0.2), 0.201336, epsilon));
|
|
|
|
assert(math.approxEq(f64, sinh64(0.8923), 1.015512, epsilon));
|
|
|
|
assert(math.approxEq(f64, sinh64(1.5), 2.129279, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.sinh32.special" {
|
|
|
|
assert(sinh32(0.0) == 0.0);
|
|
|
|
assert(sinh32(-0.0) == -0.0);
|
|
|
|
assert(math.isPositiveInf(sinh32(math.inf(f32))));
|
|
|
|
assert(math.isNegativeInf(sinh32(-math.inf(f32))));
|
|
|
|
assert(math.isNan(sinh32(math.nan(f32))));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.sinh64.special" {
|
|
|
|
assert(sinh64(0.0) == 0.0);
|
|
|
|
assert(sinh64(-0.0) == -0.0);
|
|
|
|
assert(math.isPositiveInf(sinh64(math.inf(f64))));
|
|
|
|
assert(math.isNegativeInf(sinh64(-math.inf(f64))));
|
|
|
|
assert(math.isNan(sinh64(math.nan(f64))));
|
|
|
|
}
|