Skip to content

Commit 853e7ec

Browse files
committed
Add Resistance unit
1 parent a07b556 commit 853e7ec

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ pub use voltage::Voltage;
4848
pub mod current;
4949
pub use current::Current;
5050

51+
pub mod resistance;
52+
pub use resistance::Resistance;
53+
5154
pub mod force;
5255
pub use force::Force;
5356

src/resistance.rs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
//! Types and constants for handling electrical resistance.
2+
3+
use super::measurement::*;
4+
5+
/// The `Resistance` struct can be used to deal with electrical resistance in a
6+
/// common way.
7+
///
8+
/// # Example
9+
///
10+
/// ```
11+
/// use measurements::Resistance;
12+
///
13+
/// let r = Resistance::from_kiloohms(4.7);
14+
/// let o = r.as_ohms();
15+
/// let mo = r.as_megaohms();
16+
/// println!("A 4.7 kΩ resistor has {} Ω or {} MΩ", o, mo);
17+
/// ```
18+
#[derive(Copy, Clone, Debug)]
19+
pub struct Resistance {
20+
ohms: f64,
21+
}
22+
23+
impl Resistance {
24+
/// Create a new Resistance from a floating point value in ohms
25+
pub fn from_ohms(ohms: f64) -> Self {
26+
Resistance { ohms }
27+
}
28+
29+
/// Create a new Resistance from a floating point value in kiloohms
30+
pub fn from_kiloohms(kiloohms: f64) -> Self {
31+
Self::from_ohms(kiloohms * 1000.0)
32+
}
33+
34+
/// Create a new Resistance from a floating point value in milliohms
35+
pub fn from_megaohms(megaohms: f64) -> Self {
36+
Self::from_ohms(megaohms * 1000.0 * 1000.0)
37+
}
38+
39+
/// Convert this Resistance into a floating point value in ohms
40+
pub fn as_ohms(&self) -> f64 {
41+
self.ohms
42+
}
43+
44+
/// Convert this Resistance into a floating point value in kiloohms
45+
pub fn as_kiloohms(&self) -> f64 {
46+
self.ohms / 1000.0
47+
}
48+
49+
/// Convert this Resistance into a floating point value in milliohms
50+
pub fn as_megaohms(&self) -> f64 {
51+
self.ohms / 1000.0 / 1000.0
52+
}
53+
}
54+
55+
impl Measurement for Resistance {
56+
fn as_base_units(&self) -> f64 {
57+
self.ohms
58+
}
59+
60+
fn from_base_units(units: f64) -> Self {
61+
Self::from_ohms(units)
62+
}
63+
64+
fn get_base_units_name(&self) -> &'static str {
65+
"\u{2126}"
66+
}
67+
68+
fn get_appropriate_units(&self) -> (&'static str, f64) {
69+
// Smallest to Largest
70+
let list = [
71+
("f\u{2126}", 1e-15),
72+
("p\u{2126}", 1e-12),
73+
("n\u{2126}", 1e-9),
74+
("\u{00B5}\u{2126}", 1e-6),
75+
("m\u{2126}", 1e-3),
76+
("\u{2126}", 1e0),
77+
("k\u{2126}", 1e3),
78+
("M\u{2126}", 1e6),
79+
("G\u{2126}", 1e9),
80+
("T\u{2126}", 1e12),
81+
("P\u{2126}", 1e15),
82+
("E\u{2126}", 1e18),
83+
];
84+
self.pick_appropriate_units(&list)
85+
}
86+
}
87+
88+
implement_measurement! { Resistance }
89+
90+
#[cfg(test)]
91+
mod test {
92+
use resistance::*;
93+
use test_utils::assert_almost_eq;
94+
95+
#[test]
96+
pub fn as_ohms() {
97+
let u = Resistance::from_kiloohms(1.234);
98+
assert_almost_eq(u.as_ohms(), 1234.0);
99+
}
100+
101+
#[test]
102+
pub fn as_kiloohms() {
103+
let u = Resistance::from_ohms(10_000.0);
104+
assert_almost_eq(u.as_kiloohms(), 10.0);
105+
}
106+
107+
#[test]
108+
pub fn as_megaohms() {
109+
let u = Resistance::from_ohms(1_234_567.0);
110+
assert_almost_eq(u.as_megaohms(), 1.234567);
111+
}
112+
113+
// Traits
114+
#[test]
115+
fn add() {
116+
let a = Resistance::from_ohms(2000.0);
117+
let b = Resistance::from_kiloohms(4.0);
118+
let c = a + b;
119+
assert_almost_eq(c.as_kiloohms(), 6.0);
120+
}
121+
122+
#[test]
123+
fn sub() {
124+
let a = Resistance::from_megaohms(2.0);
125+
let b = Resistance::from_kiloohms(4000.0);
126+
let c = a - b;
127+
assert_almost_eq(c.as_ohms(), -2_000_000.0);
128+
}
129+
130+
#[test]
131+
fn mul() {
132+
let a = Resistance::from_ohms(2000.0);
133+
let b = 4.0 * a;
134+
assert_almost_eq(b.as_kiloohms(), 8.0);
135+
}
136+
137+
#[test]
138+
fn div() {
139+
let a = Resistance::from_kiloohms(2.0);
140+
let b = Resistance::from_ohms(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_ohms(), 1000.0);
145+
}
146+
147+
#[test]
148+
fn eq() {
149+
let a = Resistance::from_ohms(1_000_000.0);
150+
let b = Resistance::from_megaohms(1.0);
151+
assert_eq!(a, b);
152+
}
153+
154+
#[test]
155+
fn neq() {
156+
let a = Resistance::from_ohms(2.0);
157+
let b = Resistance::from_megaohms(2.0);
158+
assert_ne!(a, b);
159+
}
160+
161+
#[test]
162+
fn cmp() {
163+
let a = Resistance::from_ohms(2.0);
164+
let b = Resistance::from_ohms(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+
}

0 commit comments

Comments
 (0)