#math #calculator #evaluator

kalk

A math evaluator library that supports user-defined functions, variables and units, and can handle fairly ambiguous syntax

38 releases (22 stable)

3.0.0 Jun 2, 2022
2.2.0 Jan 1, 2022
2.1.2 Oct 1, 2021
2.1.1 Jun 2, 2021
0.2.0 Jun 18, 2020

#98 in Parser implementations

Download history 17/week @ 2022-06-13 35/week @ 2022-06-20 18/week @ 2022-06-27 64/week @ 2022-07-04 63/week @ 2022-07-11 97/week @ 2022-07-18 92/week @ 2022-07-25 62/week @ 2022-08-01 37/week @ 2022-08-08 18/week @ 2022-08-15 96/week @ 2022-08-22 16/week @ 2022-08-29 137/week @ 2022-09-05 20/week @ 2022-09-12 18/week @ 2022-09-19 24/week @ 2022-09-26

201 downloads per month
Used in 2 crates

MIT and LGPL-3.0+

305KB
7.5K SLoC

kalk

Kalk is a math parser library that supports user-defined variables and functions. An example of what it can parse:

f(x, y) = sum(1, 3, (2sin4/x!)^y) + cos(n deg)
a = 3
f(a, 2)

>> 1.1899401098014355

Features

  • Operators: +, -, *, /, !
  • Groups: (), ⌈⌉, ⌊⌋, []
  • Pre-defined functions and constants
  • User-defined functions and variables. f(x, y) = xy, x = 5
  • Understands fairly ambiguous syntax. Eg. 2sin50 + 2xy
  • Syntax highlighting
  • Special-symbol completion on tab. Eg. write sqrt and press tab. It will be turned into .

Rust Usage

use kalk::parser;
let mut parser_context = parser::Context::new();
let precision = 53;
let result = parser::eval(&mut parser_context, "5*3", precision).unwrap().unwrap();
assert_eq!(result.to_f64(), 15f64);

Using f64 instead of rug::Float

The cargo feature rug enables rug, and is used by default. If you disalbe this, kalk will use f64 instead, making it more portable.

Compiling

Make sure you have diffutils gcc make and m4 installed.

JavaScript Usage

const kalk = await import("@paddim8/kalk");
const context = new kalk.Context();
console.log(context.evaluate("2pi + 3").toScientificNotation().toString());

Dependencies

~17MB
~437K SLoC