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/coshf.c
5// https://git.musl-libc.org/cgit/musl/tree/src/math/cosh.c
6
7const std = @import("../std.zig");
8const math = std.math;
9const expo2 = @import("expo2.zig").expo2;
10const expect = std.testing.expect;
11const maxInt = std.math.maxInt;
12
13/// Returns the hyperbolic cosine of x.
14///
15/// Special Cases:
16/// - cosh(+-0) = 1
17/// - cosh(+-inf) = +inf
18/// - cosh(nan) = nan
19pub fn cosh(x: anytype) @TypeOf(x) {
20 const T = @TypeOf(x);
21 return switch (T) {
22 f32 => cosh32(x),
23 f64 => cosh64(x),
24 else => @compileError("cosh not implemented for " ++ @typeName(T)),
25 };
26}
27
28// cosh(x) = (exp(x) + 1 / exp(x)) / 2
29// = 1 + 0.5 * (exp(x) - 1) * (exp(x) - 1) / exp(x)
30// = 1 + (x * x) / 2 + o(x^4)
31fn cosh32(x: f32) f32 {
32 const u = @as(u32, @bitCast(x));
33 const ux = u & 0x7FFFFFFF;
34 const ax = @as(f32, @bitCast(ux));
35
36 // |x| < log(2)
37 if (ux < 0x3F317217) {
38 if (ux < 0x3F800000 - (12 << 23)) {
39 math.raiseOverflow();
40 return 1.0;
41 }
42 const t = math.expm1(ax);
43 return 1 + t * t / (2 * (1 + t));
44 }
45
46 // |x| < log(FLT_MAX)
47 if (ux < 0x42B17217) {
48 const t = @exp(ax);
49 return 0.5 * (t + 1 / t);
50 }
51
52 // |x| > log(FLT_MAX) or nan
53 return expo2(ax);
54}
55
56fn cosh64(x: f64) f64 {
57 const u = @as(u64, @bitCast(x));
58 const w = @as(u32, @intCast(u >> 32)) & (maxInt(u32) >> 1);
59 const ax = @as(f64, @bitCast(u & (maxInt(u64) >> 1)));
60
61 // TODO: Shouldn't need this explicit check.
62 if (x == 0.0) {
63 return 1.0;
64 }
65
66 // |x| < log(2)
67 if (w < 0x3FE62E42) {
68 if (w < 0x3FF00000 - (26 << 20)) {
69 if (x != 0) {
70 math.raiseInexact();
71 }
72 return 1.0;
73 }
74 const t = math.expm1(ax);
75 return 1 + t * t / (2 * (1 + t));
76 }
77
78 // |x| < log(DBL_MAX)
79 if (w < 0x40862E42) {
80 const t = @exp(ax);
81 // NOTE: If x > log(0x1p26) then 1/t is not required.
82 return 0.5 * (t + 1 / t);
83 }
84
85 // |x| > log(CBL_MAX) or nan
86 return expo2(ax);
87}
88
89test cosh {
90 try expect(cosh(@as(f32, 1.5)) == cosh32(1.5));
91 try expect(cosh(@as(f64, 1.5)) == cosh64(1.5));
92}
93
94test cosh32 {
95 const epsilon = 0.000001;
96
97 try expect(math.approxEqAbs(f32, cosh32(0.0), 1.0, epsilon));
98 try expect(math.approxEqAbs(f32, cosh32(0.2), 1.020067, epsilon));
99 try expect(math.approxEqAbs(f32, cosh32(0.8923), 1.425225, epsilon));
100 try expect(math.approxEqAbs(f32, cosh32(1.5), 2.352410, epsilon));
101 try expect(math.approxEqAbs(f32, cosh32(-0.0), 1.0, epsilon));
102 try expect(math.approxEqAbs(f32, cosh32(-0.2), 1.020067, epsilon));
103 try expect(math.approxEqAbs(f32, cosh32(-0.8923), 1.425225, epsilon));
104 try expect(math.approxEqAbs(f32, cosh32(-1.5), 2.352410, epsilon));
105}
106
107test cosh64 {
108 const epsilon = 0.000001;
109
110 try expect(math.approxEqAbs(f64, cosh64(0.0), 1.0, epsilon));
111 try expect(math.approxEqAbs(f64, cosh64(0.2), 1.020067, epsilon));
112 try expect(math.approxEqAbs(f64, cosh64(0.8923), 1.425225, epsilon));
113 try expect(math.approxEqAbs(f64, cosh64(1.5), 2.352410, epsilon));
114 try expect(math.approxEqAbs(f64, cosh64(-0.0), 1.0, epsilon));
115 try expect(math.approxEqAbs(f64, cosh64(-0.2), 1.020067, epsilon));
116 try expect(math.approxEqAbs(f64, cosh64(-0.8923), 1.425225, epsilon));
117 try expect(math.approxEqAbs(f64, cosh64(-1.5), 2.352410, epsilon));
118}
119
120test "cosh32.special" {
121 try expect(cosh32(0.0) == 1.0);
122 try expect(cosh32(-0.0) == 1.0);
123 try expect(math.isPositiveInf(cosh32(math.inf(f32))));
124 try expect(math.isPositiveInf(cosh32(-math.inf(f32))));
125 try expect(math.isNan(cosh32(math.nan(f32))));
126}
127
128test "cosh64.special" {
129 try expect(cosh64(0.0) == 1.0);
130 try expect(cosh64(-0.0) == 1.0);
131 try expect(math.isPositiveInf(cosh64(math.inf(f64))));
132 try expect(math.isPositiveInf(cosh64(-math.inf(f64))));
133 try expect(math.isNan(cosh64(math.nan(f64))));
134}