Compare commits

...

3 commits

4 changed files with 19 additions and 28 deletions

View file

@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
evalexpr = "7.2.0" evalexpr = "11.0.0"
termion = "1.5.6" termion = "1.5.6"

View file

@ -7,33 +7,7 @@ Features expression evaluation, variable assignment, an interactive mode, pretty
output, and a variety of preset constants and functions focused on pure mathematics, output, and a variety of preset constants and functions focused on pure mathematics,
data science, and computer science. data science, and computer science.
## Constants More information is available on the [project wiki](https://git.vwolfe.io/valerie/qm/wiki).
| Name | Tokens | Type | Value |
| -------------- | ---------- | ----- | ------------------- |
| Euler's Number | `e` | `f64` | `2.718281828459045` |
| Golden Ratio | `phi`, `ϕ` | `f64` | `1.618033988749895` |
| Pi | `pi`, `π` | `f64` | `3.141592653589793` |
| Root Two | `√2` | `f64` | `1.414213562373095` |
## Built-in functions
### Mathematics
- `fix`: fix a float to *n* digits.
- `log`: logarithm. Defaults to natural logarithm (ln).
- `sqrt`/`√`: square root.
### Data Science
- `avg`: average an arbitrary set of numbers.
### Computer Science
- `bin`: convert an integer to a binary string or vice versa.
- `hex`: convert an integer to a hexadecimal string or vice versa.
- `oct`: convert an integer to an octal string or vice versa.
## Libraries ## Libraries

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