e
E
This commit is contained in:
@@ -2,6 +2,8 @@ use alloc::format;
|
||||
use alloc::string::String;
|
||||
use crate::println;
|
||||
|
||||
const PI: f64 = 3.14159265358979323846264338327950288419716939937510;
|
||||
|
||||
pub fn run_func(func: String, x: f64) -> Result<f64, String> {
|
||||
match func.as_str() {
|
||||
"sqrt" => sqrt(x),
|
||||
@@ -36,14 +38,51 @@ fn factorial(x: f64) -> Result<f64, String> {
|
||||
Ok((1..=x).fold(1, |a, b| a * b) as f64)
|
||||
}
|
||||
|
||||
fn sin(x: f64) -> Result<f64, String> {
|
||||
Ok(libm::sin(x))
|
||||
fn cos(mut x: f64) -> Result<f64, String> {
|
||||
while x > PI {
|
||||
x -= PI;
|
||||
}
|
||||
|
||||
let res = 1.0 - trig_term(x, 2) + trig_term(x, 4) - trig_term(x, 6) + trig_term(x, 8) - trig_term(x, 10);
|
||||
if res >= -1.0 && res <= 1.0 {
|
||||
Ok(res)
|
||||
} else {
|
||||
panic!("something is very wrong with the cos function : {}", res);
|
||||
}
|
||||
}
|
||||
|
||||
fn cos(x: f64) -> Result<f64, String> {
|
||||
Ok(libm::cos(x))
|
||||
fn sin(mut x: f64) -> Result<f64, String> {
|
||||
while x > PI {
|
||||
x -= PI;
|
||||
}
|
||||
|
||||
|
||||
let res = x - trig_term(x, 3) + trig_term(x, 5) - trig_term(x, 7) + trig_term(x, 9) - trig_term(x, 11);
|
||||
if res >= -1.0 && res <= 1.0 {
|
||||
Ok(res)
|
||||
} else {
|
||||
panic!("something is very wrong with the sin function: {}", res);
|
||||
}
|
||||
}
|
||||
|
||||
fn tan(x: f64) -> Result<f64, String> {
|
||||
Ok(libm::tan(x))
|
||||
}
|
||||
|
||||
pub fn exp(x: f64, y: f64) -> f64 {
|
||||
let mut res = 1.0;
|
||||
for _ in 0..(y as usize) {
|
||||
res *= x
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
fn trig_term(x: f64, y: usize) -> f64 {
|
||||
let mut ex = 1.0;
|
||||
for _ in 0..y {
|
||||
ex *= x;
|
||||
}
|
||||
let fact = (1..=y).fold(1, |a, b| a*b);
|
||||
|
||||
ex as f64 / fact as f64
|
||||
}
|
||||
Reference in New Issue
Block a user