injective_math/fp_decimal/
hyper.rs

1/// Hyperbolic Trig functions for FPDecimal
2use crate::fp_decimal::{FPDecimal, U256};
3
4impl FPDecimal {
5    pub(crate) fn _sinh(x: FPDecimal) -> FPDecimal {
6        let neg_x: FPDecimal = FPDecimal {
7            num: x.num,
8            sign: 1 - x.sign,
9        };
10        let denominator = FPDecimal {
11            num: FPDecimal::ONE.num * U256([2, 0, 0, 0]),
12            sign: 1,
13        };
14        let numerator: FPDecimal = FPDecimal::_sub(FPDecimal::exp(x), FPDecimal::exp(neg_x));
15        FPDecimal::_div(numerator, denominator)
16    }
17
18    pub fn sinh(&self) -> FPDecimal {
19        FPDecimal::_sinh(*self)
20    }
21
22    pub(crate) fn _cosh(x: FPDecimal) -> FPDecimal {
23        let neg_x: FPDecimal = FPDecimal {
24            num: x.num,
25            sign: 1 - x.sign,
26        };
27        let denominator = FPDecimal {
28            num: FPDecimal::ONE.num * U256([2, 0, 0, 0]),
29            sign: 1,
30        };
31        let numerator: FPDecimal = FPDecimal::_add(FPDecimal::exp(x), FPDecimal::exp(neg_x));
32        FPDecimal::_div(numerator, denominator)
33    }
34
35    pub fn cosh(&self) -> FPDecimal {
36        FPDecimal::_cosh(*self)
37    }
38
39    pub(crate) fn _tanh(x: FPDecimal) -> FPDecimal {
40        FPDecimal::_div(FPDecimal::_sinh(x), FPDecimal::_cosh(x))
41    }
42
43    pub fn tanh(&self) -> FPDecimal {
44        FPDecimal::_tanh(*self)
45    }
46}
47
48#[cfg(test)]
49mod tests {
50
51    use crate::FPDecimal;
52    use std::str::FromStr;
53
54    #[test]
55    fn test_sinh() {
56        assert_eq!(FPDecimal::_sinh(FPDecimal::ONE), FPDecimal::from_str("1.175201193643801457").unwrap());
57    }
58
59    #[test]
60    fn test_cosh() {
61        assert_eq!(FPDecimal::_cosh(FPDecimal::ONE), FPDecimal::from_str("1.543080634815243778").unwrap());
62    }
63
64    #[test]
65    fn test_tanh() {
66        assert_eq!(FPDecimal::_tanh(FPDecimal::ONE), FPDecimal::from_str("0.761594155955764888").unwrap());
67    }
68}