Evaluation of sin, cos, tan
This commit is contained in:
parent
1e541b8313
commit
26992c9e32
@ -10,8 +10,8 @@ use crate::FunctionIdentifier::*;
|
|||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Answer {
|
pub struct Answer {
|
||||||
value: d128,
|
pub value: d128,
|
||||||
unit: Unit,
|
pub unit: Unit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Answer {
|
impl Answer {
|
||||||
@ -56,6 +56,44 @@ fn cbrt(input: d128) -> d128 {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sin(mut input: d128) -> d128 {
|
||||||
|
let pi = d128!(3.141592653589793238462643383279503);
|
||||||
|
let pi2 = d128!(6.283185307179586476925286766559006);
|
||||||
|
|
||||||
|
input %= pi2;
|
||||||
|
|
||||||
|
let negative_correction = if input.is_negative() {
|
||||||
|
input -= pi;
|
||||||
|
d128!(-1)
|
||||||
|
} else {
|
||||||
|
d128!(1)
|
||||||
|
};
|
||||||
|
|
||||||
|
let one = d128!(1);
|
||||||
|
let two = d128!(2);
|
||||||
|
let neg_one = -one;
|
||||||
|
|
||||||
|
let precision = 37;
|
||||||
|
let mut result = d128!(0);
|
||||||
|
for i_int in 0..precision {
|
||||||
|
let i = d128::from(i_int);
|
||||||
|
let calc_result = two*i+one;
|
||||||
|
result += neg_one.pow(i) * (input.pow(calc_result) / factorial(calc_result, one, two));
|
||||||
|
}
|
||||||
|
|
||||||
|
return negative_correction * result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cos(input: d128) -> d128 {
|
||||||
|
let half_pi = d128!(1.570796326794896619231321691639751);
|
||||||
|
return sin(half_pi - input);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tan(input: d128) -> d128 {
|
||||||
|
return sin(input) / cos(input);
|
||||||
|
}
|
||||||
|
|
||||||
fn evaluate_node(ast_node: &AstNode) -> Result<Answer, String> {
|
fn evaluate_node(ast_node: &AstNode) -> Result<Answer, String> {
|
||||||
let token = &ast_node.token;
|
let token = &ast_node.token;
|
||||||
let children = &ast_node.children;
|
let children = &ast_node.children;
|
||||||
@ -143,25 +181,17 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Answer, String> {
|
|||||||
if rounding_change == d128!(-0.5) { result += d128!(1); }
|
if rounding_change == d128!(-0.5) { result += d128!(1); }
|
||||||
return Ok(Answer::new(result, child_answer.unit))
|
return Ok(Answer::new(result, child_answer.unit))
|
||||||
},
|
},
|
||||||
|
Sin => {
|
||||||
// Sin,
|
let result = sin(child_answer.value);
|
||||||
// Cos,
|
return Ok(Answer::new(result, child_answer.unit))
|
||||||
// Tan,
|
},
|
||||||
// Asin,
|
Cos => {
|
||||||
// Acos,
|
let result = cos(child_answer.value);
|
||||||
// Atan,
|
return Ok(Answer::new(result, child_answer.unit))
|
||||||
// Sinh,
|
},
|
||||||
// Cosh,
|
Tan => {
|
||||||
// Tanh,
|
let result = tan(child_answer.value);
|
||||||
// Asinh,
|
return Ok(Answer::new(result, child_answer.unit))
|
||||||
// Acosh,
|
|
||||||
// Atanh,
|
|
||||||
// Sin
|
|
||||||
// Sinh
|
|
||||||
// Tan
|
|
||||||
// Tanh
|
|
||||||
_ => {
|
|
||||||
return Err(format!("EVAL UNSUPPORTED FUNC").to_string())
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/lexer.rs
11
src/lexer.rs
@ -5,7 +5,7 @@ use crate::Operator::{Caret, Divide, LeftParen, Minus, Modulo, Multiply, Plus, R
|
|||||||
use crate::UnaryOperator::{Percent, Factorial};
|
use crate::UnaryOperator::{Percent, Factorial};
|
||||||
use crate::TextOperator::{Of, To};
|
use crate::TextOperator::{Of, To};
|
||||||
use crate::Constant::{E, Pi};
|
use crate::Constant::{E, Pi};
|
||||||
use crate::FunctionIdentifier::{Acos, Acosh, Asin, Asinh, Atan, Atanh, Cbrt, Ceil, Cos, Cosh, Exp, Abs, Floor, Ln, Log, Round, Sin, Sinh, Sqrt, Tan, Tanh};
|
use crate::FunctionIdentifier::{Cbrt, Ceil, Cos, Exp, Abs, Floor, Ln, Log, Round, Sin, Sqrt, Tan};
|
||||||
use crate::units::Unit::*;
|
use crate::units::Unit::*;
|
||||||
|
|
||||||
pub fn lex(input: &str) -> Result<TokenVector, String> {
|
pub fn lex(input: &str) -> Result<TokenVector, String> {
|
||||||
@ -84,15 +84,6 @@ pub fn lex(input: &str) -> Result<TokenVector, String> {
|
|||||||
"sin" => tokens.push(Token::FunctionIdentifier(Sin)),
|
"sin" => tokens.push(Token::FunctionIdentifier(Sin)),
|
||||||
"cos" => tokens.push(Token::FunctionIdentifier(Cos)),
|
"cos" => tokens.push(Token::FunctionIdentifier(Cos)),
|
||||||
"tan" => tokens.push(Token::FunctionIdentifier(Tan)),
|
"tan" => tokens.push(Token::FunctionIdentifier(Tan)),
|
||||||
"asin" => tokens.push(Token::FunctionIdentifier(Asin)),
|
|
||||||
"acos" => tokens.push(Token::FunctionIdentifier(Acos)),
|
|
||||||
"atan" => tokens.push(Token::FunctionIdentifier(Atan)),
|
|
||||||
"sinh" => tokens.push(Token::FunctionIdentifier(Sinh)),
|
|
||||||
"cosh" => tokens.push(Token::FunctionIdentifier(Cosh)),
|
|
||||||
"tanh" => tokens.push(Token::FunctionIdentifier(Tanh)),
|
|
||||||
"asinh" => tokens.push(Token::FunctionIdentifier(Asinh)),
|
|
||||||
"acosh" => tokens.push(Token::FunctionIdentifier(Acosh)),
|
|
||||||
"atanh" => tokens.push(Token::FunctionIdentifier(Atanh)),
|
|
||||||
|
|
||||||
"ns" | "nanosecond" | "nanoseconds" => tokens.push(Token::Unit(Nanosecond)),
|
"ns" | "nanosecond" | "nanoseconds" => tokens.push(Token::Unit(Nanosecond)),
|
||||||
"μs" | "us" | "microsecond" | "microseconds" => tokens.push(Token::Unit(Microsecond)),
|
"μs" | "us" | "microsecond" | "microseconds" => tokens.push(Token::Unit(Microsecond)),
|
||||||
|
|||||||
@ -50,15 +50,6 @@ pub enum FunctionIdentifier {
|
|||||||
Sin,
|
Sin,
|
||||||
Cos,
|
Cos,
|
||||||
Tan,
|
Tan,
|
||||||
Asin,
|
|
||||||
Acos,
|
|
||||||
Atan,
|
|
||||||
Sinh,
|
|
||||||
Cosh,
|
|
||||||
Tanh,
|
|
||||||
Asinh,
|
|
||||||
Acosh,
|
|
||||||
Atanh,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mod units;
|
mod units;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user