Skip to main content

Crate bxvl

Crate bxvl 

Source
Expand description

BXVL Library

bxvl is a rust library that allows variables to dynamically keep track of different unit measurements. As these variables are defined and used, they may be converted to other units, metrically scaled, arithmetically combined with others, build new units, and divided into their base units.

§Table of Contents

§Examples

Creating Values:

use bxvl::value::Value; // Unit type
use bxvl::value;        // Macro definition
use bxvl::units::{Metric, UnitTime::Second, UnitMass::Gram, UnitLength::Meter};

// Slowest
let v1:Value = match "22.3 kg*m/s^2".parse::<Value>() {
  Ok(v) => v,
  Err(e) => panic!("{}", e)
};

// Slow
let v2:Value = value!(22.3, "kg*m/s^2");

// Average
let v3:Value = match Value::new(22.3, "kg*m/s^2") {
  Ok(v) => v,
  Err(e) => panic!("{}", e)
};

// Fastest
let v4:Value = 22.3
  / Second(Metric::None)
  / Second(Metric::None)
  * Gram(Metric::Kilo)
  * Meter(Metric::None);

assert!((v1 == v2) == (v3 == v4));
assert!((v1 == v3) == (v2 == v4));
assert!((v1 == v4) == (v2 == v3));

Creating Values using other Values:

use bxvl::value::Value;
use bxvl::units::{Metric, UnitTime, UnitLength};

let time:Value = 4.0 * UnitTime::Second(Metric::None);
let dist:Value = 16.8 * UnitLength::Meter(Metric::None);

let speed:Value = dist/time;
assert!(speed.is_velocity());
assert_eq!(speed.to_string(), "4.2 m/s");

§Method Support

Values provide similar functionality to many functions that are available to other units such as i32, f32, f64 etc.

use bxvl::value::Value;
use bxvl::units::{Metric, UnitLength};

let m:Value = Value::new(f64::NAN, "feet").unwrap();
if m.is_nan() {
  println!("Our value is not a number!");
}

let a:Value = 1.4 * UnitLength::Meter(Metric::None);
let r:Value = a.sin();
assert!(r.is_radians());
assert!(r.val >= 0.985449);
assert!(r.val < 0.985450);

§Derived Units

Many of the SI units are derived from other base units. When using the values to conduct arithmetic operations, values can be explicitly asked to be ‘complex’ or ‘reduced’.

Making a complex value means combining different types into a new type.

use bxvl::value::Value;

let m:Value = Value::new(2.5, "kg").unwrap();
let acc:Value = Value::new(10.0, "m/s^2").unwrap();

let f1:Value = m*acc;
let f2:Value = (m*acc).complex();
assert!(f1.is_force() && f2.is_force());
assert!(f1.val == f2.val);
assert_eq!(f1.to_string(), "25 m*kg/s^2");
assert_eq!(f2.to_string(), "25 N");

Reducing a value means setting a value to its derived units.

use bxvl::value::Value;

let mut f:Value = Value::new(25.0, "N").unwrap();

assert!(f.is_force());
f.reduce("kg*m/s^2").unwrap();
assert!(f.is_force());
assert_eq!(f.to_string(), "25 m*kg/s^2");

This behavior is explicit and must be called by the user.

§Unit Checking

bxvl provides functions like .is_force() which will return true for both kg*m/s^2 and N. Function support includes all of the base unit types as well as extra unit combinations (See below).

FunctionMeasurement Types
is_length()Length
is_area()Length^2
is_volume()Volume
Length^3
is_temperature()Temperature
is_mass()Mass
is_density()Mass/Volume
Mass/Length^3
is_time()Time
is_substance()Substance
is_angle()Angle
is_solid_angle()Solid Angle
is_information()Information
is_velocity()Length/Time
is_acceleration()Length/Time^2
is_force()Force
Mass*acceleration
is_momentum()Mass*velocity
is_frequency()Frequency
1/Time
is_pressure()Pressure
Force/area
Mass/(Length*Time^2)
is_energy()Energy
Length*Force
Electric Potential*Electric Charge
Power*Time
Mass*area/Time^2
is_power()Power
Energy/Time
Electrical Potential*Electric Current
Mass*area/Time^3
is_electric_charge()Electric Charge
Electric Current*Time
Electric Capacitance*Electric Potential
is_electric_current()Electric Current
is_electric_potential()Electric Potential
Power/Electric Current
Energy/Electric Charge
is_capacitance()Electric Capacitance
Electric Charge/Electric Potential
Energy/Electric Charge
is_resistance()Electric Resistance
1/Electric Conductance
Electric Potential/Electric Current
is_conductance()Electric Conductance
1/Electric Resistance
Electric Current/Electric Potential
is_magnetic_flux()Magnetic Flux
Energy/Electric Current
Magnetic Flux Density*area
Electric Potential*Time
is_magnetic_flux_density()Magnetic Flux Density
Electric Potential*Time/area
Magnetic Flux/area
Force/(Electric Current*Length)
is_inductance()Electric Inductance
Electric Potential*Time/Electric Current
Electric Resistance*Time
Magnetic Flux/Electric Current
is_luminous_flux()Luminous Flux
is_illuminance()Illuminance
Luminous Flux/area
is_luminous_intensity()Luminous Intensity
is_radioactivity()Radioactivity
is_absorbed_dose()Absorbed Dose
is_equivalent_dose()Equivalent Dose
is_catalytic_activity()Catalytic Activity
Substance/Time
is_sound()Sound
is_jerk()Length/Time^3
is_snap()Length/Time^4
is_angular_velocity()Angle/Time
is_angular_acceleration()Angle/Time
is_frequency_drift()Frequency/Time
is_flow()Volume/Time
Length^3/Time
is_angular_momentum()Force*Length*Time
is_torque()Force*Length
Energy/Angle
is_energy_density()Energy/Volume
Energy/Length^3

