#sin #sqrt #cos #ln #factorio #containing

sin_cos_ln_sqrt

ITN crate containing math functions

2 releases

0.1.1 Jan 6, 2023
0.1.0 Jan 6, 2023

#873 in Math

23 downloads per month

MIT license

4KB
56 lines

use std::f64::consts::E;

pub fn sin(x:f64)->f64{
    let mut result:f64 = 0.0;
    for n in 0..{
        let addition = x.powi(n*2+1) / factorio(n as i128*2+1);
        result += if n % 2 == 0 {addition} else {-addition};
        if addition < 1e-10{
            break;
        }
    }
    result
}
pub fn cos(x:f64)->f64{
    let mut result:f64 = 0.0;
    for n in 0..{
        let addition = x.powi(n*2) / factorio(n as i128*2);
        result += if n % 2 == 0 {addition} else {-addition};
        if addition < 1e-10{
            break;
        }
    }
    result
}
pub fn factorio(x:i128)->f64{
    let mut result:f64 = 1.0;
    for i in 1..=x{
        result *= i as f64;
    }
    result
}
/// ta funkcja jest dokładniejsza od funkcji ln() ale działa tylko w x zakresie 0 > x >= 2 
pub fn ln2(x:f64)->f64{
    if x <= 0.0 || x > 2.0{
        return 0.0;
    }
    let mut result:f64 = 0.0;
    for n in 1..{
        let b:f64 = (-1i32).pow(n+1) as f64;
        let a:f64 = b/n as f64;
        let addition = a*(x-1.0).powi(n as i32);
        result += addition;
        if addition.abs() < 1e-10{
            break;
        }
    }
    result
}
pub fn ln(x:f64)->f64{
    let y:f64=0.000000000001;
    return (x.powf(y)-1.0)/y;
}
/// ta funkcja jest dokładniejsza od funkcji sqrt() ale działa tylko w x zakresie 0 > x >= 2 
pub fn sqrt2(x:f64)->f64{
   return E.powf(ln2(x)/2.0); 
}
pub fn sqrt(x:f64)->f64{
   return E.powf(ln(x)/2.0); 
}

No runtime deps