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.subdeg2rad { $^d * tau / 360 }subrad2deg { $^r * 360 / tau }subphase ($c) {my ($mag,$ang) = $c.polar;return NaN if $mag < 1e-16; $ang;}submeanAngle { 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