Skip to main content

sin_cos_ln_sqrt/
lib.rs

1use std::f64::consts::E;
2
3pub fn sin(x:f64)->f64{
4    let mut result:f64 = 0.0;
5    for n in 0..{
6        let addition = x.powi(n*2+1) / factorio(n as i128*2+1);
7        result += if n % 2 == 0 {addition} else {-addition};
8        if addition < 1e-10{
9            break;
10        }
11    }
12    result
13}
14pub fn cos(x:f64)->f64{
15    let mut result:f64 = 0.0;
16    for n in 0..{
17        let addition = x.powi(n*2) / factorio(n as i128*2);
18        result += if n % 2 == 0 {addition} else {-addition};
19        if addition < 1e-10{
20            break;
21        }
22    }
23    result
24}
25pub fn factorio(x:i128)->f64{
26    let mut result:f64 = 1.0;
27    for i in 1..=x{
28        result *= i as f64;
29    }
30    result
31}
32/// ta funkcja jest dokładniejsza od funkcji ln() ale działa tylko w x zakresie 0 > x >= 2 
33pub fn ln2(x:f64)->f64{
34    if x <= 0.0 || x > 2.0{
35        return 0.0;
36    }
37    let mut result:f64 = 0.0;
38    for n in 1..{
39        let b:f64 = (-1i32).pow(n+1) as f64;
40        let a:f64 = b/n as f64;
41        let addition = a*(x-1.0).powi(n as i32);
42        result += addition;
43        if addition.abs() < 1e-10{
44            break;
45        }
46    }
47    result
48}
49pub fn ln(x:f64)->f64{
50    let y:f64=0.000000000001;
51    return (x.powf(y)-1.0)/y;
52}
53/// ta funkcja jest dokładniejsza od funkcji sqrt() ale działa tylko w x zakresie 0 > x >= 2 
54pub fn sqrt2(x:f64)->f64{
55   return E.powf(ln2(x)/2.0); 
56}
57pub fn sqrt(x:f64)->f64{
58   return E.powf(ln(x)/2.0); 
59}
60