@@ -8,6 +8,10 @@ pub const WATT_HORSEPOWER_FACTOR: f64 = 1.0 / 745.6998715822702;
8
8
pub const WATT_BTU_MIN_FACTOR : f64 = 1.0 / 17.58426666666667 ;
9
9
/// Number of kW in a W
10
10
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 ;
11
15
/// Number of pferdstarken (PS) in a W
12
16
pub const WATT_PS_FACTOR : f64 = 1.0 / 735.499 ;
13
17
@@ -34,6 +38,16 @@ impl Power {
34
38
Power { watts : watts }
35
39
}
36
40
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
+
37
51
/// Create a new Power from a floating point value in horsepower (hp)
38
52
pub fn from_horsepower ( horsepower : f64 ) -> Power {
39
53
Self :: from_watts ( horsepower / WATT_HORSEPOWER_FACTOR )
@@ -88,6 +102,16 @@ impl Power {
88
102
pub fn as_kilowatts ( & self ) -> f64 {
89
103
self . watts * WATT_KILOWATT_FACTOR
90
104
}
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
+ }
91
115
}
92
116
93
117
impl Measurement for Power {
@@ -168,6 +192,30 @@ mod test {
168
192
assert_almost_eq ( r2, 0.1 ) ;
169
193
}
170
194
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
+
171
219
// Traits
172
220
#[ test]
173
221
fn add ( ) {
0 commit comments