Skip to main content

Quantity

Struct Quantity 

Source
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 / sm·s⁻¹).
  • Addition and subtraction via + and - panic if the two quantities have incompatible dimensions. Use checked_add and checked_sub for fallible versions that return Result.

§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

Source

pub fn new(value: f64, unit: Unit) -> Self

Create a new quantity with a value and unit.

Source

pub fn value(&self) -> f64

Get the numeric value.

Source

pub fn unit(&self) -> &Unit

Get the unit.

Source

pub fn is_dimensionless(&self) -> bool

Check if this quantity is dimensionless.

Source

pub fn dimensionless_value(&self) -> UnitResult<f64>

Get the value as a dimensionless scalar.

Returns Err if the quantity is not dimensionless.

Source

pub fn to(&self, target: impl Into<Unit>) -> UnitResult<Quantity>

Convert to another unit.

Returns Err if the units have incompatible dimensions.

Source

pub fn to_value(&self, target: impl Into<Unit>) -> UnitResult<f64>

Get the value in a target unit.

Shorthand for .to(target)?.value().

Source

pub fn decompose(&self) -> Quantity

Decompose to SI base units.

Source

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.

Source

pub fn sqrt(&self) -> Quantity

Take the square root of this quantity.

Source

pub fn abs(&self) -> Quantity

Get the absolute value of this quantity.

Source

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());
Source

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 flux
Source

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 power
Source

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 100
Source§

impl Quantity

Source

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);
Source

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

Source

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.

Source

pub fn to_equiv_list( &self, target: impl Into<Unit>, equivs: &[Equivalency], ) -> UnitResult<Quantity>

Convert to another unit using a list of equivalencies.

Trait Implementations§

Source§

impl Add for &Quantity

Source§

type Output = Quantity

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Quantity) -> Quantity

Performs the + operation. Read more
Source§

impl Add for Quantity

Source§

type Output = Quantity

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Quantity) -> Quantity

Performs the + operation. Read more
Source§

impl Clone for Quantity

Source§

fn clone(&self) -> Quantity

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Quantity

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Quantity

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div<&Quantity> for Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Quantity) -> Quantity

Performs the / operation. Read more
Source§

impl Div<&Quantity> for f64

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Quantity) -> Quantity

Performs the / operation. Read more
Source§

impl Div<&Unit> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, unit: &Unit) -> Quantity

Performs the / operation. Read more
Source§

impl Div<&Unit> for Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, unit: &Unit) -> Quantity

Performs the / operation. Read more
Source§

impl Div<BaseUnit> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, unit: BaseUnit) -> Quantity

Performs the / operation. Read more
Source§

impl Div<BaseUnit> for Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, unit: BaseUnit) -> Quantity

Performs the / operation. Read more
Source§

impl Div<Quantity> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Quantity) -> Quantity

Performs the / operation. Read more
Source§

impl Div<Quantity> for f64

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Quantity) -> Quantity

Performs the / operation. Read more
Source§

impl Div<Unit> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, unit: Unit) -> Quantity

Performs the / operation. Read more
Source§

impl Div<Unit> for Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, unit: Unit) -> Quantity

Performs the / operation. Read more
Source§

impl Div<f64> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f64) -> Quantity

Performs the / operation. Read more
Source§

impl Div<f64> for Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f64) -> Quantity

Performs the / operation. Read more
Source§

impl Div for &Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Quantity) -> Quantity

Performs the / operation. Read more
Source§

impl Div for Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Quantity) -> Quantity

Performs the / operation. Read more
Source§

impl FromStr for Quantity

Source§

type Err = UnitError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Mul<&Quantity> for Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Quantity) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<&Quantity> for f64

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Quantity) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<&Unit> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: &Unit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<&Unit> for Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: &Unit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<BaseUnit> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: BaseUnit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<BaseUnit> for Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: BaseUnit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<Quantity> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Quantity) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<Quantity> for f64

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Quantity) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<Unit> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: Unit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<Unit> for Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: Unit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<f64> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<f64> for Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Quantity

Performs the * operation. Read more
Source§

impl Mul for &Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Quantity) -> Quantity

Performs the * operation. Read more
Source§

impl Mul for Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Quantity) -> Quantity

Performs the * operation. Read more
Source§

impl Neg for &Quantity

Source§

type Output = Quantity

The resulting type after applying the - operator.
Source§

fn neg(self) -> Quantity

Performs the unary - operation. Read more
Source§

impl Neg for Quantity

Source§

type Output = Quantity

The resulting type after applying the - operator.
Source§

fn neg(self) -> Quantity

Performs the unary - operation. Read more
Source§

impl PartialEq for Quantity

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub for &Quantity

Source§

type Output = Quantity

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Quantity) -> Quantity

Performs the - operation. Read more
Source§

impl Sub for Quantity

Source§

type Output = Quantity

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Quantity) -> Quantity

Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.