Skip to content

Commit 67e9b31

Browse files
authored
Merge pull request #62 from trappitsch/add_mmHg_as_pressure_unit
add mmHg in pressure units
2 parents c10b8b2 + 0257ee8 commit 67e9b31

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/pressure.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub const PASCAL_PSI_FACTOR: f64 = 6894.76;
1818
pub const PASCAL_TORR_FACTOR: f64 = PASCAL_ATMOSPHERE_FACTOR / 760.0;
1919
/// Number of Pascals in a millitorr
2020
pub const PASCAL_MILLITORR_FACTOR: f64 = PASCAL_TORR_FACTOR / 1000.0;
21+
/// Number of Pascals in mmHg
22+
pub const PASCAL_MMHG_FACTOR: f64 = 133.322387415;
2123

2224
/// The `Pressure` struct can be used to deal with pressures in a common way.
2325
/// Common metric and imperial units are supported.
@@ -73,17 +75,21 @@ impl Pressure {
7375
Self::from_pascals(atmospheres * PASCAL_ATMOSPHERE_FACTOR)
7476
}
7577

76-
/// Create new Pressure from floating point value in Torr (also often referred to as mmHg)
78+
/// Create new Pressure from floating point value in Torr
7779
pub fn from_torrs(torrs: f64) -> Pressure {
7880
Self::from_pascals(torrs * PASCAL_TORR_FACTOR)
7981
}
8082

81-
/// Create new Pressure from floating point value in millitorr (mTorr, also often referred to
82-
/// as microns for µmHg)
83+
/// Create new Pressure from floating point value in millitorr (mTorr)
8384
pub fn from_millitorrs(millitorrs: f64) -> Pressure {
8485
Self::from_pascals(millitorrs * PASCAL_MILLITORR_FACTOR)
8586
}
8687

88+
/// Create new Pressure from floating point value in millimeter of mercury (mmHg)
89+
pub fn from_millimeter_mercury(mmhg: f64) -> Pressure {
90+
Self::from_pascals(mmhg * PASCAL_MMHG_FACTOR)
91+
}
92+
8793
/// Convert this Pressure into a floating point value in Pascals
8894
pub fn as_pascals(&self) -> f64 {
8995
self.pascals
@@ -119,16 +125,20 @@ impl Pressure {
119125
self.pascals / PASCAL_ATMOSPHERE_FACTOR
120126
}
121127

122-
/// Convert this Pressure into a floating point value in Torr (also often referred to as mmHg)
128+
/// Convert this Pressure into a floating point value in Torr
123129
pub fn as_torrs(&self) -> f64 {
124130
self.pascals / PASCAL_TORR_FACTOR
125131
}
126132

127-
/// Convert this Pressure into a floating point value in millitorr (mTorr, also often referred
128-
/// to as microns for µmHg)
133+
/// Convert this Pressure into a floating point value in millitorr (mTorr)
129134
pub fn as_millitorrs(&self) -> f64 {
130135
self.pascals / PASCAL_MILLITORR_FACTOR
131136
}
137+
138+
/// Convert this Pressure into a floating point value in millimeter of mercury (mmHg)
139+
pub fn as_millimeter_mercury(&self) -> f64 {
140+
self.pascals / PASCAL_MMHG_FACTOR
141+
}
132142
}
133143

134144
impl Measurement for Pressure {
@@ -249,6 +259,17 @@ mod test {
249259
assert_almost_eq(o, 13.33224);
250260
}
251261

262+
#[test]
263+
fn mmhg() {
264+
let t = Pressure::from_pascals(100.0);
265+
let o = t.as_millimeter_mercury();
266+
assert_almost_eq(o, 0.7500615758);
267+
268+
let t = Pressure::from_millimeter_mercury(100.0);
269+
let o = t.as_pascals();
270+
assert_almost_eq(o, 13332.2387415);
271+
}
272+
252273
// Traits
253274
#[test]
254275
fn add() {

0 commit comments

Comments
 (0)