Skip to content

Commit 84c14b8

Browse files
committed
Merge branch 'dbrgn-ohms-law'
2 parents 96009bb + c2c1011 commit 84c14b8

File tree

3 files changed

+178
-2
lines changed

3 files changed

+178
-2
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[package]
22
name = "measurements"
3-
version = "0.7.0"
3+
version = "0.8.0"
44
authors = ["James O'Cull <[email protected]>",
55
"Jonathan Pallant <[email protected]>",
6-
"Hannah McLaughlin <[email protected]>"]
6+
"Hannah McLaughlin <[email protected]>",
7+
"Danilo Bargen <[email protected]>",
8+
]
79
documentation = "https://docs.rs/crate/measurements"
810
repository = "https://github.com/jocull/rust-measurements"
911
keywords = ["measurements", "lengths", "weights", "temperatures", "volumes"]

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub use energy::Energy;
4242
pub mod power;
4343
pub use power::Power;
4444

45+
pub mod voltage;
46+
pub use voltage::Voltage;
47+
4548
pub mod force;
4649
pub use force::Force;
4750

src/voltage.rs

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
//! Types and constants for handling voltage.
2+
3+
use super::measurement::*;
4+
5+
/// The `Voltage` struct can be used to deal with electric potential difference
6+
/// in a common way.
7+
///
8+
/// # Example
9+
///
10+
/// ```
11+
/// use measurements::Voltage;
12+
///
13+
/// let volts = Voltage::from_millivolts(1500.0);
14+
/// let m_v = volts.as_millivolts();
15+
/// let k_v = volts.as_kilovolts();
16+
/// println!("A 1.5 V battery has {} mV or {} kV", m_v, k_v);
17+
/// ```
18+
#[derive(Copy, Clone, Debug)]
19+
pub struct Voltage {
20+
volts: f64,
21+
}
22+
23+
impl Voltage {
24+
/// Create a new Voltage from a floating point value in Volts
25+
pub fn from_volts(volts: f64) -> Self {
26+
Voltage { volts }
27+
}
28+
29+
/// Create a new Voltage from a floating point value in Millivolts
30+
pub fn from_millivolts(millivolts: f64) -> Self {
31+
Self::from_volts(millivolts / 1000.0)
32+
}
33+
34+
/// Create a new Voltage from a floating point value in Kilovolts
35+
pub fn from_kilovolts(kilovolts: f64) -> Self {
36+
Self::from_volts(kilovolts * 1000.0)
37+
}
38+
39+
/// Convert this Voltage into a floating point value in Volts
40+
pub fn as_volts(&self) -> f64 {
41+
self.volts
42+
}
43+
44+
/// Convert this Voltage into a floating point value in Millivolts
45+
pub fn as_millivolts(&self) -> f64 {
46+
self.volts * 1000.0
47+
}
48+
49+
/// Convert this Voltage into a floating point value in Kilovolts
50+
pub fn as_kilovolts(&self) -> f64 {
51+
self.volts / 1000.0
52+
}
53+
}
54+
55+
impl Measurement for Voltage {
56+
fn as_base_units(&self) -> f64 {
57+
self.volts
58+
}
59+
60+
fn from_base_units(units: f64) -> Self {
61+
Self::from_volts(units)
62+
}
63+
64+
fn get_base_units_name(&self) -> &'static str {
65+
"V"
66+
}
67+
68+
fn get_appropriate_units(&self) -> (&'static str, f64) {
69+
// Smallest to Largest
70+
let list = [
71+
("fV", 1e-15),
72+
("pV", 1e-12),
73+
("nV", 1e-9),
74+
("\u{00B5}V", 1e-6),
75+
("mV", 1e-3),
76+
("V", 1e0),
77+
("kV", 1e3),
78+
("MV", 1e6),
79+
("GV", 1e9),
80+
("TV", 1e12),
81+
("PV", 1e15),
82+
("EV", 1e18),
83+
];
84+
self.pick_appropriate_units(&list)
85+
}
86+
}
87+
88+
implement_measurement! { Voltage }
89+
90+
#[cfg(test)]
91+
mod test {
92+
use voltage::*;
93+
use test_utils::assert_almost_eq;
94+
95+
#[test]
96+
pub fn as_kilovolts() {
97+
let u = Voltage::from_volts(10_000.0);
98+
assert_almost_eq(u.as_kilovolts(), 10.0);
99+
}
100+
101+
#[test]
102+
pub fn as_volts() {
103+
let u = Voltage::from_kilovolts(1.234);
104+
assert_almost_eq(u.as_volts(), 1234.0);
105+
}
106+
107+
#[test]
108+
pub fn as_millivolts() {
109+
let u = Voltage::from_volts(1.234);
110+
assert_almost_eq(u.as_millivolts(), 1234.0);
111+
}
112+
113+
// Traits
114+
#[test]
115+
fn add() {
116+
let a = Voltage::from_volts(2.0);
117+
let b = Voltage::from_millivolts(4000.0);
118+
let c = a + b;
119+
assert_almost_eq(c.as_volts(), 6.0);
120+
}
121+
122+
#[test]
123+
fn sub() {
124+
let a = Voltage::from_volts(2.0);
125+
let b = Voltage::from_millivolts(4000.0);
126+
let c = a - b;
127+
assert_almost_eq(c.as_volts(), -2.0);
128+
}
129+
130+
#[test]
131+
fn mul() {
132+
let a = Voltage::from_millivolts(2000.0);
133+
let b = 4.0 * a;
134+
assert_almost_eq(b.as_volts(), 8.0);
135+
}
136+
137+
#[test]
138+
fn div() {
139+
let a = Voltage::from_volts(2.0);
140+
let b = Voltage::from_millivolts(4000.0);
141+
let c = a / b;
142+
let d = a / 2.0;
143+
assert_almost_eq(c, 0.5);
144+
assert_almost_eq(d.as_volts(), 1.0);
145+
}
146+
147+
#[test]
148+
fn eq() {
149+
let a = Voltage::from_kilovolts(1.0);
150+
let b = Voltage::from_millivolts(1_000_000.0);
151+
assert_eq!(a, b);
152+
}
153+
154+
#[test]
155+
fn neq() {
156+
let a = Voltage::from_volts(2.0);
157+
let b = Voltage::from_millivolts(2.0);
158+
assert_ne!(a, b);
159+
}
160+
161+
#[test]
162+
fn cmp() {
163+
let a = Voltage::from_volts(2.0);
164+
let b = Voltage::from_volts(4.0);
165+
assert_eq!(a < b, true);
166+
assert_eq!(a <= b, true);
167+
assert_eq!(a > b, false);
168+
assert_eq!(a >= b, false);
169+
}
170+
171+
}

0 commit comments

Comments
 (0)