1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
quantity! {
quantity: Ratio; "ratio";
dimension: ISQ<
Z0,
Z0,
Z0,
Z0,
Z0,
Z0,
Z0>;
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";
@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 r1: Ratio<V> = Ratio::<V>::from(V::one());
let r2: Ratio<V> = V::one().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()));
}
}
}