13 releases

0.1.2 Sep 17, 2022
0.1.1 Mar 24, 2022
0.0.12 Mar 11, 2022
0.0.9 Mar 20, 2021
0.0.3 Jul 19, 2017

#60 in Parser tooling

Download history 13/week @ 2023-01-26 21/week @ 2023-02-02 38/week @ 2023-02-09 54/week @ 2023-02-16 6/week @ 2023-02-23 55/week @ 2023-03-02 30/week @ 2023-03-09 72/week @ 2023-03-16 15/week @ 2023-03-23 11/week @ 2023-03-30 32/week @ 2023-04-06 22/week @ 2023-04-13 6/week @ 2023-04-20 23/week @ 2023-04-27 49/week @ 2023-05-04 22/week @ 2023-05-11

113 downloads per month
Used in ferro

MIT license

63KB
1.5K SLoC

Documentation

A library for evaluating math expressions.

Using the library

fn main() {
  let input = "sin(0.2)^2 + cos(0.2)^2";
  let expr = ShuntingParser::parse_str(input).unwrap();
  let result = MathContext::new().eval(&expr).unwrap();
  println!("{} = {}", expr, result);
}

A MathContext

MathContext allows keeping context across multiple invocations to parse and evaluate. You can do this via the setvar method.

The tool in the crate

The crate also ship with the tox binary with a math repl.

$ tox
>> 4!
24
>> a = sin(0.2)^2 + cos(0.2)^2
>> a
1
>> (-3)!
NaN
>> (84 % (5/2)) !
1.32934
>> pi * 2.1^2 / cbrt(-(6+3))
-6.660512

Dependencies

~1MB
~21K SLoC