user
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:

1. Assume all angles are on the unit circle and convert them to complex numbers expressed in real and imaginary form.
2. Compute the mean of the complex numbers.
3. Convert the complex mean to polar coordinates whereupon the phase of the complex mean is the required angular mean.

(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:

:   Given the angles 

    α

    1
    ,
    …
    ,

    α

    n
    {\displaystyle {\displaystyle \alpha \_{1},\dots ,\alpha \_{n}}}
     the mean is computed by

:   :   α
        ¯
        =
        atan2
        ⁡

        (

        1
        n
        ⋅

        ∑

        j
        =
        1

        n
        sin
        ⁡

        α

        j
        ,


        1
        n
        ⋅

        ∑

        j
        =
        1

        n
        cos
        ⁡

        α

        j
        )
        {\displaystyle {\displaystyle {\bar {\alpha }}=\operatorname {atan2} \left({\frac {1}{n}}\cdot \sum \_{j=1}^{n}\sin \alpha \_{j},{\frac {1}{n}}\cdot \sum \_{j=1}^{n}\cos \alpha \_{j}\right)}}

Task

1. write a function/method/subroutine/... that given a list of angles in degrees returns their mean angle.   
    (You should use a built-in function if you have one that does this for degrees or radians).
2. Use the function to compute the means of these lists of angles (in degrees):
   * [350, 10]
   * [90, 180, 270, 360]
   * [10, 20, 30]
3. Show your output here.

See also

|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| | Tasks for calculating statistical measures | | | --- | --- | |  | | |  |  |  |  |  | | --- | --- | --- | --- | --- | |  | | in one go | moving *(sliding window)* | moving *(cumulative)* | | Mean | Arithmetic | Statistics/Basic  Averages/Arithmetic mean  Averages/Pythagorean means | Averages/Simple moving average |  | | Geometric | Averages/Pythagorean means |  |  | | Harmonic | Averages/Pythagorean means |  |  | | Quadratic | Averages/Root mean square |  |  | | Circular | Averages/Mean angle  Averages/Mean time of day |  |  | | Median | | Averages/Median |  |  | | Mode | | Averages/Mode |  |  | | Standard deviation | | Statistics/Basic |  | Cumulative standard deviation | | | |

---
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2015.12

This solution refuses to return an answer when the angles cancel out to a tiny magnitude.

```
# Of course, you can still use pi and 180.
sub deg2rad { $^d * tau / 360 }
sub rad2deg { $^r * 360 / tau }

sub phase ($c)  {
    my ($mag,$ang) = $c.polar;
    return NaN if $mag < 1e-16;
    $ang;
}

sub meanAngle { rad2deg phase [+] map { cis deg2rad $_ }, @^angles }

say meanAngle($_).fmt("%.2f\tis the mean angle of "), $_ for
    [350, 10],
    [90, 180, 270, 360],
    [10, 20, 30];

```

Output:

```
-0.00	is the mean angle of 350 10
NaN	is the mean angle of 90 180 270 360
20.00	is the mean angle of 10 20 30
```