Averages/Mean angle: Difference between revisions

Line 2,027:
The second example is interesting because it computes ATan2(0.,0.), which is undefined in mathematics (like division by zero), but the floating point processor in IBM-PC-type computers defines it as zero. The reason -90 gets displayed instead is due to small rounding errors (and another extra-mathematical concept, -0). In fact almost any angle can result because of slight rounding errors as Y and X both approach zero.
 
=={{header|VBA}}==
<lang vb>Option Base 1
Private Function mean_angle(angles As Variant) As Double
Dim sins() As Double, coss() As Double
ReDim sins(UBound(angles))
ReDim coss(UBound(angles))
For i = LBound(angles) To UBound(angles)
sins(i) = Sin(WorksheetFunction.Radians(angles(i)))
coss(i) = Cos(WorksheetFunction.Radians(angles(i)))
Next i
mean_angle = WorksheetFunction.Degrees( _
WorksheetFunction.Atan2( _
WorksheetFunction.sum(coss), _
WorksheetFunction.sum(sins)))
End Function
Public Sub main()
Debug.Print Format(mean_angle([{350,10}]), "##0")
Debug.Print Format(mean_angle([{90, 180, 270, 360}]), "##0")
Debug.Print Format(mean_angle([{10, 20, 30}]), "##0")
End Sub</lang>{{out}}
<pre>0
-90
20</pre>
=={{header|zkl}}==
<lang zkl>fcn meanA(a1,a2,etc){
255

edits