How to resolve the algorithm Averages/Mean angle step by step in the Rust programming language
How to resolve the algorithm Averages/Mean angle step by step in the Rust programming language
Table of Contents
Problem Statement
When calculating the average or mean of an angle one has to take into account how angles wrap around so that any angle in degrees plus any integer multiple of 360 degrees is a measure of the same angle. If one wanted an average direction of the wind over two readings where the first reading was of 350 degrees and the second was of 10 degrees then the average of the numbers is 180 degrees, whereas if you can note that 350 degrees is equivalent to -10 degrees and so you have two readings at 10 degrees either side of zero degrees leading to a more fitting mean angle of zero degrees. To calculate the mean angle of several angles:
(Note that, since the mean is the sum divided by the number of numbers, and division by a positive real number does not affect the angle, you can also simply compute the sum for step 2.) You can alternatively use this formula:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Averages/Mean angle step by step in the Rust programming language
Source code in the rust programming language
use std::f64;
// the macro is from
// http://stackoverflow.com/questions/30856285/assert-eq-with-floating-
// point-numbers-and-delta
fn mean_angle(angles: &[f64]) -> f64 {
let length: f64 = angles.len() as f64;
let cos_mean: f64 = angles.iter().fold(0.0, |sum, i| sum + i.to_radians().cos()) / length;
let sin_mean: f64 = angles.iter().fold(0.0, |sum, i| sum + i.to_radians().sin()) / length;
(sin_mean).atan2(cos_mean).to_degrees()
}
fn main() {
let angles1 = [350.0_f64, 10.0];
let angles2 = [90.0_f64, 180.0, 270.0, 360.0];
let angles3 = [10.0_f64, 20.0, 30.0];
println!("Mean Angle for {:?} is {:.5} degrees",
&angles1,
mean_angle(&angles1));
println!("Mean Angle for {:?} is {:.5} degrees",
&angles2,
mean_angle(&angles2));
println!("Mean Angle for {:?} is {:.5} degrees",
&angles3,
mean_angle(&angles3));
}
macro_rules! assert_diff{
($x: expr,$y : expr, $diff :expr)=>{
if ( $x - $y ).abs() > $diff {
panic!("floating point difference is to big {}", $x - $y );
}
}
}
#[test]
fn calculate() {
let angles1 = [350.0_f64, 10.0];
let angles2 = [90.0_f64, 180.0, 270.0, 360.0];
let angles3 = [10.0_f64, 20.0, 30.0];
assert_diff!(0.0, mean_angle(&angles1), 0.001);
assert_diff!(-90.0, mean_angle(&angles2), 0.001);
assert_diff!(20.0, mean_angle(&angles3), 0.001);
}
You may also check:How to resolve the algorithm Stack traces step by step in the Lasso programming language
You may also check:How to resolve the algorithm ISBN13 check digit step by step in the Factor programming language
You may also check:How to resolve the algorithm Xiaolin Wu's line algorithm step by step in the Action! programming language
You may also check:How to resolve the algorithm Distributed programming step by step in the Python programming language
You may also check:How to resolve the algorithm The sieve of Sundaram step by step in the Java programming language