aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2019-03-31 05:08:41 +0100
committerNerdyPepper <[email protected]>2019-03-31 05:08:41 +0100
commit7ee3ce1377ee1ade1772379f898cbc546f94602e (patch)
treeb749a94b175c28d1867e9aa09c85e00d1277562d
parenta31cd9acc8d84c7628099ebb317043cfbe4dba8c (diff)
add radian mode opt
-rw-r--r--src/lex/mod.rs22
1 files 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 @@
4 4
5use std::collections::HashMap; 5use std::collections::HashMap;
6 6
7use crate::CONFIGURATION;
8
7use crate::error::{ 9use crate::error::{
8 CalcError, 10 CalcError,
9 Math 11 Math
@@ -77,12 +79,12 @@ pub enum Token {
77 79
78fn get_functions() -> HashMap<&'static str, Token> { 80fn get_functions() -> HashMap<&'static str, Token> {
79 return [ 81 return [
80 ("sin", Function::token_from_fn("sin".into(), |x| x.to_radians().sin())), 82 ("sin", Function::token_from_fn("sin".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).sin())),
81 ("cos", Function::token_from_fn("cos".into(), |x| x.to_radians().cos())), 83 ("cos", Function::token_from_fn("cos".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).cos())),
82 ("tan", Function::token_from_fn("tan".into(), |x| x.to_radians().tan())), 84 ("tan", Function::token_from_fn("tan".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).tan())),
83 ("csc", Function::token_from_fn("csc".into(), |x| x.to_radians().sin().recip())), 85 ("csc", Function::token_from_fn("csc".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).sin().recip())),
84 ("sec", Function::token_from_fn("sec".into(), |x| x.to_radians().cos().recip())), 86 ("sec", Function::token_from_fn("sec".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).cos().recip())),
85 ("cot", Function::token_from_fn("cot".into(), |x| x.to_radians().tan().recip())), 87 ("cot", Function::token_from_fn("cot".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).tan().recip())),
86 ("sinh", Function::token_from_fn("sinh".into(), |x| x.sinh())), 88 ("sinh", Function::token_from_fn("sinh".into(), |x| x.sinh())),
87 ("cosh", Function::token_from_fn("cosh".into(), |x| x.cosh())), 89 ("cosh", Function::token_from_fn("cosh".into(), |x| x.cosh())),
88 ("tanh", Function::token_from_fn("tanh".into(), |x| x.tanh())), 90 ("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<Token>) {
220 num_vec.clear(); 222 num_vec.clear();
221 } 223 }
222} 224}
225
226fn is_radian_mode(x: f64, is_radian: bool) -> f64 {
227 if is_radian {
228 return x
229 } else {
230 x.to_radians()
231 }
232}