use libm::log;
const MAGNUS_B: f64 = 17.62;
const MAGNUS_C: f64 = 243.12;
pub fn dew_point(celsius: f64, humidity_percent: f64) -> f64 {
let humidity = if humidity_percent <= 0.0 {
0.0001
} else {
humidity_percent
};
let gamma = log(humidity / 100.0) + MAGNUS_B * celsius / (MAGNUS_C + celsius);
MAGNUS_C * gamma / (MAGNUS_B - gamma)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn matches_a_worked_example() {
assert!((dew_point(20.0, 50.0) - 9.3).abs() < 0.2);
}
#[test]
fn saturated_air_dews_at_the_temperature() {
assert!((dew_point(15.0, 100.0) - 15.0).abs() < 1e-6);
assert!((dew_point(-3.0, 100.0) + 3.0).abs() < 1e-6);
}
#[test]
fn lower_humidity_means_a_lower_dew_point() {
let humid = dew_point(25.0, 80.0);
let dry = dew_point(25.0, 30.0);
assert!(dry < humid);
}
#[test]
fn a_frost_risk_shows_as_a_dew_point_below_zero() {
assert!(dew_point(2.0, 60.0) < 0.0);
}
}