§Conversions

All Values within their given measurement type will be able to be converted to each other. Values with multiple types, in most cases, can be converted to their compatible types.

Example converting feet into meters:

use bxvl::value::Value;

let mut m:Value = Value::new(3.2, "feet").unwrap();

m.convert("m").unwrap();

There is also direct syntax for this feature:

use bxvl::value::Value;

let mut m:Value = Value::new(5.9, "km/hr").unwrap();

m >>= "m/s";

You can use other Values for conversion:

use bxvl::value::Value;

let m:Value = Value::new(1.2, "yards").unwrap();
let n:Value = Value::new(1.0, "m").unwrap();

let k:Value = (m >> n).unwrap();

The types can also be directly used: (The fastest conversion method)

use bxvl::value::Value;
use bxvl::units::{Metric, UnitLength, UnitTime};

let mut m:Value = Value::new(5.9, "kph").unwrap();

if m.is_velocity() {
  m >>= UnitLength::Meter(Metric::None);
  m >>= UnitTime::Second(Metric::None);
} else {
  panic!();
}

Temperature cannot be converted to another unit if it has other units (like mass) within the value.

Units cannot be converted between disparate types, although there are some exceptions.

Exceptions
PeriodTime period (1/s)Frequency (Hz)
VolumeCubic length (m^3)Specific volume (ml)

These exceptions are valid conversion so long as they are the only units within a Value. This is to avoid conversion scenarios where Values produce (or are created with) neutralizing units, e.g. mm^3/ml, which is ‘unitless’. Therefore, m/s cannot be converted to m*kHz and m^3/N cannot be converted to ml/N.

§Constants

Some constants are provided for ease of use:

Namef64 numeric ValueUnits
Absolute Zero0.$K$
Avogadro’s Number6.022_140_76e23$mol^{-1}$
Faraday Constant96_485.332_123_310_01$C \over mol$
Atomic Mass Constant1.660_539_066_60e-27$kg$
Molar Gas Constant8.314_462_1$J \over K*mol$
Coulomb’s Constant8.987_551$mol^{-1}$
The Speed of Light299_792_458.0$m \over s$
Boltzmann Constant1.380_649e-23$J \over K$
Earth’s Average Gravitational Acceleration9.806_65$m \over s^2$
Newtonian Constant of Gravitation6.673_015e-11$m^3 \over kg*s^2$
Charge of an Electron1.602_176_634e-19$C$
Rydberg Constant10_973_731.568_539$m^{-1}$
Plank’s Constant6.626_070_15e-34$J \over Hz$
Vacuum Permittivity8.854_187_812_8e-12$F \over m$
use bxvl::{value::{Value, consts}, units::{UnitMass, Metric}};

let acc:Value = consts::EARTH_GRAVITY;
let m:Value = 100.0 * UnitMass::Gram(Metric::Kilo);

let f = (m * acc).complex();

assert_eq!(f.to_string(), "980.665 N");

§Unit Support

The project supports all base SI units as listed by the National Institute of Standards and Technology (NIST) and many units listed by the General Conference on Weights and Measures (CGPM). Some American Imperial Units are also supported.

§Lengths

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Meter1.0 mm
Inch0.025_4 min, inch[es]
Foot0.304_8 mft, feet foot
Yard0.914_4 myd[s], yard[s]
Mile1_609.344 mmile[s]
Astronomical Unit149_569_870_700.0 mAU
Parsec(648_000.0/π)*149_569_870_700.0 mpc
Light Year9_460_730_472_580_800.0 mlyr
Ångström0.000_000_000_1 mÅ, angstrom[s]

§Time

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Second1.0 ss
Minute60.0 smin, minute[s]
Hour3_600.0 sh[r], hour[s]
Day86_400.0 sd, day[s]

