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


//! Contains mathematical functions.

pub mod vector;

/// Calculates the factorial of ```n```.
pub fn factorial(n: u64) -> u64 {
    (1..n).product()
}

/// Calculates the sin of ```x```.
pub fn sin(x: f64) -> f64 {
    (0..11_u64).map(|n| {
            (2*n + 1, if n % 2 == 0 { 1 } else { -1 })
        })
        .map(|(n, i)| {
            i as f64 * (x.powi(n as i32) / factorial(n) as f64)
        })
        .sum()
}

/// Calculates the cos of ```x```.
/// # Panics
/// Always panics.
pub fn cos(x: f64) -> f64 {
    todo!("Too intense to compute.")
}

#[cfg(test)]
mod test {
    use super::sin;

    #[test]
    fn test_sin() {
        let num: f64 = 12.0;

        assert_eq!(num.sin(), sin(num));
    }
}