safe-bigmath 0.3.0

Provides non-overflowing, non-panicking numeric types as well as safe big integer and decimal that can scale to any size safely and gracefully, only wasting memory when extra precision is needed
docs.rs failed to build safe-bigmath-0.3.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: safe-bigmath-0.4.1

safe-bigmath

Crates.io Docs CI License

Safe, non-panicking numeric primitives built on pure-Rust num-bigint. safe-bigmath gives you:

  • SafeInt: arbitrary-precision integers with ergonomic operator overloads.
  • SafeDec<D>: fixed-scale decimals backed by arbitrary-precision SafeInt; the const generic D sets how many decimal places are stored exactly.
  • Parsing helpers for turning strings into safe numeric values.
  • No hidden panics: division returns Option, parsing reports structured errors.
  • std by default; disable default features for no_std + alloc (works on wasm32-unknown-unknown).

Quick start

[dependencies]
safe-bigmath = "0.2"

# Optional: go `no_std` + `alloc`
# safe-bigmath = { version = "0.2", default-features = false }

Safe integers

use safe_bigmath::SafeInt;

let a = SafeInt::from(10);
let b = SafeInt::from(3);

assert_eq!((&a / &b).unwrap(), SafeInt::from(3)); // division is fallible
assert_eq!(&a + &b, SafeInt::from(13));
assert_eq!(SafeInt::from(5) / SafeInt::from(0), None); // no panic on zero div

Fixed-scale decimals

use safe_bigmath::SafeDec;

let price: SafeDec<2> = "12.50".parse().unwrap();
let qty: SafeDec<2> = "3.00".parse().unwrap();
let total = price * qty;

assert_eq!(total.to_string(), "37.50");

Pow of ratios with scaling

Compute (x / (x + dx))^(w1 / w2) scaled to perquintill:

use safe_bigmath::SafeInt;

let x = SafeInt::from(21_000_000_000_000_000u64);
let dx = SafeInt::from(7_000_000_000_000_000u64);
let w1 = SafeInt::from(600_000_000_000_000_000u128);
let w2 = SafeInt::from(400_000_000_000_000_000u128);
let perquintill = SafeInt::from(1_000_000_000_000_000_000u128);

let result = SafeInt::pow_ratio_scaled(&x, &(x.clone() + dx), &w1, &w2, 0, &perquintill).unwrap();
assert_eq!(result, SafeInt::from(649_519_052_838_328_985u128));

Feature flags

  • std (on by default): enables std support for downstream crates; disable default features for no_std + alloc.

Supported targets

  • std targets (default).
  • no_std targets with alloc via --no-default-features.
  • wasm32-unknown-unknown (CI builds and test-compiles both --no-default-features and --all-features).

Testing

cargo test --workspace
cargo test --workspace --no-default-features
cargo test --workspace --all-features
cargo test --target wasm32-unknown-unknown --no-default-features --no-run
cargo test --target wasm32-unknown-unknown --all-features --no-run

License

MIT © sam0x17