Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logarithmic units #229

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ pub enum ConstantOp {
///
/// [units]: http://jcgm.bipm.org/vim/en/1.13.html
/// [factor]: https://jcgm.bipm.org/vim/en/1.24.html
pub trait Conversion<V> {
pub trait Conversion<V> where
V: Conversion<V, T = Self::T> {
/// Conversion factor type specific to the underlying storage type.
type T: ConversionFactor<V>;

Expand All @@ -420,6 +421,28 @@ pub trait Conversion<V> {
<Self::T as crate::num::Zero>::zero()
}

#[inline(always)]
#[allow(unused_variables)]
fn base() -> Self::T {
<Self::T as crate::num::One>::one()
}

#[inline(always)]
#[allow(unused_variables)]
fn scale() -> Self::T {
<Self::T as crate::num::One>::one()
}

#[inline(always)]
fn into_linear(x: V) -> V {
x
}

#[inline(always)]
fn from_linear(x: V) -> V {
x
}

/// Instance [conversion factor](https://jcgm.bipm.org/vim/en/1.24.html).
///
/// Default implementation returns the coefficient: `Self::coefficient()`.
Expand Down Expand Up @@ -449,6 +472,16 @@ pub trait ConversionFactor<V>:
/// Raises a `ConversionFactor<V>` to an integer power.
fn powi(self, e: i32) -> Self;

/// Raises a `ConversionFactor<V>` to a power.
fn pow(self, v: V) -> V {
unimplemented!()
}

/// Takes the log_`ConversionFactor<V>` of a value.
fn log(self, v: V) -> V {
unimplemented!()
}

/// Converts a `ConversionFactor<V>` into its underlying storage type.
fn value(self) -> V;
}
Expand Down Expand Up @@ -503,6 +536,15 @@ storage_types! {
fn value(self) -> V {
self
}

fn pow(self, v: V) -> V {
<V as crate::num::Float>::powf(self, v)
}

/// Takes the log_`ConversionFactor<V>` of a value.
fn log(self, v: V) -> V {
<V as crate::num::Float>::log(self, v)
}
}
}

Expand Down
48 changes: 46 additions & 2 deletions src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ macro_rules! quantity {
fn constant(op: $crate::ConstantOp) -> Self::T {
quantity!(@constant op $($conversion),+)
}

#[inline(always)]
#[allow(unused_variables)]
fn base() -> Self::T {
quantity!(@base $($conversion),+)
}

#[inline(always)]
#[allow(unused_variables)]
fn scale() -> Self::T {
quantity!(@scale $($conversion),+)
}

quantity!(@logarithmic $($conversion),+);
}

impl super::Conversion<V> for super::$unit {})+
Expand Down Expand Up @@ -356,7 +370,7 @@ macro_rules! quantity {
$quantity {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: super::to_base::<Dimension, U, V, N>(&v),
value: super::to_base::<Dimension, U, V, N>(&<N>::into_linear(v)),
}
}

Expand All @@ -369,7 +383,7 @@ macro_rules! quantity {
where
N: Unit + $crate::Conversion<V, T = V::T>,
{
super::from_base::<Dimension, U, V, N>(&self.value)
<N>::from_linear(super::from_base::<Dimension, U, V, N>(&self.value))
}

/// Returns the largest integer less than or equal to a number in the given
Expand Down Expand Up @@ -589,12 +603,42 @@ macro_rules! quantity {
pub struct $unit;
};
(@coefficient $factor:expr, $const:expr) => { $factor };
(@coefficient $factor:expr, $base:expr, $scale:expr) => { $factor };
(@coefficient $factor:expr) => { $factor };
(@constant $op:ident $factor:expr, $const:expr) => { $const };
(@constant $op:ident $factor:expr, $base:expr, $scale:expr) => {
match $op {
$crate::ConstantOp::Add => -0.0,
$crate::ConstantOp::Sub => 0.0,
}
};
(@constant $op:ident $factor:expr) => {
match $op {
$crate::ConstantOp::Add => -0.0,
$crate::ConstantOp::Sub => 0.0,
}
};
(@base $factor:expr, $base:expr, $scale:expr) => { $base };
(@base $factor:expr, $const:expr) => { 1.0 };
(@base $factor:expr) => { 1.0 };
(@scale $factor:expr, $base:expr, $scale:expr) => { $scale };
(@scale $factor:expr, $const:expr) => { 1.0 };
(@scale $factor:expr) => { 1.0 };
(@logarithmic $factor:expr, $base:expr, $scale:expr) => {
#[inline(always)]
fn into_linear(x: V) -> V {
use $crate::ConversionFactor;
<Self as $crate::Conversion<V>>::base().pow((
(<<Self as $crate::Conversion<V>>::T as $crate::num::One>::one() / <Self as $crate::Conversion<V>>::scale()) * x.into_conversion()
).value())
}

#[inline(always)]
fn from_linear(x: V) -> V {
use $crate::ConversionFactor;
(<Self as $crate::Conversion<V>>::scale() * x.into_conversion().log(<Self as $crate::Conversion<V>>::base().value()).into_conversion()).value()
}
};
(@logarithmic $factor:expr, $const:expr) => { };
(@logarithmic $factor:expr) => { };
}
5 changes: 5 additions & 0 deletions src/si/electric_potential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ quantity! {

@abvolt: 1.0_E-8; "abV", "abvolt", "abvolts";
@statvolt: 2.997_925_E2; "statV", "statvolt", "statvolts";

@decibel_volt: prefix!(none), 10.0, 20.0; "dBV", "decibel-volt", "decibel-volts";
@decibel_millivolt: prefix!(milli), 10.0, 20.0; "dBmV", "decibel-millivolt", "decibel-millivolts";
@decibel_microvolt: prefix!(micro), 10.0, 20.0; "dBµV", "decibel-microvolt", "decibel-microvolts";
@decibel_unit: 0.7746E0, 10.0, 20.0; "dBu", "decibel-unit", "decibel-units";
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/si/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ quantity! {
@zeptowatt: prefix!(zepto); "zW", "zeptowatt", "zeptowatts";
@yoctowatt: prefix!(yocto); "yW", "yoctowatt", "yoctowatts";

@decibel_watt: prefix!(none), 10.0, 10.0; "dBW", "decibel-watt", "decibel-watts";
@decibel_milliwatt: prefix!(milli), 10.0, 10.0; "dBm", "decibel-milliwatt", "decibel-milliwatts";// dBm is more common than dBmW
@decibel_microwatt: prefix!(micro), 10.0, 10.0; "dBµW", "decibel-microwatt", "decibel-microwatts";

@erg_per_second: 1.0_E-7; "erg/s", "erg per second", "ergs per second";
@foot_pound_per_hour: 3.766_161_111_111_111_E-4; "ft · lbf/h", "foot pound-force per hour",
"foot pounds-force per hour";
Expand All @@ -58,6 +62,18 @@ quantity! {

#[cfg(test)]
mod tests {

#[test]
fn test_dbm() {
use crate::si::power as p;
use crate::si::quantities::*;
use crate::tests::Test;

let x = Power::new::<p::decibel_milliwatt>(0.0);
println!("{:?}", x.get::<p::watt>());
println!("{:?}", x.get::<p::decibel_watt>());
}

storage_types! {
use crate::num::One;
use crate::si::energy as e;
Expand Down
2 changes: 2 additions & 0 deletions src/si/ratio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ quantity! {
@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";

@decibel: 1.0, 10.0, 20.0; "dB", "decibel", "decibels";
}
}

Expand Down