Skip to content

Commit

Permalink
Make operations work on byte representation. Add more operations
Browse files Browse the repository at this point in the history
  • Loading branch information
DanikVitek committed Oct 28, 2024
1 parent 303e8dc commit 7bba5bb
Show file tree
Hide file tree
Showing 6 changed files with 600 additions and 350 deletions.
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"cSpell.words": [
"numer"
"biguint",
"fract",
"numer",
"recip",
"repr",
"trunc"
]
}
35 changes: 34 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typst-plugin-bigrational"
version = "0.1.1"
version = "0.2.0"
edition = "2021"

[lib]
Expand All @@ -17,4 +17,6 @@ panic = "abort"
num-bigint = "0.4.6"
num-decimal = { version = "0.2.5", default-features = false, features = ["num-v04"] }
num-rational = "0.4.2"
num-traits = "0.2.19"
thiserror = "1.0.65"
wasm-minimal-protocol = "0.1.0"
150 changes: 70 additions & 80 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,6 @@
# Typst plugin to work with big rational numbers

The WASM binary exports the following several functions:

```rust
#[wasm_func]
fn add(
a_numer: &[u8],
a_denom: &[u8],
b_numer: &[u8],
b_denom: &[u8],
) -> Result<Vec<u8>, Box<dyn Error>>;

#[wasm_func]
fn sub(
a_numer: &[u8],
a_denom: &[u8],
b_numer: &[u8],
b_denom: &[u8],
) -> Result<Vec<u8>, Box<dyn Error>>;

#[wasm_func]
fn mul(
a_numer: &[u8],
a_denom: &[u8],
b_numer: &[u8],
b_denom: &[u8],
) -> Result<Vec<u8>, Box<dyn Error>>;

#[wasm_func]
fn div(
a_numer: &[u8],
a_denom: &[u8],
b_numer: &[u8],
b_denom: &[u8],
) -> Result<Vec<u8>, Box<dyn Error>>;

#[wasm_func]
fn repr(numer: &[u8], denom: &[u8]) -> Result<Vec<u8>, Box<dyn Error>>;

#[wasm_func]
fn to_decimal_string(
numer: &[u8],
denom: &[u8],
precision: &[u8],
) -> Result<Vec<u8>, Box<dyn Error>>;

#[wasm_func]
fn abs_diff(
a_numer: &[u8],
a_denom: &[u8],
b_numer: &[u8],
b_denom: &[u8],
) -> Result<Vec<u8>, Box<dyn Error>>;

#[wasm_func]
fn cmp(
a_numer: &[u8],
a_denom: &[u8],
b_numer: &[u8],
b_denom: &[u8],
) -> Result<Vec<u8>, Box<dyn Error>>;
```

Those functions can be used in Typst via the `rational.typ` file:
## Usage

```typ
#import "rational.typ": *
Expand All @@ -75,32 +13,90 @@ Those functions can be used in Typst via the `rational.typ` file:
$#repr(sum)$
```

## API

Functions, exported by the `rational.typ` file are:

```typ
// Converts `x` to an array of length two, representing the rational number,
// Converts `x` to bytes, representing the rational number,
// that can be used in the functions below.
// `x` might be an integer or a big integer, written as a string.
// If `x` is an array of length two, which elements are integers or big integers,
// then it is converted to the array of all big integers.
// `x` might be an integer or a big integer string.
// If `x` is an array of length two, which elements are integers
// or big integer strings, then it is converted to the array of all
// big integer strings, and then into the bytes representation.
#let rational(x)
// Functions below work with "rational numbers", integers or big integers, written as strings
// Functions below work with "rational numbers", integers or big integer strings
// Returns a + b
// Returns `a + b`
#let add(a, b)
// Returns a - b
// Returns `a - b`
#let sub(a, b)
// Returns a / b
// Returns `a / b`
#let div(a, b)
// Returns a * b
// Returns `a * b`
#let mul(a, b)
// Returns a value of type `content`, representing the rational number
#let repr(x)
// Returns `a % b`
#let rem(a, b)
// Returns `|a - b|`
#let abs-diff(a, b)
// Returns `-1` if `a < b`, `0` if `a == b`, `1` if `a > b`
#let cmp(a, b)
// Returns `-x`
#let neg(x)
// Returns `|x|`
#let abs(x)
// Rounds towards plus infinity
#let ceil(x)
// Rounds towards minus infinity
#let floor(x)
// Rounds to the nearest integer. Rounds half-way cases away from zero.
#let round(x)
// Rounds towards zero.
#let trunc(x)
// Returns the fractional part of a number, with division rounded towards zero.
// Satisfies `number == add(trunc(number), fract(number))`.
#let fract(number)
// Returns the reciprocal.
// Panics if the number is zero.
#let recip(x)
// Returns `x^y`. `y` must be an `int`, in range of `-2^32` to `2^32 - 1`
#let pow(x, y)
// Restrict a value to a certain interval.
//
// Returns `max` if `number` is greater than `max`,
// and `min` if `number` is less than `min`.
// Otherwise returns `number`.
//
// Returns error if `min` is greater than `max`.
#let clamp(number, min, max)
// Compares and returns the minimum of two values.
#let min(a, b)
// Compares and returns the maximum of two values.
#let max(a, b)
// Returns a value of type `content`, representing the rational number.
// If `is-mixed` is true, then the result is a mixed fraction,
// otherwise, it is a simple fraction.
#let repr(x, is-mixed: true)
// Returns a string, representing the rational number
#let to-decimal-str(x, precision: 8)
Expand All @@ -110,10 +106,4 @@ Functions, exported by the `rational.typ` file are:
// Returns a decimal number, representing the rational number
#let to-decimal(x, precision: 8)
// Returns |a - b|
#let abs-diff(a, b)
// Returns -1 if a < b, 0 if a == b, 1 if a > b
#let cmp(a, b)
```
Loading

0 comments on commit 7bba5bb

Please sign in to comment.