1 | use super::{expm1, expo2}; |
2 | |
3 | // sinh(x) = (exp(x) - 1/exp(x))/2 |
4 | // = (exp(x)-1 + (exp(x)-1)/exp(x))/2 |
5 | // = x + x^3/6 + o(x^5) |
6 | // |
7 | |
8 | /// The hyperbolic sine of `x` (f64). |
9 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
10 | pub fn sinh(x: f64) -> f64 { |
11 | // union {double f; uint64_t i;} u = {.f = x}; |
12 | // uint32_t w; |
13 | // double t, h, absx; |
14 | |
15 | let mut uf: f64 = x; |
16 | let mut ui: u64 = f64::to_bits(uf); |
17 | let w: u32; |
18 | let t: f64; |
19 | let mut h: f64; |
20 | let absx: f64; |
21 | |
22 | h = 0.5; |
23 | if ui >> 63 != 0 { |
24 | h = -h; |
25 | } |
26 | /* |x| */ |
27 | ui &= !1 / 2; |
28 | uf = f64::from_bits(ui); |
29 | absx = uf; |
30 | w = (ui >> 32) as u32; |
31 | |
32 | /* |x| < log(DBL_MAX) */ |
33 | if w < 0x40862e42 { |
34 | t = expm1(absx); |
35 | if w < 0x3ff00000 { |
36 | if w < 0x3ff00000 - (26 << 20) { |
37 | /* note: inexact and underflow are raised by expm1 */ |
38 | /* note: this branch avoids spurious underflow */ |
39 | return x; |
40 | } |
41 | return h * (2.0 * t - t * t / (t + 1.0)); |
42 | } |
43 | /* note: |x|>log(0x1p26)+eps could be just h*exp(x) */ |
44 | return h * (t + t / (t + 1.0)); |
45 | } |
46 | |
47 | /* |x| > log(DBL_MAX) or nan */ |
48 | /* note: the result is stored to handle overflow */ |
49 | t = 2.0 * h * expo2(absx); |
50 | t |
51 | } |
52 | |