Angles (geometric), normalization and conversion: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Groovy}}: new solution)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 248:
1000000 5.92562 radian 16042.8 0 1.62975e+006 5.92562 12
</pre>
 
=={{header|C}}==
<lang c>#define PI 3.141592653589793
Line 617 ⟶ 618:
1000000 5.9256211 339.5130823 377.2367581 6035.7881301
</pre>
 
 
=={{header|Groovy}}==
Line 1,065:
1e+06 radians 339.513 377.237 6035.79 5.92562</pre>
 
=={{header|Perl 6Phix}}==
Obviously if preferred you could define a long list of routines such as function d2g(atom a) return remainder(a/360,1)*400 end function
<lang Phix>constant units = {"degrees","gradians","mils","radians"},
turns = {1/360,1/400,1/6400,0.5/PI}
 
function convert(atom a, integer fdx, tdx)
return remainder(a*turns[fdx],1)/turns[tdx]
end function
 
constant tests = {-2,-1,0,1,2,2*PI,16,57.2957795,359,399,6399,1000000}
printf(1," angle unit %9s %9s %9s %9s\n",units)
for i=1 to length(tests) do
for fdx=1 to length(units) do
printf(1,"%9g %-8s",{tests[i],units[fdx]})
for tdx=1 to length(units) do
printf(1," %9g",convert(tests[i],fdx,tdx))
end for
puts(1,"\n")
end for
puts(1,"\n")
end for</lang>
{{out}}
<pre>
angle unit degrees gradians mils radians
-2 degrees -2 -2.22222 -35.5556 -0.034907
-2 gradians -1.8 -2 -32 -0.031416
-2 mils -0.1125 -0.125 -2 -0.001963
-2 radians -114.592 -127.324 -2037.18 -2
 
-1 degrees -1 -1.11111 -17.7778 -0.017453
-1 gradians -0.9 -1 -16 -0.015708
-1 mils -0.05625 -0.0625 -1 -0.000982
-1 radians -57.2958 -63.662 -1018.59 -1
 
0 degrees 0 0 0 0
0 gradians 0 0 0 0
0 mils 0 0 0 0
0 radians 0 0 0 0
 
1 degrees 1 1.11111 17.7778 0.017453
1 gradians 0.9 1 16 0.015708
1 mils 0.05625 0.0625 1 0.000982
1 radians 57.2958 63.662 1018.59 1
 
2 degrees 2 2.22222 35.5556 0.034907
2 gradians 1.8 2 32 0.031416
2 mils 0.1125 0.125 2 0.001963
2 radians 114.592 127.324 2037.18 2
 
6.28319 degrees 6.28319 6.98132 111.701 0.109662
6.28319 gradians 5.65487 6.28319 100.531 0.098696
6.28319 mils 0.353429 0.392699 6.28319 0.006169
6.28319 radians 0 0 0 0
 
16 degrees 16 17.7778 284.444 0.279253
16 gradians 14.4 16 256 0.251327
16 mils 0.9 1 16 0.015708
16 radians 196.732 218.592 3497.47 3.43363
 
57.2958 degrees 57.2958 63.662 1018.59 1
57.2958 gradians 51.5662 57.2958 916.732 0.9
57.2958 mils 3.22289 3.58099 57.2958 0.05625
57.2958 radians 42.8063 47.5626 761.002 0.747112
 
359 degrees 359 398.889 6382.22 6.26573
359 gradians 323.1 359 5744 5.63916
359 mils 20.1937 22.4375 359 0.352447
359 radians 49.1848 54.6498 874.397 0.858437
 
399 degrees 39 43.3333 693.333 0.680678
399 gradians 359.1 399 6384 6.26748
399 mils 22.4438 24.9375 399 0.391717
399 radians 181.016 201.129 3218.06 3.15933
 
6399 degrees 279 310 4960 4.86947
6399 gradians 359.1 399 6384 6.26748
6399 mils 359.944 399.938 6399 6.2822
6399 radians 155.693 172.992 2767.88 2.71736
 
1e+6 degrees 280 311.111 4977.78 4.88692
1e+6 gradians 0 0 0 0
1e+6 mils 90 100 1600 1.5708
1e+6 radians 339.513 377.237 6035.79 5.92562
</pre>
 
=={{header|Python}}==
<lang python>PI = 3.141592653589793
TWO_PI = 6.283185307179586
 
def normalize2deg(a):
while a < 0: a += 360
while a >= 360: a -= 360
return a
def normalize2grad(a):
while a < 0: a += 400
while a >= 400: a -= 400
return a
def normalize2mil(a):
while a < 0: a += 6400
while a >= 6400: a -= 6400
return a
def normalize2rad(a):
while a < 0: a += TWO_PI
while a >= TWO_PI: a -= TWO_PI
return a
 
def deg2grad(a): return a * 10.0 / 9.0
def deg2mil(a): return a * 160.0 / 9.0
def deg2rad(a): return a * PI / 180.0
 
def grad2deg(a): return a * 9.0 / 10.0
def grad2mil(a): return a * 16.0
def grad2rad(a): return a * PI / 200.0
 
def mil2deg(a): return a * 9.0 / 160.0
def mil2grad(a): return a / 16.0
def mil2rad(a): return a * PI / 3200.0
 
