added more globals and functions
This commit is contained in:
parent
b086186949
commit
b73ef17d8d
3 changed files with 78 additions and 10 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
|
||||||
pub const EULER: f64 = 2.718;
|
pub const EULER: f64 = 2.718281828459045;
|
||||||
pub const PI: f64 = 3.14;
|
pub const GOLDEN_RATIO: f64 = 1.618033988749895;
|
||||||
|
pub const PI: f64 = 3.141592653589793;
|
||||||
|
pub const ROOT_TWO: f64 = 1.414213562373095;
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,58 @@ use evalexpr::{
|
||||||
|
|
||||||
use crate::util;
|
use crate::util;
|
||||||
|
|
||||||
|
// Mathematics
|
||||||
|
pub fn logarithm(arg: &Value) -> Result<Value, EvalexprError> {
|
||||||
|
let arguments: Vec<Value>;
|
||||||
|
let count: usize;
|
||||||
|
if arg.is_tuple() {
|
||||||
|
arguments = arg.as_tuple()?;
|
||||||
|
count = arguments.len();
|
||||||
|
} else if arg.is_float() {
|
||||||
|
arguments = vec!(arg.as_float()?.into());
|
||||||
|
count = 1;
|
||||||
|
} else if arg.is_int() {
|
||||||
|
arguments = vec!((arg.as_int()? as f64).into());
|
||||||
|
count = 1;
|
||||||
|
} else {
|
||||||
|
return Err(EvalexprError::CustomMessage("Expected numbers".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let output: Value;
|
||||||
|
match count {
|
||||||
|
1 => {
|
||||||
|
let argument = &arguments[0];
|
||||||
|
if !argument.is_number() {
|
||||||
|
return Err(EvalexprError::CustomMessage("Expected number".to_string()));
|
||||||
|
}
|
||||||
|
let number = if argument.is_float() { argument.as_float()? } else { argument.as_int()? as f64 };
|
||||||
|
output = number.ln().into();
|
||||||
|
},
|
||||||
|
2 => {
|
||||||
|
let arg_value = &arguments[0];
|
||||||
|
let arg_base = &arguments[1];
|
||||||
|
if !(arg_value.is_number() && arg_base.is_number()) {
|
||||||
|
return Err(EvalexprError::CustomMessage("Expected two numbers".to_string()));
|
||||||
|
}
|
||||||
|
let value: f64 = if arg_value.is_float() { arg_value.as_float()? } else { arg_value.as_int()? as f64 };
|
||||||
|
let base: f64 = if arg_base.is_float() { arg_base.as_float()? } else { arg_base.as_int()? as f64 };
|
||||||
|
output = value.log(base).into();
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
return Err(EvalexprError::WrongFunctionArgumentAmount { expected: 2, actual: count });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn square_root(arg: &Value) -> Result<Value, EvalexprError> {
|
||||||
|
if !arg.is_number() {
|
||||||
|
return Err(EvalexprError::CustomMessage("Expected a number.".to_string()));
|
||||||
|
}
|
||||||
|
let value: f64 = if arg.is_float() { arg.as_float()? } else { arg.as_int()? as f64 };
|
||||||
|
return Ok(value.sqrt().into());
|
||||||
|
}
|
||||||
|
|
||||||
// Data Science
|
// Data Science
|
||||||
pub fn average(arg: &Value) -> Result<Value, EvalexprError> {
|
pub fn average(arg: &Value) -> Result<Value, EvalexprError> {
|
||||||
let arguments = arg.as_tuple()?;
|
let arguments = arg.as_tuple()?;
|
||||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -14,7 +14,9 @@ use evalexpr::{
|
||||||
eval_with_context_mut,
|
eval_with_context_mut,
|
||||||
|
|
||||||
HashMapContext,
|
HashMapContext,
|
||||||
Value
|
Value,
|
||||||
|
|
||||||
|
Context
|
||||||
};
|
};
|
||||||
use termion::{
|
use termion::{
|
||||||
color,
|
color,
|
||||||
|
@ -25,13 +27,19 @@ mod global;
|
||||||
mod helper;
|
mod helper;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
pub const VERSION: &str = "0.1.5";
|
pub const VERSION: &str = "0.2.0";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut context = context_map! {
|
let mut context = context_map! {
|
||||||
// globals
|
// globals
|
||||||
"e" => global::EULER,
|
"e" => global::EULER,
|
||||||
|
"phi" => global::GOLDEN_RATIO,
|
||||||
"pi" => global::PI,
|
"pi" => global::PI,
|
||||||
|
"√2" => global::ROOT_TWO,
|
||||||
|
|
||||||
|
// math functions
|
||||||
|
"log" => Function::new(|arg| helper::logarithm(arg)),
|
||||||
|
"sqrt" => Function::new(|arg| helper::square_root(arg)),
|
||||||
|
|
||||||
// data science functions
|
// data science functions
|
||||||
"avg" => Function::new(|arg| helper::average(arg)),
|
"avg" => Function::new(|arg| helper::average(arg)),
|
||||||
|
@ -39,7 +47,12 @@ fn main() {
|
||||||
// radix functions
|
// radix functions
|
||||||
"bin" => Function::new(|arg| helper::binary(arg)),
|
"bin" => Function::new(|arg| helper::binary(arg)),
|
||||||
"hex" => Function::new(|arg| helper::hexadecimal(arg)),
|
"hex" => Function::new(|arg| helper::hexadecimal(arg)),
|
||||||
"oct" => Function::new(|arg| helper::octal(arg))
|
"oct" => Function::new(|arg| helper::octal(arg)),
|
||||||
|
|
||||||
|
// character aliases
|
||||||
|
"ϕ" => global::GOLDEN_RATIO,
|
||||||
|
"π" => global::PI,
|
||||||
|
"√" => Function::new(|arg| helper::square_root(arg))
|
||||||
}.unwrap();
|
}.unwrap();
|
||||||
let expressions: Vec<String> = env::args().skip(1).collect();
|
let expressions: Vec<String> = env::args().skip(1).collect();
|
||||||
if expressions.len() == 0 {
|
if expressions.len() == 0 {
|
||||||
|
|
Loading…
Reference in a new issue