added fix helper

This commit is contained in:
Valerie Wolfe 2023-06-19 11:02:26 -04:00
parent f149e57f07
commit d7b4cc29d4
2 changed files with 17 additions and 0 deletions

View file

@ -7,6 +7,22 @@ use evalexpr::{
use crate::util; use crate::util;
// Mathematics // Mathematics
pub fn fix(arg: &Value) -> Result<Value, EvalexprError> {
let args = arg.as_tuple()?;
let count = args.len();
if count != 2 {
return Err(EvalexprError::WrongFunctionArgumentAmount { expected: 2, actual: count });
}
let float = args[0].as_float()?;
let figures = args[1].as_int()?;
let operand: f64 = i64::pow(10, figures as u32) as f64;
let output = f64::round(float * operand) / operand;
return Ok(output.into());
}
pub fn logarithm(arg: &Value) -> Result<Value, EvalexprError> { pub fn logarithm(arg: &Value) -> Result<Value, EvalexprError> {
let arguments: Vec<Value>; let arguments: Vec<Value>;
let count: usize; let count: usize;

View file

@ -36,6 +36,7 @@ fn main() {
"√2" => global::ROOT_TWO, "√2" => global::ROOT_TWO,
// math functions // math functions
"fix" => Function::new(|arg| helper::fix(arg)),
"log" => Function::new(|arg| helper::logarithm(arg)), "log" => Function::new(|arg| helper::logarithm(arg)),
"sqrt" => Function::new(|arg| helper::square_root(arg)), "sqrt" => Function::new(|arg| helper::square_root(arg)),