Skip to content

Commit

Permalink
Add more built in functions sq, sqrt, cube, cbrt, round
Browse files Browse the repository at this point in the history
  • Loading branch information
OchirErkhembayar committed Jan 16, 2024
1 parent d196d79 commit 62d1bbf
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "qcalc"
version = "0.2.2"
version = "0.2.3"
edition = "2021"
authors = ["ochir <ochir_erkhembayar@yahoo.com>"]
description = """
Expand Down
11 changes: 11 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,15 @@ mod tests {
input_and_evaluate(&mut app, "");
assert!(app.output.is_some_and(|o| o.is_err()));
}

#[test]
fn test_built_in_fns() {
let mut app = App::new();
let input_and_ans = [("sq(2)", 4.0), ("sqrt(16)", 4.0), ("cube(2)", 8.0), ("cbrt(8)", 2.0)];

input_and_ans.iter().for_each(|(input, exp)| {
input_and_evaluate(&mut app, input);
assert_output(&app, *exp);
});
}
}
46 changes: 14 additions & 32 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,6 @@ impl Interpreter {
HashMap::from_iter([
("pi".to_string(), Value::Num(PI)),
("e".to_string(), Value::Num(E)),
(
"deg".to_string(),
Value::Fn(Function::new(
vec!["rads".to_string()],
Expr::Binary(
Box::new(Expr::Var("rads".to_string())),
Token::Mult,
Box::new(Expr::Binary(
Box::new(Expr::Num(180.0)),
Token::Div,
Box::new(Expr::Num(PI)),
)),
),
HashMap::new(),
)),
),
(
"rads".to_string(),
Value::Fn(Function::new(
vec!["degs".to_string()],
Expr::Binary(
Box::new(Expr::Var("degs".to_string())),
Token::Mult,
Box::new(Expr::Binary(
Box::new(Expr::Num(PI)),
Token::Div,
Box::new(Expr::Num(180.0)),
)),
),
HashMap::new(),
)),
),
])
}

Expand Down Expand Up @@ -183,10 +151,24 @@ impl Interpreter {
Func::Tanh => arg.tanh(),
Func::Ln => arg.ln(),
Func::Log(b) => arg.log(*b),
Func::Degs => arg.to_degrees(),
Func::Rads => arg.to_radians(),
Func::Sq => arg.powi(2),
Func::Sqrt => arg.sqrt(),
Func::Cube => arg.powi(3),
Func::Cbrt => arg.cbrt(),
Func::Round => arg.round(),
};
Ok(val)
}
}
.map(|n| {
if (n.round() - n).abs() < 1e-10 {
n.round()
} else {
n
}
})
}

pub fn reset_vars(&mut self) {
Expand Down
28 changes: 28 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const TAN: &str = "tan";
const TANH: &str = "tanh";
const LOG: &str = "log";
const LN: &str = "ln";
const DEGS: &str = "degs";
const RADS: &str = "rads";
const SQRT: &str = "sqrt";
const SQ: &str = "sq";
const CBRT: &str = "cbrt";
const CUBE: &str = "cube";
const ROUND: &str = "round";

#[derive(Debug)]
pub struct Parser {
Expand All @@ -33,6 +40,13 @@ pub enum Func {
Tanh,
Ln,
Log(f64),
Degs,
Rads,
Sq,
Sqrt,
Cube,
Cbrt,
Round,
}

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -276,6 +290,13 @@ impl Parser {
));
}
}
DEGS => Func::Degs,
RADS => Func::Rads,
SQ => Func::Sq,
SQRT => Func::Sqrt,
CUBE => Func::Cube,
CBRT => Func::Cbrt,
ROUND => Func::Round,
_ => return Ok(Expr::Var(func)),
};
self.consume(Token::LParen, "Missing opening parentheses")?;
Expand Down Expand Up @@ -315,6 +336,13 @@ impl Display for Func {
Func::Tanh => TANH,
Func::Ln => LN,
Func::Log(base) => return inner_write(format!("log{}", base), f),
Func::Degs => DEGS,
Func::Rads => RADS,
Func::Sq => SQ,
Func::Sqrt => SQRT,
Func::Cube => CUBE,
Func::Cbrt => CBRT,
Func::Round => ROUND,
}
)
}
Expand Down
9 changes: 5 additions & 4 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ Available Functions
_arg_ should be replaced by an expression eg. ln(2)
_rads_ indicates that the argument should be in radians eg. cos(p)
cos(_rads_) cosh(_rads_)
sin(_rads_) sinh(_rads_)
tan(_rads_) tanh(_rads_)
log_base_(_arg_) ln(_arg_)
cos(_rads_) cosh(_rads_) sq(__arg__)
sin(_rads_) sinh(_rads_) sqrt(__arg__)
tan(_rads_) tanh(_rads_) cube(__arg__)
log_base_(_arg_) ln(_arg_) cbrt(__arg__)
degs(_rads_) rads(_degs_) round(__arg__)
The result of a successful eval is stored in \"ans\"
Expand Down

0 comments on commit 62d1bbf

Please sign in to comment.