Skip to content

Commit 74cba4f

Browse files
author
Daniel Egger
committed
Extend range of electrical units downwards
Signed-off-by: Daniel Egger <[email protected]>
1 parent 39e2429 commit 74cba4f

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

src/current.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ impl Current {
3636
Self::from_amperes(microamperes / 1000.0 / 1000.0)
3737
}
3838

39+
/// Create a new Current from a floating point value in nanoamperes
40+
pub fn from_nanoamperes(nanoamperes: f64) -> Self {
41+
Self::from_amperes(nanoamperes / 1_000_000_000.0)
42+
}
43+
3944
/// Convert this Current into a floating point value in amperes
4045
pub fn as_amperes(&self) -> f64 {
4146
self.amperes
@@ -50,6 +55,11 @@ impl Current {
5055
pub fn as_microamperes(&self) -> f64 {
5156
self.amperes * 1000.0 * 1000.0
5257
}
58+
59+
/// Convert this Current into a floating point value in nanoamperes
60+
pub fn as_nanoamperes(&self) -> f64 {
61+
self.amperes * 1_000_000_000.0
62+
}
5363
}
5464

5565
impl Measurement for Current {
@@ -110,6 +120,12 @@ mod test {
110120
assert_almost_eq(u.as_microamperes(), 1000.0);
111121
}
112122

123+
#[test]
124+
pub fn as_nanoamperes() {
125+
let u = Current::from_amperes(0.000001);
126+
assert_almost_eq(u.as_nanoamperes(), 1000.0);
127+
}
128+
113129
// Traits
114130
#[test]
115131
fn add() {
@@ -148,7 +164,10 @@ mod test {
148164
fn eq() {
149165
let a = Current::from_microamperes(1_000_000.0);
150166
let b = Current::from_milliamperes(1_000.0);
167+
let c = Current::from_nanoamperes(1_000_000_000.0);
151168
assert_eq!(a, b);
169+
assert_eq!(b, c);
170+
assert_eq!(a, c);
152171
}
153172

154173
#[test]

src/power.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ pub const WATT_HORSEPOWER_FACTOR: f64 = 1.0 / 745.6998715822702;
88
pub const WATT_BTU_MIN_FACTOR: f64 = 1.0 / 17.58426666666667;
99
/// Number of kW in a W
1010
pub const WATT_KILOWATT_FACTOR: f64 = 1e-3;
11+
/// Number of mW in a W
12+
pub const WATT_MILLIWATT_FACTOR: f64 = 1e3;
13+
/// Number of µW in a W
14+
pub const WATT_MICROWATT_FACTOR: f64 = 1e6;
1115
/// Number of pferdstarken (PS) in a W
1216
pub const WATT_PS_FACTOR: f64 = 1.0 / 735.499;
1317

@@ -34,6 +38,16 @@ impl Power {
3438
Power { watts: watts }
3539
}
3640

41+
/// Create a new Power from a floating point value in milliwatts
42+
pub fn from_milliwatts(milliwatts: f64) -> Power {
43+
Self::from_watts(milliwatts / WATT_MILLIWATT_FACTOR)
44+
}
45+
46+
/// Create a new Power from a floating point value in microwatts
47+
pub fn from_microwatts(microwatts: f64) -> Power {
48+
Self::from_watts(microwatts / WATT_MICROWATT_FACTOR)
49+
}
50+
3751
/// Create a new Power from a floating point value in horsepower (hp)
3852
pub fn from_horsepower(horsepower: f64) -> Power {
3953
Self::from_watts(horsepower / WATT_HORSEPOWER_FACTOR)
@@ -88,6 +102,16 @@ impl Power {
88102
pub fn as_kilowatts(&self) -> f64 {
89103
self.watts * WATT_KILOWATT_FACTOR
90104
}
105+
106+
/// Convert this Power into a floating point value in milliwatts (mW)
107+
pub fn as_milliwatts(&self) -> f64 {
108+
self.watts * WATT_MILLIWATT_FACTOR
109+
}
110+
111+
/// Convert this Power into a floating point value in microwatts (µW)
112+
pub fn as_microwatts(&self) -> f64 {
113+
self.watts * WATT_MICROWATT_FACTOR
114+
}
91115
}
92116

93117
impl Measurement for Power {
@@ -168,6 +192,30 @@ mod test {
168192
assert_almost_eq(r2, 0.1);
169193
}
170194

195+
#[test]
196+
pub fn as_milliwatts() {
197+
let i1 = Power::from_milliwatts(100.0);
198+
let r1 = i1.as_watts();
199+
200+
let i2 = Power::from_watts(100.0);
201+
let r2 = i2.as_milliwatts();
202+
203+
assert_almost_eq(r1, 0.1);
204+
assert_almost_eq(r2, 100_000.0);
205+
}
206+
207+
#[test]
208+
pub fn as_microwatts() {
209+
let i1 = Power::from_microwatts(100.0);
210+
let r1 = i1.as_milliwatts();
211+
212+
let i2 = Power::from_milliwatts(100.0);
213+
let r2 = i2.as_microwatts();
214+
215+
assert_almost_eq(r1, 0.1);
216+
assert_almost_eq(r2, 100_000.0);
217+
}
218+
171219
// Traits
172220
#[test]
173221
fn add() {

src/voltage.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ impl Voltage {
2626
Voltage { volts }
2727
}
2828

29+
/// Create a new Voltage from a floating point value in Microvolts
30+
pub fn from_microvolts(microvolts: f64) -> Self {
31+
Self::from_volts(microvolts / 1_000_000.0)
32+
}
33+
2934
/// Create a new Voltage from a floating point value in Millivolts
3035
pub fn from_millivolts(millivolts: f64) -> Self {
3136
Self::from_volts(millivolts / 1000.0)
@@ -41,6 +46,11 @@ impl Voltage {
4146
self.volts
4247
}
4348

49+
/// Convert this Voltage into a floating point value in Microvolts
50+
pub fn as_microvolts(&self) -> f64 {
51+
self.volts * 1_000_000.0
52+
}
53+
4454
/// Convert this Voltage into a floating point value in Millivolts
4555
pub fn as_millivolts(&self) -> f64 {
4656
self.volts * 1000.0
@@ -106,6 +116,12 @@ mod test {
106116
assert_almost_eq(u.as_volts(), 1234.0);
107117
}
108118

119+
#[test]
120+
pub fn as_microvolts() {
121+
let u = Voltage::from_volts(1.234567);
122+
assert_almost_eq(u.as_microvolts(), 1234567.0);
123+
}
124+
109125
#[test]
110126
pub fn as_millivolts() {
111127
let u = Voltage::from_volts(1.234);
@@ -150,7 +166,10 @@ mod test {
150166
fn eq() {
151167
let a = Voltage::from_kilovolts(1.0);
152168
let b = Voltage::from_millivolts(1_000_000.0);
169+
let c = Voltage::from_microvolts(1_000_000_000.0);
153170
assert_eq!(a, b);
171+
assert_eq!(a, c);
172+
assert_eq!(b, c);
154173
}
155174

156175
#[test]

0 commit comments

Comments
 (0)