DimensionalVariable

Struct DimensionalVariable 

Source
pub struct DimensionalVariable {
    pub value: f64,
    pub unit: [f64; 8],
}

Fields§

§value: f64§unit: [f64; 8]

Implementations§

Source§

impl DimensionalVariable

A struct representing a dimensional variable with a value and a unit.

Source

pub fn new(value: f64, unit_str: &str) -> Result<Self, String>

Creates a new DimensionalVariable with the given value and unit.

Rules for writing the unit string:

  • Use a single / as a delimiter between the numerator and denominator.
  • Use - as a delimiter between individual units
  • Exponents can be represented either by using ^ to indicate exponent (ex. m^2) or without the delimiter (ex. m2)
  • Inverses can be represented either by negative exponents or in the denominator (ex. m^-2 or 1/m^2)

Returns an error if the unit string is invalid or contains unknown units.

Source

pub fn value(&self) -> f64

Returns the value of this DimensionalVariable.

Source

pub fn value_in(&self, unit_str: &str) -> Result<f64, String>

Converts the value of this DimensionalVariable to the specified unit. Returns an error if the unit string is invalid or incompatible.

Source

pub fn check_compatibility(&self, other_unit: [f64; 8]) -> bool

Source

pub fn unit(&self) -> [f64; 8]

Returns the base unit array of this DimensionalVariable.

Source

pub fn is_unitless(&self) -> bool

Returns whether the variable is unitless (all base exponents are 0).

Source

pub fn try_add( &self, other: &DimensionalVariable, ) -> Result<DimensionalVariable, String>

Fallible add with unit compatibility check.

Source

pub fn try_sub( &self, other: &DimensionalVariable, ) -> Result<DimensionalVariable, String>

Fallible subtraction with unit compatibility check.

Source

pub fn powi(&self, exp: i32) -> DimensionalVariable

Raise to integer power. Units exponents are multiplied by exp. Returns a new DimensionalVariable.

Source

pub fn powf(&self, exp: f64) -> Result<DimensionalVariable, String>

Raise to floating power. Returns a new DimensionalVariable.

Source

pub fn sqrt(&self) -> Result<DimensionalVariable, String>

Square root. Allowed only when all unit exponents are value >= 0 (no complex results). Returns a new DimensionalVariable.

Source

pub fn ln(&self) -> Result<f64, String>

Natural logarithm. Requires unitless and value > 0.

Source

pub fn log2(&self) -> Result<f64, String>

Base-2 logarithm. Requires unitless and value > 0.

Source

pub fn log10(&self) -> Result<f64, String>

Base-10 logarithm. Requires unitless and value > 0.

Source

pub fn sin(&self) -> Result<f64, String>

Sine function. Requires angle (radians).

Source

pub fn cos(&self) -> Result<f64, String>

Cosine function. Requires angle (radians).

Source

pub fn tan(&self) -> Result<f64, String>

Tangent function. Requires angle (radians).

Source

pub fn asin(&self) -> Result<DimensionalVariable, String>

Arcsine function. Requires unitless value in [-1, 1]. Returns angle in radians.

Source

pub fn acos(&self) -> Result<DimensionalVariable, String>

Arccosine function. Requires unitless value in [-1, 1]. Returns angle in radians.

Source

pub fn atan(&self) -> Result<DimensionalVariable, String>

Arctangent function. Requires unitless value. Returns angle in radians.

Source

pub fn neg(&self) -> DimensionalVariable

Negate the value, keeping the same unit.

Source

pub fn abs(&self) -> DimensionalVariable

Returns the absolute value, keeping the same unit.

Trait Implementations§

Source§

impl<'a, 'b> Add<&'b DimensionalVariable> for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &'b DimensionalVariable) -> Self::Output

Performs the + operation. Read more
Source§

impl<'b> Add<&'b DimensionalVariable> for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &'b DimensionalVariable) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a> Add<DimensionalVariable> for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the + operator.
Source§

