master
  1// Ported from musl, which is licensed under the MIT license:
  2// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
  3//
  4// https://git.musl-libc.org/cgit/musl/tree/src/math/atanhf.c
  5// https://git.musl-libc.org/cgit/musl/tree/src/math/atanh.c
  6
  7const std = @import("../std.zig");
  8const math = std.math;
  9const mem = std.mem;
 10const expect = std.testing.expect;
 11const maxInt = std.math.maxInt;
 12
 13/// Returns the hyperbolic arc-tangent of x.
 14///
 15/// Special Cases:
 16///  - atanh(+-1) = +-inf with signal
 17///  - atanh(x)   = nan if |x| > 1 with signal
 18///  - atanh(nan) = nan
 19pub fn atanh(x: anytype) @TypeOf(x) {
 20    const T = @TypeOf(x);
 21    return switch (T) {
 22        f32 => atanh_32(x),
 23        f64 => atanh_64(x),
 24        else => @compileError("atanh not implemented for " ++ @typeName(T)),
 25    };
 26}
 27
 28// atanh(x) = log((1 + x) / (1 - x)) / 2 = log1p(2x / (1 - x)) / 2 ~= x + x^3 / 3 + o(x^5)
 29fn atanh_32(x: f32) f32 {
 30    const u = @as(u32, @bitCast(x));
 31    const i = u & 0x7FFFFFFF;
 32    const s = u >> 31;
 33
 34    var y = @as(f32, @bitCast(i)); // |x|
 35
 36    if (y == 1.0) {
 37        return math.copysign(math.inf(f32), x);
 38    }
 39
 40    if (u < 0x3F800000 - (1 << 23)) {
 41        if (u < 0x3F800000 - (32 << 23)) {
 42            // underflow
 43            if (u < (1 << 23)) {
 44                mem.doNotOptimizeAway(y * y);
 45            }
 46        }
 47        // |x| < 0.5
 48        else {
 49            y = 0.5 * math.log1p(2 * y + 2 * y * y / (1 - y));
 50        }
 51    } else {
 52        y = 0.5 * math.log1p(2 * (y / (1 - y)));
 53    }
 54
 55    return if (s != 0) -y else y;
 56}
 57
 58fn atanh_64(x: f64) f64 {
 59    const u: u64 = @bitCast(x);
 60    const e = (u >> 52) & 0x7FF;
 61    const s = u >> 63;
 62
 63    var y: f64 = @bitCast(u & (maxInt(u64) >> 1)); // |x|
 64
 65    if (y == 1.0) {
 66        return math.copysign(math.inf(f64), x);
 67    }
 68
 69    if (e < 0x3FF - 1) {
 70        if (e < 0x3FF - 32) {
 71            // underflow
 72            if (e == 0) {
 73                mem.doNotOptimizeAway(@as(f32, @floatCast(y)));
 74            }
 75        }
 76        // |x| < 0.5
 77        else {
 78            y = 0.5 * math.log1p(2 * y + 2 * y * y / (1 - y));
 79        }
 80    } else {
 81        y = 0.5 * math.log1p(2 * (y / (1 - y)));
 82    }
 83
 84    return if (s != 0) -y else y;
 85}
 86
 87test atanh {
 88    try expect(atanh(@as(f32, 0.0)) == atanh_32(0.0));
 89    try expect(atanh(@as(f64, 0.0)) == atanh_64(0.0));
 90}
 91
 92test atanh_32 {
 93    const epsilon = 0.000001;
 94
 95    try expect(math.approxEqAbs(f32, atanh_32(0.0), 0.0, epsilon));
 96    try expect(math.approxEqAbs(f32, atanh_32(0.2), 0.202733, epsilon));
 97    try expect(math.approxEqAbs(f32, atanh_32(0.8923), 1.433099, epsilon));
 98}
 99
100test atanh_64 {
101    const epsilon = 0.000001;
102
103    try expect(math.approxEqAbs(f64, atanh_64(0.0), 0.0, epsilon));
104    try expect(math.approxEqAbs(f64, atanh_64(0.2), 0.202733, epsilon));
105    try expect(math.approxEqAbs(f64, atanh_64(0.8923), 1.433099, epsilon));
106}
107
108test "atanh32.special" {
109    try expect(math.isPositiveInf(atanh_32(1)));
110    try expect(math.isNegativeInf(atanh_32(-1)));
111    try expect(math.isNan(atanh_32(1.5)));
112    try expect(math.isNan(atanh_32(-1.5)));
113    try expect(math.isNan(atanh_32(math.nan(f32))));
114}
115
116test "atanh64.special" {
117    try expect(math.isPositiveInf(atanh_64(1)));
118    try expect(math.isNegativeInf(atanh_64(-1)));
119    try expect(math.isNan(atanh_64(1.5)));
120    try expect(math.isNan(atanh_64(-1.5)));
121    try expect(math.isNan(atanh_64(math.nan(f64))));
122}