1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/// Hyperbolic Trig functions for FPDecimal
use crate::fp_decimal::{FPDecimal, U256};

impl FPDecimal {
    pub(crate) fn _sinh(x: FPDecimal) -> FPDecimal {
        let neg_x: FPDecimal = FPDecimal {
            num: x.num,
            sign: 1 - x.sign,
        };
        let denominator = FPDecimal {
            num: FPDecimal::ONE.num * U256([2, 0, 0, 0]),
            sign: 1,
        };
        let numerator: FPDecimal = FPDecimal::_sub(FPDecimal::exp(x), FPDecimal::exp(neg_x));
        FPDecimal::_div(numerator, denominator)
    }

    pub fn sinh(&self) -> FPDecimal {
        FPDecimal::_sinh(*self)
    }

    pub(crate) fn _cosh(x: FPDecimal) -> FPDecimal {
        let neg_x: FPDecimal = FPDecimal {
            num: x.num,
            sign: 1 - x.sign,
        };
        let denominator = FPDecimal {
            num: FPDecimal::ONE.num * U256([2, 0, 0, 0]),
            sign: 1,
        };
        let numerator: FPDecimal = FPDecimal::_add(FPDecimal::exp(x), FPDecimal::exp(neg_x));
        FPDecimal::_div(numerator, denominator)
    }

    pub fn cosh(&self) -> FPDecimal {
        FPDecimal::_cosh(*self)
    }

    pub(crate) fn _tanh(x: FPDecimal) -> FPDecimal {
        FPDecimal::_div(FPDecimal::_sinh(x), FPDecimal::_cosh(x))
    }

    pub fn tanh(&self) -> FPDecimal {
        FPDecimal::_tanh(*self)
    }
}

#[cfg(test)]
mod tests {

    use crate::FPDecimal;
    use std::str::FromStr;

    #[test]
    fn test_sinh() {
        assert_eq!(FPDecimal::_sinh(FPDecimal::ONE), FPDecimal::from_str("1.175201193643801457").unwrap());
    }

    #[test]
    fn test_cosh() {
        assert_eq!(FPDecimal::_cosh(FPDecimal::ONE), FPDecimal::from_str("1.543080634815243778").unwrap());
    }

    #[test]
    fn test_tanh() {
        assert_eq!(FPDecimal::_tanh(FPDecimal::ONE), FPDecimal::from_str("0.761594155955764888").unwrap());
    }
}