From 9d0b667deadf9e64d3b692274275d57a61c107c5 Mon Sep 17 00:00:00 2001 From: end-4 <97237370+end-4@users.noreply.github.com> Date: Wed, 16 Aug 2023 18:40:24 +0700 Subject: [PATCH] Add trigonometric functions (#823) --- CHANGELOG.md | 1 + crates/simplexpr/src/eval.rs | 42 +++++++++++++++++++++++++++++++++ docs/src/expression_language.md | 3 +++ 3 files changed, 46 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f3bbcb..7744ad6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ All notable changes to eww will be listed here, starting at changes since versio - Add `jq` function, offering jq-style json processing - Add `justify` property to the label widget, allowing text justification (By: n3oney) - Add `EWW_TIME` magic variable (By: Erenoit) +- Add trigonometric functions (`sin`, `cos`, `tan`, `cot`) and degree/radian conversions (`degtorad`, `radtodeg`) (By: end-4) ## [0.4.0] (04.09.2022) diff --git a/crates/simplexpr/src/eval.rs b/crates/simplexpr/src/eval.rs index c0b029d..c890ce8 100644 --- a/crates/simplexpr/src/eval.rs +++ b/crates/simplexpr/src/eval.rs @@ -320,6 +320,48 @@ fn call_expr_function(name: &str, args: Vec) -> Result Err(EvalError::WrongArgCount(name.to_string())), }, + "sin" => match args.as_slice() { + [num] => { + let num = num.as_f64()?; + Ok(DynVal::from(num.sin())) + } + _ => Err(EvalError::WrongArgCount(name.to_string())), + }, + "cos" => match args.as_slice() { + [num] => { + let num = num.as_f64()?; + Ok(DynVal::from(num.cos())) + } + _ => Err(EvalError::WrongArgCount(name.to_string())), + }, + "tan" => match args.as_slice() { + [num] => { + let num = num.as_f64()?; + Ok(DynVal::from(num.tan())) + } + _ => Err(EvalError::WrongArgCount(name.to_string())), + }, + "cot" => match args.as_slice() { + [num] => { + let num = num.as_f64()?; + Ok(DynVal::from(1.0 / num.tan())) + } + _ => Err(EvalError::WrongArgCount(name.to_string())), + }, + "degtorad" => match args.as_slice() { + [num] => { + let num = num.as_f64()?; + Ok(DynVal::from(num.to_radians())) + } + _ => Err(EvalError::WrongArgCount(name.to_string())), + }, + "radtodeg" => match args.as_slice() { + [num] => { + let num = num.as_f64()?; + Ok(DynVal::from(num.to_degrees())) + } + _ => Err(EvalError::WrongArgCount(name.to_string())), + }, "matches" => match args.as_slice() { [string, pattern] => { let string = string.as_string()?; diff --git a/docs/src/expression_language.md b/docs/src/expression_language.md index 57e15ea..7d54811 100644 --- a/docs/src/expression_language.md +++ b/docs/src/expression_language.md @@ -39,6 +39,9 @@ Supported currently are the following features: - for this, the object/array value needs to refer to a variable that contains a valid json string. - some function calls: - `round(number, decimal_digits)`: Round a number to the given amount of decimals + - `sin(number)`, `cos(number)`, `tan(number)`, `cot(number)`: Calculate the trigonometric value of a given number in **radians** + - `degtorad(number)`: Converts a number from degrees to radians + - `radtodeg(number)`: Converts a number from radians to degrees - `replace(string, regex, replacement)`: Replace matches of a given regex in a string - `search(string, regex)`: Search for a given regex in a string (returns array) - `matches(string, regex)`: check if a given string matches a given regex (returns bool)