/rust/registry/src/index.crates.io-1949cf8c6b5b557f/libm-0.2.11/src/math/sinh.rs
Line | Count | Source |
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 | 0 | 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 | 0 | let mut uf: f64 = x; |
16 | 0 | 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 | 0 | h = 0.5; |
23 | 0 | if ui >> 63 != 0 { |
24 | 0 | h = -h; |
25 | 0 | } |
26 | | /* |x| */ |
27 | 0 | ui &= !1 / 2; |
28 | 0 | uf = f64::from_bits(ui); |
29 | 0 | absx = uf; |
30 | 0 | w = (ui >> 32) as u32; |
31 | | |
32 | | /* |x| < log(DBL_MAX) */ |
33 | 0 | if w < 0x40862e42 { |
34 | 0 | t = expm1(absx); |
35 | 0 | if w < 0x3ff00000 { |
36 | 0 | if w < 0x3ff00000 - (26 << 20) { |
37 | | /* note: inexact and underflow are raised by expm1 */ |
38 | | /* note: this branch avoids spurious underflow */ |
39 | 0 | return x; |
40 | 0 | } |
41 | 0 | return h * (2.0 * t - t * t / (t + 1.0)); |
42 | 0 | } |
43 | | /* note: |x|>log(0x1p26)+eps could be just h*exp(x) */ |
44 | 0 | return h * (t + t / (t + 1.0)); |
45 | 0 | } |
46 | | |
47 | | /* |x| > log(DBL_MAX) or nan */ |
48 | | /* note: the result is stored to handle overflow */ |
49 | 0 | t = 2.0 * h * expo2(absx); |
50 | 0 | t |
51 | 0 | } |