Skip to content

Commit

Permalink
Add Ratio quantity.
Browse files Browse the repository at this point in the history
Includes `From<Ratio> for V` and `From<V> for Ratio` implementations to
allow for easy conversions be Ratio and the underlying storage type.
Resolves #64.
  • Loading branch information
iliekturtles committed Apr 29, 2018
1 parent 6584b03 commit de55871
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/si/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ system! {
units: SI {
acceleration::Acceleration,
amount_of_substance::AmountOfSubstance,
available_energy::AvailableEnergy,
area::Area,
available_energy::AvailableEnergy,
density::Density,
electric_current::ElectricCurrent,
electric_potential::ElectricPotential,
Expand All @@ -37,6 +37,7 @@ system! {
mass_rate::MassRate,
power::Power,
pressure::Pressure,
ratio::Ratio,
thermodynamic_temperature::ThermodynamicTemperature,
time::Time,
velocity::Velocity,
Expand Down
107 changes: 107 additions & 0 deletions src/si/ratio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//! Ratio (dimensionless).
quantity! {
/// Ratio (dimensionless).
quantity: Ratio; "ratio";
/// Ratio dimension (dimensionless).
dimension: ISQ<
Z0, // length
Z0, // mass
Z0, // time
Z0, // electric current
Z0, // thermodynamic temperature
Z0, // amount of substance
Z0>; // luminous intensity
units {
@ratio: 1.0; "", "", "";
@part_per_hundred: 1.0_E-2; "parts per hundred", "part per hundred", "parts per hundred";
@percent: 1.0_E-2; "%", "percent", "percent";
@part_per_thousand: 1.0_E-3; "parts per thousand", "part per thousand",
"parts per thousand";
@per_mille: 1.0_E-3; "‰", "per mille", "per mille";
@part_per_ten_thousand: 1.0_E-4; "parts per ten thousand", "part per then thousand",
"parts per ten thousand"; // ‱, doesn't display properly.
@basis_point: 1.0_E-4; "bp", "basis point", "basis points";
@part_per_million: 1.0_E-6; "ppm", "part per million", "parts per million";
@part_per_billion: 1.0_E-9; "ppb", "part per billion", "parts per billion";
@part_per_trillion: 1.0_E-12; "ppt", "part per trillion", "parts per trillion";
@part_per_quadrillion: 1.0_E-15; "ppq", "part per quadrillion", "parts per quadrillion";
}
}

mod convert {
use super::*;

impl<U, V> ::lib::convert::From<V> for Ratio<U, V>
where
U: ::si::Units<V> + ?Sized,
V: ::num::Num + ::Conversion<V>,
{
fn from(t: V) -> Self {
Ratio {
dimension: ::lib::marker::PhantomData,
units: ::lib::marker::PhantomData,
value: t,
}
}
}

storage_types! {
use super::*;

impl<U> ::lib::convert::From<Ratio<U, V>> for V
where
U: ::si::Units<V> + ?Sized,
V: ::num::Num + ::Conversion<V>,
{
fn from(t: Ratio<U, V>) -> Self {
t.value
}
}
}
}

#[cfg(test)]
mod tests {
storage_types! {
use num::{One, FromPrimitive};
use si::quantities::*;
use si::ratio as r;
use tests::Test;

#[test]
fn from() {
let v = V::one();
let r1: Ratio<V> = Ratio::<V>::from(v);
let r2: Ratio<V> = v.into();
let _: V = V::from(r1);
let _: V = r2.into();
}

#[test]
fn check_units() {
Test::assert_eq(&Ratio::new::<r::ratio>(V::one() / V::from_f64(100.0).unwrap()),
&Ratio::new::<r::part_per_hundred>(V::one()));
Test::assert_eq(&Ratio::new::<r::ratio>(V::one() / V::from_f64(100.0).unwrap()),
&Ratio::new::<r::percent>(V::one()));
Test::assert_eq(&Ratio::new::<r::ratio>(V::one() / V::from_f64(1000.0).unwrap()),
&Ratio::new::<r::part_per_thousand>(V::one()));
Test::assert_eq(&Ratio::new::<r::ratio>(V::one() / V::from_f64(1000.0).unwrap()),
&Ratio::new::<r::per_mille>(V::one()));
Test::assert_eq(&Ratio::new::<r::ratio>(V::one() / V::from_f64(10000.0).unwrap()),
&Ratio::new::<r::part_per_ten_thousand>(V::one()));
Test::assert_eq(&Ratio::new::<r::ratio>(V::one() / V::from_f64(10000.0).unwrap()),
&Ratio::new::<r::basis_point>(V::one()));
Test::assert_eq(&Ratio::new::<r::ratio>(V::one() / V::from_f64(1000000.0).unwrap()),
&Ratio::new::<r::part_per_million>(V::one()));
Test::assert_eq(&Ratio::new::<r::ratio>(V::one() / V::from_f64(1000000000.0).unwrap()),
&Ratio::new::<r::part_per_billion>(V::one()));
Test::assert_eq(&Ratio::new::<r::ratio>(V::one()
/ V::from_f64(1000000000000.0).unwrap()),
&Ratio::new::<r::part_per_trillion>(V::one()));
Test::assert_eq(&Ratio::new::<r::ratio>(V::one()
/ V::from_f64(1000000000000000.0).unwrap()),
&Ratio::new::<r::part_per_quadrillion>(V::one()));
}
}
}

0 comments on commit de55871

Please sign in to comment.