DimensionalVariable (DV) Rust Library
| FULL DOCUMENTATION AT https://dv.alextac.com |
|---|
| This crate is the multi‑language core, so full documentation is centralized on a single website. For the exhaustive unit catalog, extended guides/examples, design rationale, and more, see the link above. |
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. Exponents are f64, so fractional dimensions (like square‑roots) are supported. 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, powi/powf adjust exponents, sqrt halves exponents; logs and trig require unitless).
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
// Fractional exponents via sqrt and powf
let a = new.unwrap;
let r = a.sqrt.unwrap;
assert_eq!;
let b = new.unwrap;
let r2 = b.sqrt.unwrap;
assert_eq!;
Check out the the docs for more!