pub struct Quantity { /* private fields */ }Expand description
A physical quantity: a numeric value with an associated unit.
§Arithmetic
- Multiplication and division always succeed and combine dimensions
automatically (e.g.,
m * m→m²,m / s→m·s⁻¹). - Addition and subtraction via
+and-panic if the two quantities have incompatible dimensions. Usechecked_addandchecked_subfor fallible versions that returnResult.
§Examples
use iridium_units::prelude::*;
let distance = 100.0 * M;
let time = 9.58 * S;
let speed = &distance / &time;
let speed_kmh = speed.to(&(KM / H)).unwrap();Implementations§
Source§impl Quantity
impl Quantity
Sourcepub fn is_dimensionless(&self) -> bool
pub fn is_dimensionless(&self) -> bool
Check if this quantity is dimensionless.
Sourcepub fn dimensionless_value(&self) -> UnitResult<f64>
pub fn dimensionless_value(&self) -> UnitResult<f64>
Get the value as a dimensionless scalar.
Returns Err if the quantity is not dimensionless.
Sourcepub fn to(&self, target: impl Into<Unit>) -> UnitResult<Quantity>
pub fn to(&self, target: impl Into<Unit>) -> UnitResult<Quantity>
Convert to another unit.
Returns Err if the units have incompatible dimensions.
Sourcepub fn to_value(&self, target: impl Into<Unit>) -> UnitResult<f64>
pub fn to_value(&self, target: impl Into<Unit>) -> UnitResult<f64>
Get the value in a target unit.
Shorthand for .to(target)?.value().
Sourcepub fn pow(&self, exp: impl Into<Rational16>) -> Quantity
pub fn pow(&self, exp: impl Into<Rational16>) -> Quantity
Raise this quantity to a power.
Accepts any type convertible to Rational16, including Rational16 itself.
When passing an i32, the value must fit in i16 range.
Uses powi for integer exponents and powf for fractional exponents.
Sourcepub fn is_logarithmic(&self) -> bool
pub fn is_logarithmic(&self) -> bool
Check if this quantity has a logarithmic unit (magnitude dimension).
Logarithmic units include magnitudes, decibels, and dex.
§Example
use iridium_units::prelude::*;
use iridium_units::systems::logarithmic::MAG;
let mag = 5.0 * MAG;
assert!(mag.is_logarithmic());
let length = 10.0 * M;
assert!(!length.is_logarithmic());Sourcepub fn mag_to_flux_ratio(&self) -> UnitResult<f64>
pub fn mag_to_flux_ratio(&self) -> UnitResult<f64>
Convert a magnitude quantity to a flux ratio.
Uses the Pogson formula: F/F₀ = 10^(-0.4 * m)
Returns Err if this quantity does not have magnitude dimension.
§Example
use iridium_units::prelude::*;
use iridium_units::systems::logarithmic::MAG;
let star = 5.0 * MAG; // 5th magnitude
let flux = star.mag_to_flux_ratio().unwrap();
assert!((flux - 0.01).abs() < 1e-10); // 1/100 of reference fluxSourcepub fn db_to_power_ratio(&self) -> UnitResult<f64>
pub fn db_to_power_ratio(&self) -> UnitResult<f64>
Convert a decibel quantity to a power ratio.
Uses the formula: P/P₀ = 10^(dB/10)
Returns Err if this quantity does not have magnitude dimension.
§Example
use iridium_units::prelude::*;
use iridium_units::systems::logarithmic::DB;
let signal = 10.0 * DB; // 10 dB
let power = signal.db_to_power_ratio().unwrap();
assert!((power - 10.0).abs() < 1e-10); // 10x powerSourcepub fn dex_to_ratio(&self) -> UnitResult<f64>
pub fn dex_to_ratio(&self) -> UnitResult<f64>
Convert a dex quantity to a linear ratio.
Uses the formula: x/x₀ = 10^dex
Returns Err if this quantity does not have magnitude dimension.
§Example
use iridium_units::prelude::*;
use iridium_units::systems::logarithmic::DEX;
let order = 2.0 * DEX; // 2 orders of magnitude
let ratio = order.dex_to_ratio().unwrap();
assert!((ratio - 100.0).abs() < 1e-10); // factor of 100Source§impl Quantity
impl Quantity
Sourcepub fn checked_add(&self, rhs: &Quantity) -> UnitResult<Quantity>
pub fn checked_add(&self, rhs: &Quantity) -> UnitResult<Quantity>
Add two quantities, returning an error if their dimensions don’t match.
This is the fallible version of the + operator.
use iridium_units::prelude::*;
let a = 1.0 * KM;
let b = 500.0 * M;
let c = a.checked_add(&b).unwrap();
assert!((c.value() - 1.5).abs() < 1e-10);Sourcepub fn checked_sub(&self, rhs: &Quantity) -> UnitResult<Quantity>
pub fn checked_sub(&self, rhs: &Quantity) -> UnitResult<Quantity>
Subtract two quantities, returning an error if their dimensions don’t match.
This is the fallible version of the - operator.
use iridium_units::prelude::*;
let a = 1.0 * KM;
let b = 500.0 * M;
let c = a.checked_sub(&b).unwrap();
assert!((c.value() - 0.5).abs() < 1e-10);Source§impl Quantity
impl Quantity
Sourcepub fn to_equiv(
&self,
target: impl Into<Unit>,
equiv: Equivalency,
) -> UnitResult<Quantity>
pub fn to_equiv( &self, target: impl Into<Unit>, equiv: Equivalency, ) -> UnitResult<Quantity>
Convert to another unit using equivalencies.
First tries a direct dimensional conversion. If that fails, tries each equivalency in order until one succeeds.
Sourcepub fn to_equiv_list(
&self,
target: impl Into<Unit>,
equivs: &[Equivalency],
) -> UnitResult<Quantity>
pub fn to_equiv_list( &self, target: impl Into<Unit>, equivs: &[Equivalency], ) -> UnitResult<Quantity>
Convert to another unit using a list of equivalencies.