twofloat/functions/
hyperbolic.rs

1use crate::TwoFloat;
2
3impl TwoFloat {
4    /// Hyperbolic cosine function.
5    ///
6    /// This is a convenience method that computes the value by calling the
7    /// exponential function.
8    ///
9    /// # Examples
10    ///
11    /// ```
12    /// # use twofloat::TwoFloat;
13    /// let a = TwoFloat::from(2.0);
14    /// let b = a.cosh();
15    /// let c = 2.0f64.cosh();
16    ///
17    /// assert!((b - c).abs() < 1e-10);
18    /// ```
19    pub fn cosh(self) -> Self {
20        self.exp() / 2.0 + (-self).exp() / 2.0
21    }
22
23    /// Hyperbolic sine function.
24    ///
25    /// This is a convenience method that computes the value by calling the
26    /// exponential function.
27    ///
28    /// # Examples
29    ///
30    /// ```
31    /// # use twofloat::TwoFloat;
32    /// let a = TwoFloat::from(2.0);
33    /// let b = a.sinh();
34    /// let c = 2.0f64.sinh();
35    ///
36    /// assert!((b - c).abs() < 1e-10);
37    /// ```
38    pub fn sinh(self) -> Self {
39        self.exp() / 2.0 - (-self).exp() / 2.0
40    }
41
42    /// Hyperbolic tangent function.
43    ///
44    /// This is a convenience method that computes the value by calling the
45    /// exponential function.
46    ///
47    /// # Examples
48    ///
49    /// ```
50    /// # use twofloat::TwoFloat;
51    /// let a = TwoFloat::from(2.0);
52    /// let b = a.tanh();
53    /// let c = 2.0f64.tanh();
54    ///
55    /// assert!((b - c).abs() < 1e-10);
56    /// ```
57    pub fn tanh(self) -> Self {
58        let e_plus = self.exp();
59        let e_minus = (-self).exp();
60        (e_plus - e_minus) / (e_plus + e_minus)
61    }
62
63    /// Inverse hyperbolic cosine function.
64    ///
65    /// This is a convenience method that computes the value by calling the
66    /// `sqrt` and `ln` functions.
67    ///
68    /// # Examples
69    ///
70    /// ```
71    /// # use twofloat::TwoFloat;
72    /// let a = TwoFloat::from(2.0);
73    /// let b = a.acosh();
74    /// let c = 2.0f64.acosh();
75    ///
76    /// assert!((b - c).abs() < 1e-10);
77    /// ```
78    pub fn acosh(self) -> Self {
79        (self + (self * self - 1.0).sqrt()).ln()
80    }
81
82    /// Inverse hyperbolic sine function.
83    ///
84    /// This is a convenience method that computes the value by calling the
85    /// `sqrt` and `ln` functions.
86    ///
87    /// # Examples
88    ///
89    /// ```
90    /// # use twofloat::TwoFloat;
91    /// let a = TwoFloat::from(2.0);
92    /// let b = a.asinh();
93    /// let c = 2.0f64.asinh();
94    ///
95    /// assert!((b - c).abs() < 1e-10);
96    /// ```
97    pub fn asinh(self) -> Self {
98        (self + (self * self + 1.0).sqrt()).ln()
99    }
100
101    /// Inverse hyperbolic tangent function.
102    ///
103    /// This is a convenience method that computes the value by calling the
104    /// `ln` function.
105    ///
106    /// # Examples
107    ///
108    /// ```
109    /// # use twofloat::TwoFloat;
110    /// let a = TwoFloat::from(0.5);
111    /// let b = a.atanh();
112    /// let c = 0.5f64.atanh();
113    ///
114    /// assert!((b - c).abs() < 1e-10);
115    /// ```
116    pub fn atanh(self) -> Self {
117        ((1.0 + self) / (1.0 - self)).ln() / 2.0
118    }
119}