Averages/Mean angle: Difference between revisions

Content deleted Content added
MaiconSoft (talk | contribs)
No edit summary
Petelomax (talk | contribs)
m →‎{{header|Phix}}: minor tidy, use builtin atan2, round to 10dp.
Line 1,483: Line 1,483:


=={{header|Phix}}==
=={{header|Phix}}==
Copied from [[Euphoria]], and slightly improved
Copied from [[Averages/Mean_angle#Euphoria|Euphoria]], and slightly improved
<lang Phix>function atan2(atom y, atom x)
<lang Phix>function MeanAngle(sequence angles)
atom x = 0, y = 0
return 2*arctan((sqrt(power(x,2)+power(y,2))-x)/y)
for i=1 to length(angles) do
end function
atom ai_rad = angles[i]*PI/180

function MeanAngle(sequence angles)
atom x=0, y=0, ai_rad
integer l=length(angles)

for i=1 to l do
ai_rad = angles[i]*PI/180
x += cos(ai_rad)
x += cos(ai_rad)
y += sin(ai_rad)
y += sin(ai_rad)
end for
end for
if abs(x)<1e-16 then return "not meaningful" end if
if abs(x)<1e-16 then return "not meaningful" end if
return sprintf("%9.5f",atan2(y,x)*180/PI)
return sprintf("%g",round(atan2(y,x)*180/PI,1e10))
end function
end function

constant AngleLists = {{350,10},{90,180,270,360},{10,20,30},{180},{0,180}}
constant AngleLists = {{350,10},{90,180,270,360},{10,20,30},{180},{0,180}}
sequence ai
for i=1 to length(AngleLists) do
for i=1 to length(AngleLists) do
ai = AngleLists[i]
sequence ai = AngleLists[i]
printf(1,"%+16s: Mean Angle is %s\n",{sprint(ai),MeanAngle(ai)})
printf(1,"%16v: Mean Angle is %s\n",{ai,MeanAngle(ai)})
end for
end for</lang>
{} = wait_key()</lang>
{{out}}
{{out}}
<pre>
<pre>
{350,10}: Mean Angle is 0.00000
{350,10}: Mean Angle is 0
{90,180,270,360}: Mean Angle is not meaningful
{90,180,270,360}: Mean Angle is not meaningful
{10,20,30}: Mean Angle is 20.00000
{10,20,30}: Mean Angle is 20
{180}: Mean Angle is 180.00000
{180}: Mean Angle is 180
{0,180}: Mean Angle is not meaningful
{0,180}: Mean Angle is not meaningful
</pre>
</pre>