def rad2deg(a): return a * 180.0 / PI
def rad2grad(a): return a * 200.0 / PI
def rad2mil(a): return a * 3200.0 / PI</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>
my @units =
Line 1,269 ⟶ 1,392:
1000000 mils 0.25 90 100 1600 1.57079633
1000000 radians 0.9430919 339.51308233 377.23675814 6035.78813022 5.92562114</pre>
 
=={{header|Phix}}==
Obviously if preferred you could define a long list of routines such as function d2g(atom a) return remainder(a/360,1)*400 end function
<lang Phix>constant units = {"degrees","gradians","mils","radians"},
turns = {1/360,1/400,1/6400,0.5/PI}
 
function convert(atom a, integer fdx, tdx)
return remainder(a*turns[fdx],1)/turns[tdx]
end function
 
constant tests = {-2,-1,0,1,2,2*PI,16,57.2957795,359,399,6399,1000000}
printf(1," angle unit %9s %9s %9s %9s\n",units)
for i=1 to length(tests) do
for fdx=1 to length(units) do
printf(1,"%9g %-8s",{tests[i],units[fdx]})
for tdx=1 to length(units) do
printf(1," %9g",convert(tests[i],fdx,tdx))
end for
puts(1,"\n")
end for
puts(1,"\n")
end for</lang>
{{out}}
<pre>
angle unit degrees gradians mils radians
-2 degrees -2 -2.22222 -35.5556 -0.034907
-2 gradians -1.8 -2 -32 -0.031416
-2 mils -0.1125 -0.125 -2 -0.001963
-2 radians -114.592 -127.324 -2037.18 -2
 
-1 degrees -1 -1.11111 -17.7778 -0.017453
-1 gradians -0.9 -1 -16 -0.015708
-1 mils -0.05625 -0.0625 -1 -0.000982
-1 radians -57.2958 -63.662 -1018.59 -1
 
0 degrees 0 0 0 0
0 gradians 0 0 0 0
0 mils 0 0 0 0
0 radians 0 0 0 0
 
1 degrees 1 1.11111 17.7778 0.017453
1 gradians 0.9 1 16 0.015708
1 mils 0.05625 0.0625 1 0.000982
1 radians 57.2958 63.662 1018.59 1
 
2 degrees 2 2.22222 35.5556 0.034907
2 gradians 1.8 2 32 0.031416
2 mils 0.1125 0.125 2 0.001963
2 radians 114.592 127.324 2037.18 2
 
6.28319 degrees 6.28319 6.98132 111.701 0.109662
6.28319 gradians 5.65487 6.28319 100.531 0.098696
6.28319 mils 0.353429 0.392699 6.28319 0.006169
6.28319 radians 0 0 0 0
 
16 degrees 16 17.7778 284.444 0.279253
16 gradians 14.4 16 256 0.251327
16 mils 0.9 1 16 0.015708
16 radians 196.732 218.592 3497.47 3.43363
 
57.2958 degrees 57.2958 63.662 1018.59 1
57.2958 gradians 51.5662 57.2958 916.732 0.9
57.2958 mils 3.22289 3.58099 57.2958 0.05625
57.2958 radians 42.8063 47.5626 761.002 0.747112
 
359 degrees 359 398.889 6382.22 6.26573
359 gradians 323.1 359 5744 5.63916
359 mils 20.1937 22.4375 359 0.352447
359 radians 49.1848 54.6498 874.397 0.858437
 
399 degrees 39 43.3333 693.333 0.680678
399 gradians 359.1 399 6384 6.26748
399 mils 22.4438 24.9375 399 0.391717
399 radians 181.016 201.129 3218.06 3.15933
 
6399 degrees 279 310 4960 4.86947
6399 gradians 359.1 399 6384 6.26748
6399 mils 359.944 399.938 6399 6.2822
6399 radians 155.693 172.992 2767.88 2.71736
 
1e+6 degrees 280 311.111 4977.78 4.88692
1e+6 gradians 0 0 0 0
1e+6 mils 90 100 1600 1.5708
1e+6 radians 339.513 377.237 6035.79 5.92562
</pre>
 
=={{header|Python}}==
<lang python>PI = 3.141592653589793
TWO_PI = 6.283185307179586
 
def normalize2deg(a):
while a < 0: a += 360
while a >= 360: a -= 360
return a
def normalize2grad(a):
while a < 0: a += 400
while a >= 400: a -= 400
return a
def normalize2mil(a):
while a < 0: a += 6400
while a >= 6400: a -= 6400
return a
def normalize2rad(a):
while a < 0: a += TWO_PI
while a >= TWO_PI: a -= TWO_PI
return a
 
def deg2grad(a): return a * 10.0 / 9.0
def deg2mil(a): return a * 160.0 / 9.0
def deg2rad(a): return a * PI / 180.0
 
def grad2deg(a): return a * 9.0 / 10.0
def grad2mil(a): return a * 16.0
def grad2rad(a): return a * PI / 200.0
 
def mil2deg(a): return a * 9.0 / 160.0
def mil2grad(a): return a / 16.0
def mil2rad(a): return a * PI / 3200.0
 
def rad2deg(a): return a * 180.0 / PI
def rad2grad(a): return a * 200.0 / PI
def rad2mil(a): return a * 3200.0 / PI</lang>
 
=={{header|REXX}}==
10,327

edits