DimensionalVariable (DV) Rust Library
Info: This crate is the multi‑language core. For the exhaustive unit catalog and extended guides see dv.alextac.com.
DimensionalVariable (DV) povides parsing of unit strings (e.g. "m/s^2", "kWh", "1/ft^2"), normalization to base SI dimensions, arithmetic with unit checking, and value conversion. Features include:
- Parse compound units with
/,-, exponents (^or suffixed digits), negatives. - Convert between compatible units (
m↔cm,kWh↔J, etc.). - Dimensionally aware math (add/sub match units, pow/sqrt/log validation).
Examples
The library will use the units to convert to a standardized unit (base SI units). Therefore, you can switch between units simply by using the .value_in function. These get checked so you are unable to convert between 2 incompatible units.
use DimensionalVariable as dv;
// Example
let len = new.unwrap;
let len_cm = len.value_in.unwrap;
assert_eq!;
// Bad Example
let d = new.expect;
let result = d.value_in;
assert!;
Most math is supported, including +, -, *, /, powi, powf, sqrt, abs. Units are checked to make sure math operations are compatible.
use DimensionalVariable as dv;
// Example
let v = new.expect;
let t = new.expect;
let d = &v * &t;
assert_eq!;
assert_eq!;
// Bad Example
let m = new.expect;
let s = new.expect;
assert!;
assert!;
// Bad Example, throws panic
let m = new.expect;
let s = new.expect;
let _ = m + s; // should panic due to incompatible units
Check out the the docs for more!