Skip to main content

use_physical_constants/
lib.rs

1#![forbid(unsafe_code)]
2
3//! Reusable physical constants expressed as plain `f64` values.
4
5/// Exact SI-defined speed of light in vacuum, in meters per second.
6pub const SPEED_OF_LIGHT: f64 = 299_792_458.0;
7
8/// Exact SI-defined Planck constant, in joule seconds.
9pub const PLANCK_CONSTANT: f64 = 6.626_070_15e-34;
10
11/// Derived physical constant h-bar, represented here as a rounded `f64` in joule seconds.
12pub const REDUCED_PLANCK_CONSTANT: f64 = 1.054_571_817e-34;
13
14/// Exact SI-defined elementary charge, in coulombs.
15pub const ELEMENTARY_CHARGE: f64 = 1.602_176_634e-19;
16
17/// Exact SI-defined Boltzmann constant, in joules per kelvin.
18pub const BOLTZMANN_CONSTANT: f64 = 1.380_649e-23;
19
20/// Exact SI-defined Avogadro constant, in reciprocal moles.
21pub const AVOGADRO_CONSTANT: f64 = 6.022_140_76e23;
22
23/// Measured fine-structure constant, dimensionless.
24pub const FINE_STRUCTURE_CONSTANT: f64 = 7.297_352_564_3e-3;
25
26/// Measured Newtonian constant of gravitation, in cubic meters per kilogram second squared.
27pub const GRAVITATIONAL_CONSTANT: f64 = 6.674_30e-11;
28
29/// Measured vacuum permittivity, in farads per meter.
30pub const VACUUM_PERMITTIVITY: f64 = 8.854_187_812_8e-12;
31
32/// Measured vacuum permeability, in newtons per ampere squared.
33pub const VACUUM_PERMEABILITY: f64 = 1.256_637_062_12e-6;
34
35/// Derived radiative constant sigma, represented here as a rounded `f64` in watts per square meter kelvin to the fourth.
36pub const STEFAN_BOLTZMANN_CONSTANT: f64 = 5.670_374_419e-8;
37
38#[cfg(test)]
39mod tests {
40    use core::f64::consts::TAU;
41
42    use super::{
43        PLANCK_CONSTANT, REDUCED_PLANCK_CONSTANT, SPEED_OF_LIGHT, STEFAN_BOLTZMANN_CONSTANT,
44    };
45
46    fn runtime(value: f64) -> f64 {
47        value
48    }
49
50    fn approx_eq(left: f64, right: f64, relative_tolerance: f64) {
51        let scale = left.abs().max(right.abs()).max(1.0);
52        let delta = (left - right).abs();
53
54        assert!(
55            delta <= relative_tolerance * scale,
56            "left={left:e} right={right:e} delta={delta:e} rel_tol={relative_tolerance:e}"
57        );
58    }
59
60    #[test]
61    fn reduced_planck_matches_h_over_tau() {
62        approx_eq(REDUCED_PLANCK_CONSTANT, PLANCK_CONSTANT / TAU, 1.0e-9);
63    }
64
65    #[test]
66    fn representative_constants_are_positive() {
67        assert!(runtime(SPEED_OF_LIGHT) > 0.0);
68        assert!(runtime(PLANCK_CONSTANT) > 0.0);
69        assert!(runtime(STEFAN_BOLTZMANN_CONSTANT) > 0.0);
70    }
71}