Skip to main content

use_chemical_constants/
lib.rs

1#![forbid(unsafe_code)]
2
3//! Reusable chemical and thermodynamic constants expressed as plain `f64` values.
4
5/// Derived SI constant for the molar gas constant, represented here as a rounded `f64` in joules per mole kelvin.
6pub const GAS_CONSTANT: f64 = 8.314_462_618_153_24;
7
8/// Derived SI constant for the Faraday constant, represented here as a rounded `f64` in coulombs per mole.
9pub const FARADAY_CONSTANT: f64 = 96_485.332_123_310_018;
10
11/// Conventional standard atmosphere, in pascals.
12pub const STANDARD_ATMOSPHERE: f64 = 101_325.0;
13
14/// Conventional standard temperature, in kelvin.
15pub const STANDARD_TEMPERATURE_KELVIN: f64 = 273.15;
16
17/// Conventional standard-state pressure, in pascals.
18pub const STANDARD_PRESSURE_PASCAL: f64 = 100_000.0;
19
20#[cfg(test)]
21mod tests {
22    use super::{FARADAY_CONSTANT, GAS_CONSTANT, STANDARD_ATMOSPHERE, STANDARD_PRESSURE_PASCAL};
23
24    const AVOGADRO_CONSTANT: f64 = 6.022_140_76e23;
25    const BOLTZMANN_CONSTANT: f64 = 1.380_649e-23;
26    const ELEMENTARY_CHARGE: f64 = 1.602_176_634e-19;
27
28    fn approx_eq(left: f64, right: f64, relative_tolerance: f64) {
29        let scale = left.abs().max(right.abs()).max(1.0);
30        let delta = (left - right).abs();
31
32        assert!(
33            delta <= relative_tolerance * scale,
34            "left={left:e} right={right:e} delta={delta:e} rel_tol={relative_tolerance:e}"
35        );
36    }
37
38    #[test]
39    fn faraday_matches_avogadro_times_charge() {
40        approx_eq(
41            FARADAY_CONSTANT,
42            AVOGADRO_CONSTANT * ELEMENTARY_CHARGE,
43            1.0e-12,
44        );
45    }
46
47    #[test]
48    fn gas_constant_matches_avogadro_times_boltzmann() {
49        approx_eq(
50            GAS_CONSTANT,
51            AVOGADRO_CONSTANT * BOLTZMANN_CONSTANT,
52            1.0e-12,
53        );
54    }
55
56    #[test]
57    fn conventional_reference_pressures_are_positive() {
58        assert!(STANDARD_ATMOSPHERE > STANDARD_PRESSURE_PASCAL);
59        assert!(STANDARD_PRESSURE_PASCAL > 0.0);
60    }
61}