From 8c08349b049557174b6de9f111374c23150a56c5 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 13 Jan 2020 11:25:15 -0500 Subject: [PATCH] Make output_Energy safe --- src/nbody-2.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/nbody-2.rs b/src/nbody-2.rs index 35335de..f66ca9a 100644 --- a/src/nbody-2.rs +++ b/src/nbody-2.rs @@ -102,7 +102,7 @@ fn offset_Momentum(bodies: &mut [body; BODIES_COUNT]) { } // Output the total energy of the system. -unsafe fn output_Energy(bodies: &mut [body; BODIES_COUNT]) { +fn output_Energy(bodies: &mut [body; BODIES_COUNT]) { let mut energy = 0.; for i in 0..BODIES_COUNT { // Add the kinetic energy for each body. @@ -115,21 +115,18 @@ unsafe fn output_Energy(bodies: &mut [body; BODIES_COUNT]) { // Add the potential energy between this body and // every other body for j in i + 1..BODIES_COUNT { - let mut position_Delta = [mem::MaybeUninit::::uninit(); 3]; + let mut position_Delta = [0.; 3]; for m in 0..3 { - position_Delta[m] - .as_mut_ptr() - .write(bodies[i].position[m] - bodies[j].position[m]); + position_Delta[m] = + bodies[i].position[m] - bodies[j].position[m]; } let position_Delta: [f64; 3] = mem::transmute(position_Delta); - energy -= bodies[i].mass * bodies[j].mass - / f64::sqrt( + energy -= bodies[i].mass * bodies[j].mass / f64::sqrt( position_Delta[0] * position_Delta[0] + position_Delta[1] * position_Delta[1] - + position_Delta[2] * position_Delta[2], - ); + + position_Delta[2] * position_Delta[2]); } }