const PASCALS_PER_PSI: f32 = 6894.7573;
pub fn celsius_to_fahrenheit(celsius: f32) -> f32 {
celsius * 9.0 / 5.0 + 32.0
}
pub fn fahrenheit_to_celsius(fahrenheit: f32) -> f32 {
(fahrenheit - 32.0) * 5.0 / 9.0
}
pub fn celsius_to_kelvin(celsius: f32) -> f32 {
celsius + 273.15
}
pub fn kelvin_to_celsius(kelvin: f32) -> f32 {
kelvin - 273.15
}
pub fn pascals_to_hectopascals(pascals: f32) -> f32 {
pascals / 100.0
}
pub fn hectopascals_to_pascals(hectopascals: f32) -> f32 {
hectopascals * 100.0
}
pub fn pascals_to_kilopascals(pascals: f32) -> f32 {
pascals / 1000.0
}
pub fn kilopascals_to_pascals(kilopascals: f32) -> f32 {
kilopascals * 1000.0
}
pub fn pascals_to_psi(pascals: f32) -> f32 {
pascals / PASCALS_PER_PSI
}
pub fn psi_to_pascals(psi: f32) -> f32 {
psi * PASCALS_PER_PSI
}
pub fn ratio_to_percent(ratio: f32) -> f32 {
ratio * 100.0
}
pub fn percent_to_ratio(percent: f32) -> f32 {
percent / 100.0
}
#[cfg(test)]
mod tests {
use super::*;
fn approx(a: f32, b: f32, tol: f32) -> bool {
(a - b).abs() < tol
}
#[test]
fn temperature_reference_points() {
assert!(approx(celsius_to_fahrenheit(100.0), 212.0, 1e-3));
assert!(approx(celsius_to_fahrenheit(0.0), 32.0, 1e-3));
assert!(approx(celsius_to_fahrenheit(37.0), 98.6, 1e-2));
assert!(approx(celsius_to_fahrenheit(-40.0), -40.0, 1e-3));
assert!(approx(fahrenheit_to_celsius(212.0), 100.0, 1e-3));
assert!(approx(celsius_to_kelvin(0.0), 273.15, 1e-2));
assert!(approx(kelvin_to_celsius(273.15), 0.0, 1e-2));
}
#[test]
fn temperature_round_trips() {
assert!(approx(
fahrenheit_to_celsius(celsius_to_fahrenheit(21.0)),
21.0,
1e-3
));
assert!(approx(
kelvin_to_celsius(celsius_to_kelvin(21.0)),
21.0,
1e-3
));
}
#[test]
fn pressure_reference_points() {
assert!(approx(pascals_to_hectopascals(101325.0), 1013.25, 1e-1));
assert!(approx(pascals_to_kilopascals(101325.0), 101.325, 1e-2));
assert!(approx(pascals_to_psi(101325.0), 14.6959, 1e-2));
assert!(approx(hectopascals_to_pascals(1013.25), 101325.0, 1.0));
assert!(approx(psi_to_pascals(1.0), 6894.7573, 1e-1));
}
#[test]
fn percent_helpers() {
assert!(approx(ratio_to_percent(0.25), 25.0, 1e-4));
assert!(approx(percent_to_ratio(25.0), 0.25, 1e-4));
assert!(approx(percent_to_ratio(ratio_to_percent(0.6)), 0.6, 1e-4));
}
}