@@ -8,6 +8,10 @@ pub const WATT_HORSEPOWER_FACTOR: f64 = 1.0 / 745.6998715822702;
88pub const WATT_BTU_MIN_FACTOR : f64 = 1.0 / 17.58426666666667 ;
99/// Number of kW in a W
1010pub 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
1216pub 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
93117impl 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 ( ) {
0 commit comments