§Mass

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Gram1.0 gg
Grain453.592_37/7_000.0 ggr, grain[s]
Ounce453.592_37/16.0 goz, ounce[s]
Pound453.592_37 glb[s], pounds

§Electric Current

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Ampere1.0 AA

§Electric Charge

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Coulomb1.0 CC

§Electric Potential

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Volt1.0 VV

§Electric Conductance

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Siemens1.0 SS

§Electric Capacitance

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Farad1.0 FF, farad[s]

§Electric Resistance

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Ohm1.0 ΩΩ O

§Electric Inductance

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Henry1.0 HH

§Magnetic Flux

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Weber1.0 WbWb

§Magnetic Flux Density

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Tesla1.0 TT

§Thermal Temperature

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Celsiusc-273.15 Kc, °[Metric Prefix]c, °C
Fahrenheit((f-32.0)/1.8)-273.15 Kf, °f, °F
Kelvin1.0 KK

§Substance

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Mole1.0 molmol

§Luminous Intensity

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Candela1.0 cdcd

§Luminous Flux

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Lumen1.0 lmlm

§Illuminance

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Lux1.0 lxlx

§Spatial Volume

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Liter1.0 ll

§Pressure

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Pascal1.0 PaPa
Bar100_000.0 Pabar
Torr101_325.0/760.0 Patorr
mmHg133.322_387_415 PammHg
cmHg1333.22_387_415 PacmHg
inHg3_386.388_666_6 PainHg
Atmospheres101_325.0 PaATM, atm
Pounds per square inch6894.757 PaPSI, psi

§Geometric Angle

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Degreeπ/180.0 rad°, degree[s]
Radian1.0 radrad, radian[s]
Milliradian1_000.0 radmil[s], MIL
Minute of Angleπ/10_800.0 radmoa, MOA

§Geometric Solid Angle

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Steradian1.0 srsr

§Frequency

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Hertz1.0 HzHz

§Force

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Newton1.0 NN
Pound Force4.448_221_615_260_5 Nlbfr, lbsfr, poundforce poundsforce

§Energy

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Joule1.0 JJ
Calorie4.184 Jcal
Foot pound1.355_818 Jftlb[s], footpound[s]
Electron Volt1.6021_766_34e-19 JeV

§Power

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Watt1.0 WW
Horsepower745.699872 Whp

§Radioactivity

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Becquerel1.0 BqBq
Curie37_000_000_000.0 BqCi

§Absorbed Dosage of Ionizing Radiation

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Gray1.0 GyGy
Röntgen0.01 GyR
Rad1.0/114.025 Gyrads, Rads

§Equivalent Dosage of Ionizing Radiation

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Sievert1.0 SvSv
Rem0.01 Svrem, Rem

§Catalytic Activity

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Katal1.0 katkat

§Sound Intensity

UnitMetric Prefixing SupportBase Conversion FactorUnit string
Bel1.0 BB

§Information

:warning: Metric scaling is in base 2

i.e. kb1024 bytes, not 1000 bytes

UnitMetric Prefixing SupportBase Conversion FactorUnit string
BitKilo - Quetta8.0 bits == 1.0 bytebits
ByteKilo - Quetta1.0 bytesb, byte[s]

§Special Unit Keywords

UnitUnit stringEquivalent
Miles per hourmphmiles/hr
Kilometers per hourkphkm/hr
kilocalorieCalkcal

§Metric Prefix Identifiers

Metric namePrefix stringMetric Scaling
QuettaQ$1e30$
RonnaR$1e27$
YottaY$1e24$
ZettaZ$1e21$
ExaE$1e18$
PetaP$1e15$
TeraT$1e12$
GigaG$1e9$
MegaM$1e6$
Kilok$1e3$
Hectoh$1e2$
Decada$1e1$
None$1$
Decid$1e-1$
Centic$1e-2$
Millim$1e-3$
Microμ u$1e-6$
Nanon$1e-9$
Picop$1e-12$
Femtof$1e-15$
Attoa$1e-18$
Zeptoz$1e-21$
Yoctoy$1e-24$
Rontor$1e-27$
Quectoq$1e-30$

Note that some unit strings like eV could be indented to be Exa-Volts or Electron Volts. The library is case sensitive and will default to the ‘least complex’ unit that matches. So Electron Volts will be the parsed result. To get Exa-Volts, the user must properly specify EV or simply V for volts and then convert to the Exa metric scaler.

Modules§

consts
Various const definitions that are used throughout [bxvl]
errors
The module for [bxvl] specific error definitions
units
The main module for [Metric] and [Unit] types
value
The module used for [Value] implementations

Macros§

create_defs
The [create_defs] macro is used to define operations for primitive type operations with values.
value
Macro to create a new Value

Constants§

VERSION
The BXVL Library Version