2017-06-20 04:01:04 -07:00
|
|
|
// Special Cases:
|
|
|
|
//
|
|
|
|
// - acosh(x) = snan if x < 1
|
|
|
|
// - acosh(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-16 01:26:10 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn acosh(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 => acosh32(x),
|
|
|
|
f64 => acosh64(x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("acosh not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// acosh(x) = log(x + sqrt(x * x - 1))
|
2018-01-25 01:10:11 -08:00
|
|
|
fn acosh32(x: f32) f32 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const u = @bitCast(u32, x);
|
|
|
|
const i = u & 0x7FFFFFFF;
|
|
|
|
|
|
|
|
// |x| < 2, invalid if x < 1 or nan
|
|
|
|
if (i < 0x3F800000 + (1 << 23)) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return math.log1p(x - 1 + math.sqrt((x - 1) * (x - 1) + 2 * (x - 1)));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
// |x| < 0x1p12
|
|
|
|
else if (i < 0x3F800000 + (12 << 23)) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return math.ln(2 * x - 1 / (x + math.sqrt(x * x - 1)));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
// |x| >= 0x1p12
|
|
|
|
else {
|
2017-12-21 21:50:30 -08:00
|
|
|
return math.ln(x) + 0.693147180559945309417232121458176568;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn acosh64(x: f64) f64 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const u = @bitCast(u64, x);
|
|
|
|
const e = (u >> 52) & 0x7FF;
|
|
|
|
|
|
|
|
// |x| < 2, invalid if x < 1 or nan
|
|
|
|
if (e < 0x3FF + 1) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return math.log1p(x - 1 + math.sqrt((x - 1) * (x - 1) + 2 * (x - 1)));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
// |x| < 0x1p26
|
|
|
|
else if (e < 0x3FF + 26) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return math.ln(2 * x - 1 / (x + math.sqrt(x * x - 1)));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
// |x| >= 0x1p26 or nan
|
|
|
|
else {
|
2017-12-21 21:50:30 -08:00
|
|
|
return math.ln(x) + 0.693147180559945309417232121458176568;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.acosh" {
|
2017-08-28 01:28:42 -07:00
|
|
|
assert(acosh(f32(1.5)) == acosh32(1.5));
|
|
|
|
assert(acosh(f64(1.5)) == acosh64(1.5));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.acosh32" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
assert(math.approxEq(f32, acosh32(1.5), 0.962424, epsilon));
|
|
|
|
assert(math.approxEq(f32, acosh32(37.45), 4.315976, epsilon));
|
|
|
|
assert(math.approxEq(f32, acosh32(89.123), 5.183133, epsilon));
|
|
|
|
assert(math.approxEq(f32, acosh32(123123.234375), 12.414088, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.acosh64" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
assert(math.approxEq(f64, acosh64(1.5), 0.962424, epsilon));
|
|
|
|
assert(math.approxEq(f64, acosh64(37.45), 4.315976, epsilon));
|
|
|
|
assert(math.approxEq(f64, acosh64(89.123), 5.183133, epsilon));
|
|
|
|
assert(math.approxEq(f64, acosh64(123123.234375), 12.414088, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.acosh32.special" {
|
|
|
|
assert(math.isNan(acosh32(math.nan(f32))));
|
|
|
|
assert(math.isSignalNan(acosh32(0.5)));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.acosh64.special" {
|
|
|
|
assert(math.isNan(acosh64(math.nan(f64))));
|
|
|
|
assert(math.isSignalNan(acosh64(0.5)));
|
|
|
|
}
|