Add trigonometric functions (#823)

This commit is contained in:
end-4 2023-08-16 18:40:24 +07:00 committed by GitHub
parent efce800aff
commit 9d0b667dea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 0 deletions

View File

@ -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)

View File

@ -320,6 +320,48 @@ fn call_expr_function(name: &str, args: Vec<DynVal>) -> Result<DynVal, EvalError
}
_ => 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()?;

View File

@ -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)