pub enum Unit {
Base(BaseUnit),
Composite(CompositeUnit),
Dimensionless {
scale: f64,
},
}Expand description
A physical unit.
Units can be base units (like meter), named derived units (like newton), composite units (like m/s), or dimensionless.
§Examples
use iridium_units::prelude::*;
// Base units are Copy — use them directly
let mass = 10.0 * KG;
// Arithmetic creates composite units
let velocity_unit = KM / H;
// Check dimensions
assert_eq!((M / S).dimension(), (KM / H).dimension());
// Convert between compatible units
let speed = 100.0 * &(KM / H);
let in_ms = speed.to(M / S).unwrap();Variants§
Base(BaseUnit)
A base irreducible unit (meter, second, kilogram, etc.)
Composite(CompositeUnit)
A composite unit from arithmetic operations (m/s, kg·m/s², etc.)
Dimensionless
Dimensionless with a scale factor.
Implementations§
Source§impl Unit
impl Unit
Sourcepub fn dimensionless() -> Self
pub fn dimensionless() -> Self
Create a dimensionless unit with scale 1.
Sourcepub fn dimensionless_scaled(scale: f64) -> Self
pub fn dimensionless_scaled(scale: f64) -> Self
Create a dimensionless unit with a scale factor.
Sourcepub fn is_dimensionless(&self) -> bool
pub fn is_dimensionless(&self) -> bool
Check if this unit is dimensionless.
Sourcepub fn offset(&self) -> f64
pub fn offset(&self) -> f64
Get the additive offset relative to the SI base unit.
Most units have offset 0.0. Offset units like Celsius (273.15)
and Fahrenheit (459.67) use this for affine conversions.
The formula is: SI_value = (value + offset) * scale.
Offsets are only defined for Unit::Base. Composite units are
treated as interval units, so composing an offset base unit does
not preserve its additive offset.
Sourcepub fn has_offset(&self) -> bool
pub fn has_offset(&self) -> bool
Check if this unit has an additive offset (e.g., Celsius, Fahrenheit).
Only true for Unit::Base values with a non-zero offset.
Sourcepub fn to_si(&self, value: f64) -> f64
pub fn to_si(&self, value: f64) -> f64
Convert a value in this unit to SI base units.
For pure scale units: value * scale
For offset base units: (value + offset) * scale
Affine offsets are only applied for Unit::Base. Composite
units are converted using scale alone (interval semantics).
Sourcepub fn from_si(&self, si_value: f64) -> f64
pub fn from_si(&self, si_value: f64) -> f64
Convert a value from SI base units to this unit.
For simple units: si_value / scale
For offset units: si_value / scale - offset
Sourcepub fn conversion_factor(&self, to: &Unit) -> UnitResult<f64>
pub fn conversion_factor(&self, to: &Unit) -> UnitResult<f64>
Get the conversion factor to convert from this unit to another.
This returns a single multiplicative factor, which only works for
units without additive offsets. For offset units like Celsius or
Fahrenheit, use Quantity::to instead.
Returns Err if the units have incompatible dimensions or if
either unit has an additive offset.
Sourcepub fn to_composite(&self) -> CompositeUnit
pub fn to_composite(&self) -> CompositeUnit
Convert this unit to a composite representation.
Sourcepub fn pow(&self, exp: impl Into<Rational16>) -> Unit
pub fn pow(&self, exp: impl Into<Rational16>) -> Unit
Raise this unit to a power.