fn add(self, rhs: DimensionalVariable) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the + operator.
Source§

fn add(self, rhs: DimensionalVariable) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<&DimensionalVariable> for DimensionalVariable

Source§

fn add_assign(&mut self, rhs: &DimensionalVariable)

Performs the += operation. Read more
Source§

impl Display for DimensionalVariable

Source§

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

Formats the DimensionalVariable as a string in the form “value unit”.

The unit is displayed in fraction style (e.g., “m/s^2”) with:

  • Positive exponents in the numerator
  • Negative exponents in the denominator (as positive values)
  • Exponents of 1 are omitted
  • Units with exponent 0 are omitted
  • Unitless quantities show “(unitless)”

Examples:

  • 9.81 m/s^2 for acceleration
  • 100 kg*m^2/s^2 for energy
  • 3.14 rad for angle
  • 5 (unitless) for dimensionless quantities
Source§

impl<'a, 'b> Div<&'b DimensionalVariable> for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'b DimensionalVariable) -> Self::Output

Performs the / operation. Read more
Source§

impl<'b> Div<&'b DimensionalVariable> for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'b DimensionalVariable) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a> Div<&'a DimensionalVariable> for f64

Source§

type Output = DimensionalVariable

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'a DimensionalVariable) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a> Div<DimensionalVariable> for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the / operator.
Source§

fn div(self, rhs: DimensionalVariable) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<DimensionalVariable> for f64

Source§

type Output = DimensionalVariable

The resulting type after applying the / operator.
Source§

fn div(self, rhs: DimensionalVariable) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a> Div<f64> for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f64) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<f64> for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f64) -> Self::Output

Performs the / operation. Read more
Source§

impl Div for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the / operator.
Source§

fn div(self, rhs: DimensionalVariable) -> Self::Output

Performs the / operation. Read more
Source§

impl DivAssign<&DimensionalVariable> for DimensionalVariable

Source§

fn div_assign(&mut self, rhs: &DimensionalVariable)

Performs the /= operation. Read more
Source§

impl DivAssign<f64> for DimensionalVariable

Source§

fn div_assign(&mut self, rhs: f64)

Performs the /= operation. Read more
Source§

impl<'a, 'b> Mul<&'b DimensionalVariable> for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &'b DimensionalVariable) -> Self::Output

Performs the * operation. Read more
Source§

impl<'b> Mul<&'b DimensionalVariable> for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &'b DimensionalVariable) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Mul<&'a DimensionalVariable> for f64

Source§

type Output = DimensionalVariable

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &'a DimensionalVariable) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Mul<DimensionalVariable> for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: DimensionalVariable) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<DimensionalVariable> for f64

Source§

type Output = DimensionalVariable

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: DimensionalVariable) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Mul<f64> for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<f64> for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: DimensionalVariable) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<&DimensionalVariable> for DimensionalVariable

Source§

fn mul_assign(&mut self, rhs: &DimensionalVariable)

Performs the *= operation. Read more
Source§

impl MulAssign<f64> for DimensionalVariable

Source§

fn mul_assign(&mut self, rhs: f64)

Performs the *= operation. Read more
Source§

impl<'a> Neg for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Neg for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for DimensionalVariable

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 PartialOrd for DimensionalVariable

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, 'b> Sub<&'b DimensionalVariable> for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &'b DimensionalVariable) -> Self::Output

Performs the - operation. Read more
Source§

impl<'b> Sub<&'b DimensionalVariable> for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &'b DimensionalVariable) -> Self::Output

Performs the - operation. Read more
Source§

impl<'a> Sub<DimensionalVariable> for &'a DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: DimensionalVariable) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for DimensionalVariable

Source§

type Output = DimensionalVariable

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: DimensionalVariable) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign<&DimensionalVariable> for DimensionalVariable

Source§

fn sub_assign(&mut self, rhs: &DimensionalVariable)

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> 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> 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.