2017-06-20 04:01:04 -07:00
|
|
|
// Special Cases:
|
|
|
|
//
|
|
|
|
// - atanh(+-1) = +-inf with signal
|
|
|
|
// - atanh(x) = nan if |x| > 1 with signal
|
|
|
|
// - atanh(nan) = nan
|
|
|
|
|
2017-06-16 01:26:10 -07:00
|
|
|
const math = @import("index.zig");
|
|
|
|
const assert = @import("../debug.zig").assert;
|
|
|
|
|
2017-08-28 01:28:42 -07:00
|
|
|
pub fn atanh(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 => atanh_32(x),
|
|
|
|
f64 => atanh_64(x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("atanh not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// atanh(x) = log((1 + x) / (1 - x)) / 2 = log1p(2x / (1 - x)) / 2 ~= x + x^3 / 3 + o(x^5)
|
2017-06-19 11:36:33 -07:00
|
|
|
fn atanh_32(x: f32) -> f32 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const u = @bitCast(u32, x);
|
|
|
|
const i = u & 0x7FFFFFFF;
|
|
|
|
const s = u >> 31;
|
|
|
|
|
|
|
|
var y = @bitCast(f32, i); // |x|
|
|
|
|
|
2017-06-20 04:01:04 -07:00
|
|
|
if (y == 1.0) {
|
|
|
|
return math.copysign(f32, math.inf(f32), x);
|
|
|
|
}
|
|
|
|
|
2017-06-16 01:26:10 -07:00
|
|
|
if (u < 0x3F800000 - (1 << 23)) {
|
|
|
|
if (u < 0x3F800000 - (32 << 23)) {
|
|
|
|
// underflow
|
|
|
|
if (u < (1 << 23)) {
|
2017-12-21 21:50:30 -08:00
|
|
|
math.forceEval(y * y);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// |x| < 0.5
|
|
|
|
else {
|
|
|
|
y = 0.5 * math.log1p(2 * y + 2 * y * y / (1 - y));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
y = 0.5 * math.log1p(2 * (y / (1 - y)));
|
|
|
|
}
|
|
|
|
|
2017-12-21 21:50:30 -08:00
|
|
|
return if (s != 0) -y else y;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
fn atanh_64(x: f64) -> f64 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const u = @bitCast(u64, x);
|
|
|
|
const e = (u >> 52) & 0x7FF;
|
|
|
|
const s = u >> 63;
|
|
|
|
|
|
|
|
var y = @bitCast(f64, u & (@maxValue(u64) >> 1)); // |x|
|
|
|
|
|
2017-06-20 04:01:04 -07:00
|
|
|
if (y == 1.0) {
|
|
|
|
return math.copysign(f64, math.inf(f64), x);
|
|
|
|
}
|
|
|
|
|
2017-06-16 01:26:10 -07:00
|
|
|
if (e < 0x3FF - 1) {
|
|
|
|
if (e < 0x3FF - 32) {
|
|
|
|
// underflow
|
|
|
|
if (e == 0) {
|
|
|
|
math.forceEval(f32(y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// |x| < 0.5
|
|
|
|
else {
|
|
|
|
y = 0.5 * math.log1p(2 * y + 2 * y * y / (1 - y));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
y = 0.5 * math.log1p(2 * (y / (1 - y)));
|
|
|
|
}
|
|
|
|
|
2017-12-21 21:50:30 -08:00
|
|
|
return if (s != 0) -y else y;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.atanh" {
|
|
|
|
assert(atanh(f32(0.0)) == atanh_32(0.0));
|
|
|
|
assert(atanh(f64(0.0)) == atanh_64(0.0));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.atanh_32" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
assert(math.approxEq(f32, atanh_32(0.0), 0.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atanh_32(0.2), 0.202733, epsilon));
|
|
|
|
assert(math.approxEq(f32, atanh_32(0.8923), 1.433099, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.atanh_64" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
assert(math.approxEq(f64, atanh_64(0.0), 0.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atanh_64(0.2), 0.202733, epsilon));
|
|
|
|
assert(math.approxEq(f64, atanh_64(0.8923), 1.433099, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.atanh32.special" {
|
|
|
|
assert(math.isPositiveInf(atanh_32(1)));
|
|
|
|
assert(math.isNegativeInf(atanh_32(-1)));
|
|
|
|
assert(math.isSignalNan(atanh_32(1.5)));
|
|
|
|
assert(math.isSignalNan(atanh_32(-1.5)));
|
|
|
|
assert(math.isNan(atanh_32(math.nan(f32))));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.atanh64.special" {
|
|
|
|
assert(math.isPositiveInf(atanh_64(1)));
|
|
|
|
assert(math.isNegativeInf(atanh_64(-1)));
|
|
|
|
assert(math.isSignalNan(atanh_64(1.5)));
|
|
|
|
assert(math.isSignalNan(atanh_64(-1.5)));
|
|
|
|
assert(math.isNan(atanh_64(math.nan(f64))));
|
|
|
|
}
|