From 7ee3ce1377ee1ade1772379f898cbc546f94602e Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Sun, 31 Mar 2019 09:38:41 +0530 Subject: add radian mode opt --- src/lex/mod.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/lex/mod.rs b/src/lex/mod.rs index 51f124c..bd69885 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs @@ -4,6 +4,8 @@ use std::collections::HashMap; +use crate::CONFIGURATION; + use crate::error::{ CalcError, Math @@ -77,12 +79,12 @@ pub enum Token { fn get_functions() -> HashMap<&'static str, Token> { return [ - ("sin", Function::token_from_fn("sin".into(), |x| x.to_radians().sin())), - ("cos", Function::token_from_fn("cos".into(), |x| x.to_radians().cos())), - ("tan", Function::token_from_fn("tan".into(), |x| x.to_radians().tan())), - ("csc", Function::token_from_fn("csc".into(), |x| x.to_radians().sin().recip())), - ("sec", Function::token_from_fn("sec".into(), |x| x.to_radians().cos().recip())), - ("cot", Function::token_from_fn("cot".into(), |x| x.to_radians().tan().recip())), + ("sin", Function::token_from_fn("sin".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).sin())), + ("cos", Function::token_from_fn("cos".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).cos())), + ("tan", Function::token_from_fn("tan".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).tan())), + ("csc", Function::token_from_fn("csc".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).sin().recip())), + ("sec", Function::token_from_fn("sec".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).cos().recip())), + ("cot", Function::token_from_fn("cot".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).tan().recip())), ("sinh", Function::token_from_fn("sinh".into(), |x| x.sinh())), ("cosh", Function::token_from_fn("cosh".into(), |x| x.cosh())), ("tanh", Function::token_from_fn("tanh".into(), |x| x.tanh())), @@ -220,3 +222,11 @@ fn drain_num_stack(num_vec: &mut String, result: &mut Vec) { num_vec.clear(); } } + +fn is_radian_mode(x: f64, is_radian: bool) -> f64 { + if is_radian { + return x + } else { + x.to_radians() + } +} -- cgit v1.2.3