const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn asinh(x: anytype) @TypeOf(x) {
const T = @TypeOf(x);
return switch (T) {
f32 => asinh32(x),
f64 => asinh64(x),
else => @compileError("asinh not implemented for " ++ @typeName(T)),
};
}
fn asinh32(x: f32) f32 {
const u = @bitCast(u32, x);
const i = u & 0x7FFFFFFF;
const s = i >> 31;
var rx = @bitCast(f32, i);
if (math.isNegativeInf(x)) {
return x;
}
if (i >= 0x3F800000 + (12 << 23)) {
rx = @log(rx) + 0.69314718055994530941723212145817656;
}
else if (i >= 0x3F800000 + (1 << 23)) {
rx = @log(2 * x + 1 / (@sqrt(x * x + 1) + x));
}
else if (i >= 0x3F800000 - (12 << 23)) {
rx = math.log1p(x + x * x / (@sqrt(x * x + 1) + 1));
}
else {
math.doNotOptimizeAway(x + 0x1.0p120);
}
return if (s != 0) -rx else rx;
}
fn asinh64(x: f64) f64 {
const u = @bitCast(u64, x);
const e = (u >> 52) & 0x7FF;
const s = e >> 63;
var rx = @bitCast(f64, u & (maxInt(u64) >> 1));
if (math.isNegativeInf(x)) {
return x;
}
if (e >= 0x3FF + 26) {
rx = @log(rx) + 0.693147180559945309417232121458176568;
}
else if (e >= 0x3FF + 1) {
rx = @log(2 * x + 1 / (@sqrt(x * x + 1) + x));
}
else if (e >= 0x3FF - 26) {
rx = math.log1p(x + x * x / (@sqrt(x * x + 1) + 1));
}
else {
math.doNotOptimizeAway(x + 0x1.0p120);
}
return if (s != 0) -rx else rx;
}
test "math.asinh" {
try expect(asinh(@as(f32, 0.0)) == asinh32(0.0));
try expect(asinh(@as(f64, 0.0)) == asinh64(0.0));
}
test "math.asinh32" {
const epsilon = 0.000001;
try expect(math.approxEqAbs(f32, asinh32(0.0), 0.0, epsilon));
try expect(math.approxEqAbs(f32, asinh32(-0.2), -0.198690, epsilon));
try expect(math.approxEqAbs(f32, asinh32(0.2), 0.198690, epsilon));
try expect(math.approxEqAbs(f32, asinh32(0.8923), 0.803133, epsilon));
try expect(math.approxEqAbs(f32, asinh32(1.5), 1.194763, epsilon));
try expect(math.approxEqAbs(f32, asinh32(37.45), 4.316332, epsilon));
try expect(math.approxEqAbs(f32, asinh32(89.123), 5.183196, epsilon));
try expect(math.approxEqAbs(f32, asinh32(123123.234375), 12.414088, epsilon));
}
test "math.asinh64" {
const epsilon = 0.000001;
try expect(math.approxEqAbs(f64, asinh64(0.0), 0.0, epsilon));
try expect(math.approxEqAbs(f64, asinh64(-0.2), -0.198690, epsilon));
try expect(math.approxEqAbs(f64, asinh64(0.2), 0.198690, epsilon));
try expect(math.approxEqAbs(f64, asinh64(0.8923), 0.803133, epsilon));
try expect(math.approxEqAbs(f64, asinh64(1.5), 1.194763, epsilon));
try expect(math.approxEqAbs(f64, asinh64(37.45), 4.316332, epsilon));
try expect(math.approxEqAbs(f64, asinh64(89.123), 5.183196, epsilon));
try expect(math.approxEqAbs(f64, asinh64(123123.234375), 12.414088, epsilon));
}
test "math.asinh32.special" {
try expect(asinh32(0.0) == 0.0);
try expect(asinh32(-0.0) == -0.0);
try expect(math.isPositiveInf(asinh32(math.inf(f32))));
try expect(math.isNegativeInf(asinh32(-math.inf(f32))));
try expect(math.isNan(asinh32(math.nan(f32))));
}
test "math.asinh64.special" {
try expect(asinh64(0.0) == 0.0);
try expect(asinh64(-0.0) == -0.0);
try expect(math.isPositiveInf(asinh64(math.inf(f64))));
try expect(math.isNegativeInf(asinh64(-math.inf(f64))));
try expect(math.isNan(asinh64(math.nan(f64))));
}