Averages/Mean angle: Difference between revisions
Content added Content deleted
(→{{header|Befunge}}: Didn't work) |
No edit summary |
||
Line 1,662: | Line 1,662: | ||
</pre> |
</pre> |
||
Note that with the increase in decimal digit precision, the 2<sup>nd</sup> mean angle changed dramatically from an earlier result. <br><br> |
Note that with the increase in decimal digit precision, the 2<sup>nd</sup> mean angle changed dramatically from an earlier result. <br><br> |
||
=={{header|Ring}}== |
|||
<lang ring> |
|||
# Project : Averages/Mean angle |
|||
# Date : 2018/01/08 |
|||
# Author : Gal Zsolt (~ CalmoSoft ~) |
|||
# Email : <calmosoft@gmail.com> |
|||
load "stdlib.ring" |
|||
decimals(6) |
|||
pi = 3.1415926535897 |
|||
angles = [350,10] |
|||
see meanangle(angles, 2) + nl |
|||
angles = [90,180,270,360] |
|||
see meanangle(angles, 4) + nl |
|||
angles = [10,20,30] |
|||
see meanangle(angles, 3) + nl |
|||
func meanangle(angles, n) |
|||
sumsin = 0 |
|||
sumcos = 0 |
|||
for i = 1 to n |
|||
sumsin = sumsin + sin(angles[i]*pi/180) |
|||
sumcos = sumcos + cos(angles[i]*pi/180) |
|||
next |
|||
return 180/pi*atan3(sumsin, sumcos) |
|||
func atan3(y,x) |
|||
if x <= 0 |
|||
return sign(y)*pi/2 |
|||
ok |
|||
if x>0 |
|||
return atan(y/x) |
|||
else |
|||
if y>0 |
|||
return atan(y/x)+pi |
|||
else |
|||
return atan(y/x)-pi |
|||
ok |
|||
ok |
|||
</lang> |
|||
Output: |
|||
<pre> |
|||
-0.000000 |
|||
-90 |
|||
20.000000 |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |