Skip to content

Commit

Permalink
physics: Add kinetic energy mod
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jul 28, 2023
1 parent 3700022 commit acaec83
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions physics/src/kinetic_energy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2023 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

//! Find the kinetic energy of an object, given its mass and velocity.
//!
//! [Kinetic energy](https://en.m.wikipedia.org/wiki/Kinetic_energy)

/// Calculate kinetic energy.
///
/// The kinetic energy of a non-rotating object of mass m traveling at a speed v is ½mv²
#[must_use]
pub fn kinetic_energy(mass: f64, velocity: f64) -> f64 {
debug_assert!(mass >= 0.0);
0.5 * mass * velocity.abs() * velocity.abs()
}

#[cfg(test)]
mod tests {
use super::kinetic_energy;

#[test]
fn test_kinetic_energy() {
const PAIRS: &[(f64, f64, f64)] = &[
(10.0, 10.0, 500.0),
(0.0, 10.0, 0.0),
(10.0, 0.0, 0.0),
(20.0, -20.0, 4000.0),
(0.0, 0.0, 0.0),
(2.0, 2.0, 4.0),
(100.0, 100.0, 500000.0),
];

for (mass, velocity, result) in PAIRS {
assert_eq!(kinetic_energy(*mass, *velocity), *result);
}
}
}
1 change: 1 addition & 0 deletions physics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
clippy::pedantic
)]

pub mod kinetic_energy;
pub mod potential_energy;
pub mod shear_stress;
pub mod speed_of_sound;

0 comments on commit acaec83

Please sign in to comment.