Skip to main content

use_energy/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4//! Work and mechanical energy helpers.
5
6pub mod prelude;
7
8#[must_use]
9pub const fn kinetic_energy(mass: f64, velocity: f64) -> f64 {
10    0.5 * mass * velocity * velocity
11}
12
13#[must_use]
14pub const fn potential_energy(mass: f64, gravity: f64, height: f64) -> f64 {
15    mass * gravity * height
16}
17
18#[must_use]
19pub const fn work(force: f64, displacement: f64) -> f64 {
20    force * displacement
21}
22
23#[cfg(test)]
24#[allow(clippy::float_cmp)]
25mod tests {
26    use super::{kinetic_energy, potential_energy, work};
27
28    #[test]
29    fn energy_helpers_cover_common_calculations() {
30        assert_eq!(kinetic_energy(2.0, 3.0), 9.0);
31        assert_eq!(potential_energy(2.0, 10.0, 3.0), 60.0);
32        assert_eq!(work(5.0, 10.0), 50.0);
33    }
34}