Crates.io | kalk |
lib.rs | kalk |
version | 3.2.1 |
source | src |
created_at | 2020-06-11 21:52:21.582782 |
updated_at | 2024-10-03 18:03:13.719589 |
description | A math evaluator library that supports user-defined functions, variables and units, and can handle fairly ambiguous syntax. |
homepage | |
repository | https://github.com/PaddiM8/kalker/tree/master/kalk |
max_upload_size | |
id | 253021 |
size | 322,017 |
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
+
, -
, *
, /
, !
()
, []
, ⌈ceil⌉
, ⌊floor⌋
f(x, y) = xy
, x = 5
f'(2)
, sin'(-pi)
∫(0, pi, sin(x) dx)
or ∫(0, π, sin(x) dx)
, maybe sometimes be slightly off2sin50 + 2xy
sum(start, to, expression)
Eg. sum(1, 3, 2n+1)
is the same as 2*1+1 + 2*2+1 + 2*3+1
= 15
f(x) = { f(x + 1) if x <= 1; x otherwise }
, pressing enter before typing the final }
will make a new line without submitting0b1101
, 0o5.3
, 0xff
or a format like 1101_2
. The latter does not support letters, as they would be interpreted as variablesans
variable to get the value of the previously calculated expressionuse 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);
The cargo feature rug
enables rug, and is used by default. If you disable this, kalk will use f64
instead, making it more portable.
Make sure you have diffutils
gcc
make
and m4
installed.
const kalk = await import("@paddim8/kalk");
const context = new kalk.Context();
console.log(context.evaluate("2pi + 3").toScientificNotation().toString());