Skip to main content

use_force/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4//! Force, weight, and impulse helpers.
5
6pub mod prelude;
7
8pub const STANDARD_GRAVITY: f64 = 9.806_65;
9
10#[must_use]
11pub const fn force(mass: f64, acceleration: f64) -> f64 {
12    mass * acceleration
13}
14
15#[must_use]
16pub const fn weight(mass: f64, gravity: f64) -> f64 {
17    force(mass, gravity)
18}
19
20#[must_use]
21pub const fn earth_weight(mass: f64) -> f64 {
22    weight(mass, STANDARD_GRAVITY)
23}
24
25#[must_use]
26pub const fn impulse(mass: f64, initial_velocity: f64, final_velocity: f64) -> f64 {
27    mass * (final_velocity - initial_velocity)
28}
29
30#[cfg(test)]
31#[allow(clippy::float_cmp)]
32mod tests {
33    use super::{STANDARD_GRAVITY, earth_weight, force, impulse, weight};
34
35    #[test]
36    fn force_helpers_cover_common_relationships() {
37        assert_eq!(force(10.0, 2.0), 20.0);
38        assert_eq!(weight(2.0, 10.0), 20.0);
39        assert_eq!(impulse(2.0, 1.0, 4.0), 6.0);
40        assert_eq!(earth_weight(1.0), STANDARD_GRAVITY);
41    }
42}