pub struct Quantity {
pub value: f64,
pub unit: String,
}Expand description
A magnitude expressed in a UCUM unit.
Quantity keeps its unit symbolic (as a UCUM string). Arithmetic builds a
new compound unit expression rather than eagerly reducing, so no precision is
lost and any unit (even one this build doesn’t know) round-trips. Errors
surface lazily, when you analyze or
convert_to.
use ucum::Quantity;
let force = Quantity::new(2.0, "g").mul(&Quantity::new(3.0, "m/s2"));
assert_eq!(force.value, 6.0);
// The product is commensurable with the newton's base form.
assert!(force.is_comparable("N").unwrap());
// Dividing commensurable quantities yields a dimensionless ratio.
let ratio = Quantity::new(1.0, "[lb_av]/h").div(&Quantity::new(1.0, "kg/s"));
assert!(ratio.dimension().unwrap().is_dimensionless());Fields§
§value: f64The numeric magnitude.
unit: StringThe UCUM unit expression.
Implementations§
Source§impl Quantity
impl Quantity
Sourcepub fn new(value: f64, unit: impl Into<String>) -> Self
pub fn new(value: f64, unit: impl Into<String>) -> Self
Create a quantity from a value and a UCUM unit string.
Sourcepub fn dimension(&self) -> Result<Dimension, UcumError>
pub fn dimension(&self) -> Result<Dimension, UcumError>
The dimension of this quantity’s unit. Total.
Sourcepub fn is_comparable(&self, unit: &str) -> Result<bool, UcumError>
pub fn is_comparable(&self, unit: &str) -> Result<bool, UcumError>
Whether this quantity’s unit is commensurable with unit. Total.
Sourcepub fn mul(&self, other: &Quantity) -> Quantity
pub fn mul(&self, other: &Quantity) -> Quantity
Multiply two quantities: values multiply and units concatenate. The
result unit is (self.unit).(other.unit).
Sourcepub fn div(&self, other: &Quantity) -> Quantity
pub fn div(&self, other: &Quantity) -> Quantity
Divide two quantities: values divide and the result unit is
(self.unit)/(other.unit).
Sourcepub fn convert_to(&self, unit: &str) -> Result<Quantity, UcumError>
pub fn convert_to(&self, unit: &str) -> Result<Quantity, UcumError>
Convert this quantity to unit, returning a new quantity. Total.
Errors with UcumError::NotComparable or
UcumError::UnsupportedSpecial as for crate::convert.