2020-06-12 00:06:15 +02:00
|
|
|
# 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: +, -, \*, /, !
|
2021-06-03 01:01:20 +02:00
|
|
|
* Groups: (), ⌈⌉, ⌊⌋, []
|
2020-06-12 00:06:15 +02:00
|
|
|
* [Pre-defined functions and constants](https://github.com/PaddiM8/kalk/blob/master/kalk/src/prelude.rs)
|
|
|
|
* User-defined functions and variables. `f(x, y) = xy`, `x = 5`
|
|
|
|
* Understands fairly ambiguous syntax. Eg. `2sin50 + 2xy`
|
2020-06-12 00:34:50 +02:00
|
|
|
* Syntax highlighting
|
|
|
|
* Special-symbol completion on tab. Eg. write `sqrt` and press tab. It will be turned into `√`.
|
2020-06-12 00:06:15 +02:00
|
|
|
|
2020-12-31 01:47:09 +01:00
|
|
|
## Rust Usage
|
2020-06-12 21:00:03 +02:00
|
|
|
```rust
|
2020-06-12 00:06:15 +02:00
|
|
|
use kalk::parser;
|
2020-06-12 21:00:03 +02:00
|
|
|
let mut parser_context = parser::Context::new();
|
|
|
|
let precision = 53;
|
2020-12-30 16:03:02 +01:00
|
|
|
let result = parser::eval(&mut parser_context, "5*3", precision).unwrap().unwrap();
|
|
|
|
assert_eq!(result.to_f64(), 15f64);
|
2020-06-12 00:06:15 +02:00
|
|
|
```
|
|
|
|
|
2020-12-31 01:47:09 +01:00
|
|
|
### Using f64 instead of rug::Float
|
2023-01-11 00:35:08 +01:00
|
|
|
The cargo feature `rug` enables rug, and is used by default. If you disable this, kalk will use `f64` instead, making it more portable.
|
2020-12-30 16:03:02 +01:00
|
|
|
|
2020-12-31 01:47:09 +01:00
|
|
|
### Compiling
|
2020-06-12 00:06:15 +02:00
|
|
|
Make sure you have `diffutils` `gcc` `make` and `m4` installed.
|
2020-12-31 01:47:09 +01:00
|
|
|
|
|
|
|
## JavaScript Usage
|
|
|
|
```js
|
|
|
|
const kalk = await import("@paddim8/kalk");
|
|
|
|
const context = new kalk.Context();
|
|
|
|
console.log(context.evaluate("2pi + 3").toScientificNotation().toString());
|
2023-01-11 00:35:08 +01:00
|
|
|
```
|