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

Added Algol W
m (Typo)
(Added Algol W)
 
(30 intermediate revisions by 19 users not shown)
Line 47:
:::*   deg
:::*   º       (a symbol)
:::*   °       (aanother symbol)
 
 
Line 56:
:::*   gon(s)
:::*   metric degree(s)
:::* &nbsp; (Note that &nbsp; '''centigrade''' &nbsp; was used for <sup>1</sup>/<sub>100</sub><sup>th</sup> of a grade, see the note below.)
 
 
Line 117 ⟶ 118:
:* &nbsp; '''-2 &nbsp; -1 &nbsp; 0 &nbsp; 1 &nbsp; 2 &nbsp; 6.2831853 &nbsp; 16 &nbsp; 57.2957795 &nbsp; 359 &nbsp; 399 &nbsp; 6399 &nbsp; 1000000'''
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">V values = [Float(-2), -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000]
 
F normd(x) {R fmod(x, 360)}
F normg(x) {R fmod(x, 400)}
F normm(x) {R fmod(x, 6400)}
F normr(x) {R fmod(x, (2 * math:pi))}
 
F d2g(x) {R normd(x) * 10 / 9}
F d2m(x) {R normd(x) * 160 / 9}
F d2r(x) {R normd(x) * math:pi / 180}
 
F g2d(x) {R normg(x) * 9 / 10}
F g2m(x) {R normg(x) * 16}
F g2r(x) {R normg(x) * math:pi / 200}
 
F m2d(x) {R normm(x) * 9 / 160}
F m2g(x) {R normm(x) / 16}
F m2r(x) {R normm(x) * math:pi / 3200}
 
F r2d(x) {R normr(x) * 180 / math:pi}
F r2g(x) {R normr(x) * 200 / math:pi}
F r2m(x) {R normr(x) * 3200 / math:pi}
 
print(‘ Degrees Normalized Gradians Mils Radians’)
print(‘───────────────────────────────────────────────────────────────────────────────────’)
L(val) values
print(‘#7.7 #7.7 #7.7 #7.7 #7.7’.format(val, normd(val), d2g(val), d2m(val), d2r(val)))
 
print()
print(‘ Gradians Normalized Degrees Mils Radians’)
print(‘───────────────────────────────────────────────────────────────────────────────────’)
L(val) values
print(‘#7.7 #7.7 #7.7 #7.7 #7.7’.format(val, normg(val), g2d(val), g2m(val), g2r(val)))
 
print()
print(‘ Mils Normalized Degrees Gradians Radians’)
print(‘───────────────────────────────────────────────────────────────────────────────────’)
L(val) values
print(‘#7.7 #7.7 #7.7 #7.7 #7.7’.format(val, normm(val), m2d(val), m2g(val), m2r(val)))
 
print()
print(‘ Radians Normalized Degrees Gradians Mils’)
print(‘───────────────────────────────────────────────────────────────────────────────────’)
L(val) values
print(‘#7.7 #7.7 #7.7 #7.7 #7.7’.format(val, normr(val), r2d(val), r2g(val), r2m(val)))</syntaxhighlight>
 
{{out}}
<pre>
Degrees Normalized Gradians Mils Radians
───────────────────────────────────────────────────────────────────────────────────
-2.0000000 -2.0000000 -2.2222222 -35.5555556 -0.0349066
-1.0000000 -1.0000000 -1.1111111 -17.7777778 -0.0174533
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 1.1111111 17.7777778 0.0174533
2.0000000 2.0000000 2.2222222 35.5555556 0.0349066
6.2831853 6.2831853 6.9813170 111.7010720 0.1096623
16.0000000 16.0000000 17.7777778 284.4444444 0.2792527
57.2957795 57.2957795 63.6619772 1018.5916356 1.0000000
359.0000000 359.0000000 398.8888889 6382.2222222 6.2657320
399.0000000 39.0000000 43.3333333 693.3333333 0.6806784
6399.0000000 279.0000000 310.0000000 4960.0000000 4.8694686
1000000.0000000 280.0000000 311.1111111 4977.7777778 4.8869219
 
Gradians Normalized Degrees Mils Radians
───────────────────────────────────────────────────────────────────────────────────
-2.0000000 -2.0000000 -1.8000000 -32.0000000 -0.0314159
-1.0000000 -1.0000000 -0.9000000 -16.0000000 -0.0157080
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 0.9000000 16.0000000 0.0157080
2.0000000 2.0000000 1.8000000 32.0000000 0.0314159
6.2831853 6.2831853 5.6548668 100.5309648 0.0986960
16.0000000 16.0000000 14.4000000 256.0000000 0.2513274
57.2957795 57.2957795 51.5662015 916.7324720 0.9000000
359.0000000 359.0000000 323.1000000 5744.0000000 5.6391588
399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
6399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
1000000.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 
Mils Normalized Degrees Gradians Radians
───────────────────────────────────────────────────────────────────────────────────
-2.0000000 -2.0000000 -0.1125000 -0.1250000 -0.0019635
-1.0000000 -1.0000000 -0.0562500 -0.0625000 -0.0009817
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 0.0562500 0.0625000 0.0009817
2.0000000 2.0000000 0.1125000 0.1250000 0.0019635
6.2831853 6.2831853 0.3534292 0.3926991 0.0061685
16.0000000 16.0000000 0.9000000 1.0000000 0.0157080
57.2957795 57.2957795 3.2228876 3.5809862 0.0562500
359.0000000 359.0000000 20.1937500 22.4375000 0.3524474
399.0000000 399.0000000 22.4437500 24.9375000 0.3917173
6399.0000000 6399.0000000 359.9437500 399.9375000 6.2822036
1000000.0000000 1600.0000000 90.0000000 100.0000000 1.5707963
 
Radians Normalized Degrees Gradians Mils
───────────────────────────────────────────────────────────────────────────────────
-2.0000000 -2.0000000 -114.5915590 -127.3239545 -2037.1832716
-1.0000000 -1.0000000 -57.2957795 -63.6619772 -1018.5916358
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 57.2957795 63.6619772 1018.5916358
2.0000000 2.0000000 114.5915590 127.3239545 2037.1832716
6.2831853 6.2831853 359.9999996 399.9999995 6399.9999927
16.0000000 3.4336294 196.7324722 218.5916358 3497.4661726
57.2957795 0.7471117 42.8063493 47.5626103 761.0017647
359.0000000 0.8584375 49.1848452 54.6498280 874.3972479
399.0000000 3.1593256 181.0160257 201.1289175 3218.0626795
6399.0000000 2.7173573 155.6931042 172.9923380 2767.8774082
1000000.0000000 5.9256211 339.5130823 377.2367581 6035.7881302
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|11l|using an array of procedures to simplify the output}}
<syntaxhighlight lang="algol68">
BEGIN # Angles (geometric), normalization and conversion - translated from the 11l sample #
 
[]REAL values = ( -2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000 );
 
PROC norm = ( REAL x, y )REAL:
BEGIN
INT n = ENTIER ( ABS x / ABS y );
REAL r := x - ( n * y );
r
END # norm # ;
PROC normd = ( REAL x )REAL: norm( x, 360 );
PROC normg = ( REAL x )REAL: norm( x, 400 );
PROC normm = ( REAL x )REAL: norm( x, 6400 );
PROC normr = ( REAL x )REAL: norm( x, 2 * pi );
 
PROC d2g = ( REAL x )REAL: normd( x ) * 10 / 9;
PROC d2m = ( REAL x )REAL: normd( x ) * 160 / 9;
PROC d2r = ( REAL x )REAL: normd( x ) * pi / 180;
 
PROC g2d = ( REAL x )REAL: normg( x ) * 9 / 10;
PROC g2m = ( REAL x )REAL: normg( x ) * 16;
PROC g2r = ( REAL x )REAL: normg( x ) * pi / 200;
 
PROC m2d = ( REAL x )REAL: normm( x ) * 9 / 160;
PROC m2g = ( REAL x )REAL: normm( x ) / 16;
PROC m2r = ( REAL x )REAL: normm( x ) * pi / 3200;
 
PROC r2d = ( REAL x )REAL: normr( x ) * 180 / pi;
PROC r2g = ( REAL x )REAL: normr( x ) * 200 / pi;
PROC r2m = ( REAL x )REAL: normr( x ) * 3200 / pi;
 
STRING underline = "----------------------------------------------------------------------------------";
PROC f7d7 = ( REAL v )STRING: fixed( v, -15, 7 );
 
PROC print values = ( STRING heading, []PROC(REAL)REAL f )VOID:
BEGIN
print( ( heading, newline ) );
print( ( underline, newline ) );
FOR i FROM LWB values TO UPB values DO
REAL v = values[ i ];
print( ( f7d7( v ) ) );
FOR p FROM LWB f TO UPB f DO
print( ( " ", f7d7( f[ p ]( v ) ) ) )
OD;
print( ( newline ) )
OD;
print( ( newline ) )
END # print values # ;
 
print values( " Degrees Normalized Gradians Mils Radians"
, ( normd, d2g, d2m, d2r )
);
print values( " Gradians Normalized Degrees Mils Radians"
, ( normg, g2d, g2m, g2r )
);
print values( " Mils Normalized Degrees Gradians Radians"
, ( normm, m2d, m2g, m2r )
);
print values( " Radians Normalized Degrees Gradians Mils"
, ( normr, r2d, r2g, r2m )
)
 
END
</syntaxhighlight>
{{out}}
<pre>
Degrees Normalized Gradians Mils Radians
----------------------------------------------------------------------------------
-2.0000000 -2.0000000 -2.2222222 -35.5555556 -0.0349066
-1.0000000 -1.0000000 -1.1111111 -17.7777778 -0.0174533
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 1.1111111 17.7777778 0.0174533
2.0000000 2.0000000 2.2222222 35.5555556 0.0349066
6.2831853 6.2831853 6.9813170 111.7010720 0.1096623
16.0000000 16.0000000 17.7777778 284.4444444 0.2792527
57.2957795 57.2957795 63.6619772 1018.5916356 1.0000000
359.0000000 359.0000000 398.8888889 6382.2222222 6.2657320
399.0000000 39.0000000 43.3333333 693.3333333 0.6806784
6399.0000000 279.0000000 310.0000000 4960.0000000 4.8694686
1000000.0000000 280.0000000 311.1111111 4977.7777778 4.8869219
 
Gradians Normalized Degrees Mils Radians
----------------------------------------------------------------------------------
-2.0000000 -2.0000000 -1.8000000 -32.0000000 -0.0314159
-1.0000000 -1.0000000 -0.9000000 -16.0000000 -0.0157080
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 0.9000000 16.0000000 0.0157080
2.0000000 2.0000000 1.8000000 32.0000000 0.0314159
6.2831853 6.2831853 5.6548668 100.5309648 0.0986960
16.0000000 16.0000000 14.4000000 256.0000000 0.2513274
57.2957795 57.2957795 51.5662015 916.7324720 0.9000000
359.0000000 359.0000000 323.1000000 5744.0000000 5.6391588
399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
6399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
1000000.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 
Mils Normalized Degrees Gradians Radians
----------------------------------------------------------------------------------
-2.0000000 -2.0000000 -0.1125000 -0.1250000 -0.0019635
-1.0000000 -1.0000000 -0.0562500 -0.0625000 -0.0009817
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 0.0562500 0.0625000 0.0009817
2.0000000 2.0000000 0.1125000 0.1250000 0.0019635
6.2831853 6.2831853 0.3534292 0.3926991 0.0061685
16.0000000 16.0000000 0.9000000 1.0000000 0.0157080
57.2957795 57.2957795 3.2228876 3.5809862 0.0562500
359.0000000 359.0000000 20.1937500 22.4375000 0.3524474
399.0000000 399.0000000 22.4437500 24.9375000 0.3917173
6399.0000000 6399.0000000 359.9437500 399.9375000 6.2822036
1000000.0000000 1600.0000000 90.0000000 100.0000000 1.5707963
 
Radians Normalized Degrees Gradians Mils
----------------------------------------------------------------------------------
-2.0000000 -2.0000000 -114.5915590 -127.3239545 -2037.1832716
-1.0000000 -1.0000000 -57.2957795 -63.6619772 -1018.5916358
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 57.2957795 63.6619772 1018.5916358
2.0000000 2.0000000 114.5915590 127.3239545 2037.1832716
6.2831853 6.2831853 359.9999996 399.9999995 6399.9999927
16.0000000 3.4336294 196.7324722 218.5916358 3497.4661726
57.2957795 0.7471117 42.8063493 47.5626103 761.0017647
359.0000000 0.8584375 49.1848452 54.6498280 874.3972479
399.0000000 3.1593256 181.0160257 201.1289175 3218.0626795
6399.0000000 2.7173573 155.6931042 172.9923380 2767.8774082
1000000.0000000 5.9256211 339.5130823 377.2367581 6035.7881302
</pre>
 
=={{header|ALGOL W}}==
{{Trans|ALGOL 68|which was a translation of 11l}}
Algol W doesn't have arrays of procedures so separate parameters for the four functions are used.
<syntaxhighlight lang="algolw">
begin % Angles (geometric), normalization and conversion %
% - translation of the Algol 68 translation of the 11l sample %
 
real procedure norm ( real value x, y ) ;
begin
integer n;
n := entier( abs x / abs y );
x - ( n * y )
end norm ;
real procedure normd ( real value x ) ; norm( x, 360 );
real procedure normg ( real value x ) ; norm( x, 400 );
real procedure normm ( real value x ) ; norm( x, 6400 );
real procedure normr ( real value x ) ; norm( x, 2 * pi );
 
real procedure d2g ( real value x ) ; normd( x ) * 10 / 9;
real procedure d2m ( real value x ) ; normd( x ) * 160 / 9;
real procedure d2r ( real value x ) ; normd( x ) * pi / 180;
 
real procedure g2d ( real value x ) ; normg( x ) * 9 / 10;
real procedure g2m ( real value x ) ; normg( x ) * 16;
real procedure g2r ( real value x ) ; normg( x ) * pi / 200;
 
real procedure m2d ( real value x ) ; normm( x ) * 9 / 160;
real procedure m2g ( real value x ) ; normm( x ) / 16;
real procedure m2r ( real value x ) ; normm( x ) * pi / 3200;
 
real procedure r2d ( real value x ) ; normr( x ) * 180 / pi;
real procedure r2g ( real value x ) ; normr( x ) * 200 / pi;
real procedure r2m ( real value x ) ; normr( x ) * 3200 / pi;
 
procedure writeonF7d7 ( real value v ) ; writeon( r_format := "A", r_w := 15, r_d := 7, s_w := 0, v );
 
procedure printValues ( string(82) value heading
; real procedure f1, f2, f3, f4
; real array values( * )
; integer value numberOfValues
) ;
begin
write( heading );
write( "----------------------------------------------------------------------------------" );
for i := 1 until numberOfValues do begin
real v;
v := values( i );
write();
writeonF7d7( v );
writeon( " " ); writeonF7d7( f1( v ) ); writeon( " " ); writeonF7d7( f2( v ) );
writeon( " " ); writeonF7d7( f3( v ) ); writeon( " " ); writeonF7d7( f4( v ) );
end for_i ;
write()
end printValues ;
 
real array values ( 1 :: 12 );
values( 1 ) := -2; values( 2 ) := -1; values( 3 ) := 0; values( 4 ) := 1; values( 5 ) := 2;
values( 6 ) := 6.2831853; values( 7 ) := 16; values( 8 ) := 57.2957795; values( 9 ) := 359;
values( 10 ) := 399; values( 11 ) := 6399; values( 12 ) := 1000000;
 
printValues( " Degrees Normalized Gradians Mils Radians"
, normd, d2g, d2m, d2r, values, 12
);
printValues( " Gradians Normalized Degrees Mils Radians"
, normg, g2d, g2m, g2r, values, 12
);
printValues( " Mils Normalized Degrees Gradians Radians"
, normm, m2d, m2g, m2r, values, 12
);
printValues( " Radians Normalized Degrees Gradians Mils"
, normr, r2d, r2g, r2m, values, 12
)
 
end.
</syntaxhighlight>
{{out}}
<pre>
Degrees Normalized Gradians Mils Radians
----------------------------------------------------------------------------------
-2.0000000 -2.0000000 -2.2222222 -35.5555556 -0.0349066
-1.0000000 -1.0000000 -1.1111111 -17.7777778 -0.0174533
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 1.1111111 17.7777778 0.0174533
2.0000000 2.0000000 2.2222222 35.5555556 0.0349066
6.2831853 6.2831853 6.9813170 111.7010720 0.1096623
16.0000000 16.0000000 17.7777778 284.4444444 0.2792527
57.2957795 57.2957795 63.6619772 1018.5916356 1.0000000
359.0000000 359.0000000 398.8888889 6382.2222222 6.2657320
399.0000000 39.0000000 43.3333333 693.3333333 0.6806784
6399.0000000 279.0000000 310.0000000 4960.0000000 4.8694686
1000000.0000000 280.0000000 311.1111111 4977.7777778 4.8869219
 
Gradians Normalized Degrees Mils Radians
----------------------------------------------------------------------------------
-2.0000000 -2.0000000 -1.8000000 -32.0000000 -0.0314159
-1.0000000 -1.0000000 -0.9000000 -16.0000000 -0.0157080
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 0.9000000 16.0000000 0.0157080
2.0000000 2.0000000 1.8000000 32.0000000 0.0314159
6.2831853 6.2831853 5.6548668 100.5309648 0.0986960
16.0000000 16.0000000 14.4000000 256.0000000 0.2513274
57.2957795 57.2957795 51.5662016 916.7324720 0.9000000
359.0000000 359.0000000 323.1000000 5744.0000000 5.6391588
399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
6399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
1000000.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 
Mils Normalized Degrees Gradians Radians
----------------------------------------------------------------------------------
-2.0000000 -2.0000000 -0.1125000 -0.1250000 -0.0019635
-1.0000000 -1.0000000 -0.0562500 -0.0625000 -0.0009817
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 0.0562500 0.0625000 0.0009817
2.0000000 2.0000000 0.1125000 0.1250000 0.0019635
6.2831853 6.2831853 0.3534292 0.3926991 0.0061685
16.0000000 16.0000000 0.9000000 1.0000000 0.0157080
57.2957795 57.2957795 3.2228876 3.5809862 0.0562500
359.0000000 359.0000000 20.1937500 22.4375000 0.3524474
399.0000000 399.0000000 22.4437500 24.9375000 0.3917173
6399.0000000 6399.0000000 359.9437500 399.9375000 6.2822036
1000000.0000000 1600.0000000 90.0000000 100.0000000 1.5707963
 
Radians Normalized Degrees Gradians Mils
----------------------------------------------------------------------------------
-2.0000000 -2.0000000 -114.5915590 -127.3239545 -2037.1832716
-1.0000000 -1.0000000 -57.2957795 -63.6619772 -1018.5916358
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 57.2957795 63.6619772 1018.5916358
2.0000000 2.0000000 114.5915590 127.3239545 2037.1832716
6.2831853 6.2831853 359.9999996 399.9999995 6399.9999927
16.0000000 3.4336294 196.7324722 218.5916358 3497.4661726
57.2957795 0.7471117 42.8063493 47.5626103 761.0017647
359.0000000 0.8584375 49.1848452 54.6498280 874.3972479
399.0000000 3.1593256 181.0160257 201.1289175 3218.0626795
6399.0000000 2.7173573 155.6931042 172.9923380 2767.8774082
1000000.0000000 5.9256211 339.5130823 377.2367581 6035.7881302
</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">testAngles := [-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000]
 
result .= "Degrees Degrees Gradians Mils Radians`n"
for i, a in testAngles
result .= a "`t" Deg2Deg(a) "`t" Deg2Grad(a) "`t" Deg2Mil(a) "`t" Deg2Rad(a) "`n"
result .= "`nGradians Degrees Gradians Mils Radians`n"
for i, a in testAngles
result .= a "`t" Grad2Deg(a) "`t" Grad2Grad(a) "`t" Grad2Mil(a) "`t" Grad2Rad(a) "`n"
result .= "`nMills Degrees Gradians Mils Radians`n"
for i, a in testAngles
result .= a "`t" Mil2Deg(a) "`t" Mil2Grad(a) "`t" Mil2Mil(a) "`t" Mil2Rad(a) "`n"
result .= "`nRadians Degrees Gradians Mils Radians`n"
for i, a in testAngles
result .= a "`t" Rad2Deg(a) "`t" Rad2Grad(a) "`t" Rad2Mil(a) "`t" Rad2Rad(a) "`n"
 
MsgBox, 262144, , % result
return
;-------------------------------------------------------
Deg2Deg(Deg){
return Mod(Deg, 360)
}
Deg2Grad(Deg){
return Deg2Deg(Deg) * 400 / 360
}
Deg2Mil(Deg){
return Deg2Deg(Deg) * 6400 / 360
}
Deg2Rad(Deg){
return Deg2Deg(Deg) * (π:=3.141592653589793) / 180
}
;-------------------------------------------------------
Grad2Grad(Grad){
return Mod(Grad, 400)
}
Grad2Deg(Grad){
return Grad2Grad(Grad) * 360 / 400
}
Grad2Mil(Grad){
return Grad2Grad(Grad) * 6400 / 400
}
Grad2Rad(Grad){
return Grad2Grad(Grad) * (π:=3.141592653589793) / 200
}
;-------------------------------------------------------
Mil2Mil(Mil){
return Mod(Mil, 6400)
}
Mil2Deg(Mil){
return Mil2Mil(Mil) * 360 / 6400
}
Mil2Grad(Mil){
return Mil2Mil(Mil) * 400 / 6400
}
Mil2Rad(Mil){
return Mil2Mil(Mil) * (π:=3.141592653589793) / 3200
}
;-------------------------------------------------------
Rad2Rad(Rad){
return Mod(Rad, 2*(π:=3.141592653589793))
}
Rad2Deg(Rad){
return Rad2Rad(Rad) * 180 / (π:=3.141592653589793)
}
Rad2Grad(Rad){
return Rad2Rad(Rad) * 200 / (π:=3.141592653589793)
}
Rad2Mil(Rad){
return Rad2Rad(Rad) * 3200 / (π:=3.141592653589793)
}
;-------------------------------------------------------</syntaxhighlight>
{{out}}
<pre>Degrees Degrees Gradians Mils Radians
-2 -2 -2.222222 -35.555556 -0.034907
-1 -1 -1.111111 -17.777778 -0.017453
0 0 0.000000 0.000000 0.000000
1 1 1.111111 17.777778 0.017453
2 2 2.222222 35.555556 0.034907
6.2831853 6.283185 6.981317 111.701072 0.109662
16 16 17.777778 284.444444 0.279253
57.2957795 57.295780 63.661977 1018.591636 1.000000
359 359 398.888889 6382.222222 6.265732
399 39 43.333333 693.333333 0.680678
6399 279 310.000000 4960.000000 4.869469
1000000 280 311.111111 4977.777778 4.886922
 
Gradians Degrees Gradians Mils Radians
-2 -1.800000 -2 -32.000000 -0.031416
-1 -0.900000 -1 -16.000000 -0.015708
0 0.000000 0 0.000000 0.000000
1 0.900000 1 16.000000 0.015708
2 1.800000 2 32.000000 0.031416
6.2831853 5.654867 6.283185 100.530965 0.098696
16 14.400000 16 256.000000 0.251327
57.2957795 51.566202 57.295780 916.732472 0.900000
359 323.100000 359 5744.000000 5.639159
399 359.100000 399 6384.000000 6.267477
6399 359.100000 399 6384.000000 6.267477
1000000 0.000000 0 0.000000 0.000000
 
Mills Degrees Gradians Mils Radians
-2 -0.112500 -0.125000 -2 -0.001963
-1 -0.056250 -0.062500 -1 -0.000982
0 0.000000 0.000000 0 0.000000
1 0.056250 0.062500 1 0.000982
2 0.112500 0.125000 2 0.001963
6.2831853 0.353429 0.392699 6.283185 0.006169
16 0.900000 1.000000 16 0.015708
57.2957795 3.222888 3.580986 57.295780 0.056250
359 20.193750 22.437500 359 0.352447
399 22.443750 24.937500 399 0.391717
6399 359.943750 399.937500 6399 6.282204
1000000 90.000000 100.000000 1600 1.570796
 
Radians Degrees Gradians Mils Radians
-2 -114.591559 -127.323954 -2037.183272 -2.000000
-1 -57.295780 -63.661977 -1018.591636 -1.000000
0 0.000000 0.000000 0.000000 0.000000
1 57.295780 63.661977 1018.591636 1.000000
2 114.591559 127.323954 2037.183272 2.000000
6.2831853 360.000000 400.000000 6399.999993 6.283185
16 196.732472 218.591636 3497.466173 3.433629
57.2957795 42.806349 47.562610 761.001765 0.747112
359 49.184845 54.649828 874.397248 0.858437
399 181.016026 201.128917 3218.062679 3.159326
6399 155.693104 172.992338 2767.877408 2.717357
1000000 339.513082 377.236758 6035.788130 5.925621</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ANGLES_(GEOMETRIC)_NORMALIZATION_AND_CONVERSION.AWK
# gawk starts showing discrepancies at test case number 7 when compared with C#
Line 183 ⟶ 694:
function radian2gradian(angle) { return(angle * 200 / pi) }
function radian2mil(angle) { return(angle * 3200 / pi) }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 248 ⟶ 759:
1000000 5.92562 radian 16042.8 0 1.62975e+006 5.92562 12
</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> MODE 15 REM Suitable sized output window so that all output fits neatly
DEF FNMod(a, b) WHILE a >= b a-=b ENDWHILE =a REM Floating point mod
 
DEF FNd2d(a) = FNMod(a, 360)
DEF FNg2g(a) = FNMod(a, 400)
DEF FNm2m(a) = FNMod(a, 6400)
DEF FNr2r(a) = FNMod(a, 2 * PI)
 
DEF FNd2g(a) = FNd2d(a) * 10 / 9
DEF FNd2m(a) = FNd2d(a) * 160 / 9
DEF FNd2r(a) = RAD(FNd2d(a))
 
DEF FNg2d(a) = FNg2g(a) * 9 / 10
DEF FNg2m(a) = FNg2g(a) * 16
DEF FNg2r(a) = FNg2g(a) * PI / 200
 
DEF FNm2d(a) = FNm2m(a) * 9 / 160
DEF FNm2g(a) = FNm2m(a) / 16
DEF FNm2r(a) = FNm2m(a) * PI / 3200
 
DEF FNr2d(a) = DEG(FNr2r(a))
DEF FNr2g(a) = FNr2r(a) * 200 / PI
DEF FNr2m(a) = FNr2r(a) * 3200 / PI
 
@%=&02070F
Dash75$=STRING$(75, "-")
 
PRINT TAB(0, 0) " Degrees Normalized Gradians Mils Radians"'Dash75$
PRINT TAB(0, 15) " Gradians Normalized Degrees Mils Radians"'Dash75$
PRINT TAB(0, 30) " Mils Normalized Degrees Gradians Radians"'Dash75$
PRINT TAB(0, 45) " Radians Normalized Degrees Gradians Mils "'Dash75$
 
FOR I%=1 TO 12
READ Angle
PRINT TAB(0, 1+I%) Angle, FNd2d(Angle), FNd2g(Angle), FNd2m(Angle), FNd2r(Angle)
PRINT TAB(0, 16+I%) Angle, FNg2g(Angle), FNg2d(Angle), FNg2m(Angle), FNg2r(Angle)
PRINT TAB(0, 31+I%) Angle, FNm2m(Angle), FNm2d(Angle), FNm2g(Angle), FNm2r(Angle)
PRINT TAB(0, 46+I%) Angle, FNr2r(Angle), FNr2d(Angle), FNr2g(Angle), FNr2m(Angle)
NEXT
 
DATA -2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000</syntaxhighlight>
{{Out}}
<pre> Degrees Normalized Gradians Mils Radians
---------------------------------------------------------------------------
-2.0000000 -2.0000000 -2.2222222 -35.5555556 -0.0349066
-1.0000000 -1.0000000 -1.1111111 -17.7777778 -0.0174533
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 1.1111111 17.7777778 0.0174533
2.0000000 2.0000000 2.2222222 35.5555556 0.0349066
6.2831853 6.2831853 6.9813170 111.7010720 0.1096623
16.0000000 16.0000000 17.7777778 284.4444444 0.2792527
57.2957795 57.2957795 63.6619772 1018.5916356 1.0000000
359.0000000 359.0000000 398.8888889 6382.2222222 6.2657320
399.0000000 39.0000000 43.3333333 693.3333333 0.6806784
6399.0000000 279.0000000 310.0000000 4960.0000000 4.8694686
1000000.0000000 280.0000000 311.1111111 4977.7777778 4.8869219
 
Gradians Normalized Degrees Mils Radians
---------------------------------------------------------------------------
-2.0000000 -2.0000000 -1.8000000 -32.0000000 -0.0314159
-1.0000000 -1.0000000 -0.9000000 -16.0000000 -0.0157080
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 0.9000000 16.0000000 0.0157080
2.0000000 2.0000000 1.8000000 32.0000000 0.0314159
6.2831853 6.2831853 5.6548668 100.5309648 0.0986960
16.0000000 16.0000000 14.4000000 256.0000000 0.2513274
57.2957795 57.2957795 51.5662016 916.7324720 0.9000000
359.0000000 359.0000000 323.1000000 5744.0000000 5.6391588
399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
6399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
1000000.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 
Mils Normalized Degrees Gradians Radians
---------------------------------------------------------------------------
-2.0000000 -2.0000000 -0.1125000 -0.1250000 -0.0019635
-1.0000000 -1.0000000 -0.0562500 -0.0625000 -0.0009817
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 0.0562500 0.0625000 0.0009817
2.0000000 2.0000000 0.1125000 0.1250000 0.0019635
6.2831853 6.2831853 0.3534292 0.3926991 0.0061685
16.0000000 16.0000000 0.9000000 1.0000000 0.0157080
57.2957795 57.2957795 3.2228876 3.5809862 0.0562500
359.0000000 359.0000000 20.1937500 22.4375000 0.3524474
399.0000000 399.0000000 22.4437500 24.9375000 0.3917173
6399.0000000 6399.0000000 359.9437500 399.9375000 6.2822036
1000000.0000000 1600.0000000 90.0000000 100.0000000 1.5707963
 
Radians Normalized Degrees Gradians Mils
---------------------------------------------------------------------------
-2.0000000 -2.0000000 -114.5915590 -127.3239545 -2037.1832716
-1.0000000 -1.0000000 -57.2957795 -63.6619772 -1018.5916358
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 57.2957795 63.6619772 1018.5916358
2.0000000 2.0000000 114.5915590 127.3239545 2037.1832716
6.2831853 6.2831853 359.9999996 399.9999995 6399.9999927
16.0000000 3.4336294 196.7324722 218.5916358 3497.4661726
57.2957795 0.7471117 42.8063493 47.5626103 761.0017647
359.0000000 0.8584375 49.1848452 54.6498280 874.3972479
399.0000000 3.1593256 181.0160257 201.1289175 3218.0626795
6399.0000000 2.7173573 155.6931042 172.9923380 2767.8774082
1000000.0000000 5.9256211 339.5130824 377.2367582 6035.7881314</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#define PI 3.141592653589793
#define TWO_PI 6.283185307179586
 
Line 288 ⟶ 903:
double rad2deg(double a) {return a * 180 / PI;}
double rad2grad(double a) {return a * 200 / PI;}
double rad2mil(double a) {return a * 3200 / PI;}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
public static class Angles
Line 354 ⟶ 969:
public static double RadToGrad(double angle) => angle * 200 / Math.PI;
public static double RadToMil(double angle) => angle * 3200 / Math.PI;
}</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 405 ⟶ 1,020:
=={{header|C++}}==
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <functional>
#include <iostream>
#include <iomanip>
Line 472 ⟶ 1,087:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> ┌───────────────────┐
Line 512 ⟶ 1,127:
or (angles) for auto-test.
VAL* is unnormalized.
<langsyntaxhighlight lang="lisp">(defun DegToDeg (a) (rem a 360))
(defun GradToGrad (a) (rem a 400))
(defun MilToMil (a) (rem a 6400))
Line 540 ⟶ 1,155:
(format t "Grad | ~15f | ~15f | ~15f | ~15f | ~15f~%" a (GradToDeg a) (GradToGrad a) (GradToMil a) (GradToRad a))
(format t "Mil | ~15f | ~15f | ~15f | ~15f | ~15f~%" a (MilToDeg a) (MilToGrad a) (MilToMil a) (MilToRad a))
(format t "Rad | ~15f | ~15f | ~15f | ~15f | ~15f~%~%" a (RadToDeg a) (RadToGrad a) (RadToMil a) (RadToRad a))))</langsyntaxhighlight>
{{out}}
<pre>[CLISP]> (angles)
Line 614 ⟶ 1,229:
Mil | 1000000.0 | 90.0 | 100.0 | 1600.0 | 1.5707963267949
Rad | 1000000.0 | 339.51308232088 | 377.23675813431 | 6035.7881301489 | 5.9256211400939</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.Math}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program normalization_and_conversion;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils,
System.Math;
 
function d2d(d: double): double;
begin
result := FMod(d, 360);
end;
 
function g2g(g: double): double;
begin
result := FMod(g, 400);
end;
 
function m2m(m: double): double;
begin
result := FMod(m, 6400);
end;
 
function r2r(r: double): double;
begin
result := FMod(r, 2 * Pi);
end;
 
function d2g(d: double): double;
begin
result := d2d(d) * 400 / 360;
end;
 
function d2m(d: double): double;
begin
result := d2d(d) * 6400 / 360;
end;
 
function d2r(d: double): double;
begin
result := d2d(d) * Pi / 180;
end;
 
function g2d(g: double): double;
begin
result := g2g(g) * 360 / 400;
end;
 
function g2m(g: double): double;
begin
result := g2g(g) * 6400 / 400;
end;
 
function g2r(g: double): double;
begin
result := g2g(g) * Pi / 200;
end;
 
function m2d(m: double): double;
begin
result := m2m(m) * 360 / 6400;
end;
 
function m2g(m: double): double;
begin
result := m2m(m) * 400 / 6400;
end;
 
function m2r(m: double): double;
begin
result := m2m(m) * Pi / 3200;
end;
 
function r2d(r: double): double;
begin
result := r2r(r) * 180 / Pi;
end;
 
function r2g(r: double): double;
begin
result := r2r(r) * 200 / Pi;
end;
 
function r2m(r: double): double;
begin
result := r2r(r) * 3200 / Pi;
end;
 
function s(f: double): string;
begin
var wf := FloatToStrF(f, ffGeneral, 16, 64).Split([FormatSettings.DecimalSeparator], TStringSplitOptions.ExcludeEmpty);
if Length(wf) = 1 then
exit(format('%7s ', [wf[0]]));
var le := length(wf[1]);
if le > 7 then
le := 7;
Result := format('%7s.%-7s', [wf[0], copy(wf[1], 0, le)]);
end;
 
begin
var angles: TArray<Double> := [-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359,
399, 6399, 1000000];
 
var ft := '%s %s %s %s %s';
 
writeln(format(ft, [' degrees ', 'normalized degs', ' gradians ',
' mils ', ' radians']));
for var a in angles do
writeln(format(ft, [s(a), s(d2d(a)), s(d2g(a)), s(d2m(a)), s(d2r(a))]));
 
writeln(format(ft, [#10' gradians ', 'normalized grds', ' degrees ',
' mils ', ' radians']));
 
for var a in angles do
writeln(format(ft, [s(a), s(g2g(a)), s(g2d(a)), s(g2m(a)), s(g2r(a))]));
 
writeln(format(ft, [#10' mils ', 'normalized mils', ' degrees ',
' gradians ', ' radians']));
for var a in angles do
writeln(format(ft, [s(a), s(m2m(a)), s(m2d(a)), s(m2g(a)), s(m2r(a))]));
 
writeln(format(ft, [#10' radians ', 'normalized rads', ' degrees ',
' gradians ', ' mils ']));
for var a in angles do
writeln(format(ft, [s(a), s(r2r(a)), s(r2d(a)), s(r2g(a)), s(r2m(a))]));
readln;
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
func angconv ang f$ t$ .
sgn = sign ang
ang = abs ang
if f$ = "degree"
turn = ang / 360 mod 1
elif f$ = "gradian"
turn = ang / 400 mod 1
elif f$ = "mil"
turn = ang / 6400 mod 1
elif f$ = "radian"
turn = ang / (2 * pi) mod 1
.
if t$ = "degree"
ang = turn * 360
elif t$ = "gradian"
ang = turn * 400
elif t$ = "mil"
ang = turn * 6400
elif t$ = "radian"
ang = turn * 2 * pi
.
return ang * sgn
.
func$ fmt s$ .
return substr " " 1 (9 - len s$) & s$ & " "
.
#
scales$[] = [ "degree" "gradian" "mil" "radian" ]
values[] = [ -2 -1 0 1 2 6.2831853 16 57.2957795 359 399 6399 1000000 ]
numfmt 3 10
for f$ in scales$[]
write fmt f$
for t$ in scales$[]
write fmt t$
.
print ""
print " ------------------------------------------------"
for v in values[]
write v
for t$ in scales$[]
write angconv v f$ t$
.
print ""
.
print ""
.
</syntaxhighlight>
{{out}}
<pre>
degree degree gradian mil radian
------------------------------------------------
-2 -2 -2.222 -35.556 -0.035
-1 -1 -1.111 -17.778 -0.017
0 0 0 0 0
1 1 1.111 17.778 0.017
2 2 2.222 35.556 0.035
6.283 6.283 6.981 111.701 0.110
16 16 17.778 284.444 0.279
57.296 57.296 63.662 1018.592 1.000
359 359 398.889 6382.222 6.266
399 39.000 43.333 693.333 0.681
6399 279.000 310.000 4960.000 4.869
1000000 280.000 311.111 4977.778 4.887
 
gradian degree gradian mil radian
------------------------------------------------
-2 -1.800 -2 -32 -0.031
-1 -0.900 -1 -16 -0.016
0 0 0 0 0
1 0.900 1 16 0.016
2 1.800 2 32 0.031
6.283 5.655 6.283 100.531 0.099
16 14.400 16 256 0.251
57.296 51.566 57.296 916.732 0.900
359 323.100 359 5744 5.639
399 359.100 399 6384 6.267
6399 359.100 399.000 6384.000 6.267
1000000 0 0 0 0
 
mil degree gradian mil radian
------------------------------------------------
-2 -0.113 -0.125 -2 -0.002
-1 -0.056 -0.062 -1 -0.001
0 0 0 0 0
1 0.056 0.062 1 0.001
2 0.113 0.125 2 0.002
6.283 0.353 0.393 6.283 0.006
16 0.900 1 16 0.016
57.296 3.223 3.581 57.296 0.056
359 20.194 22.438 359 0.352
399 22.444 24.938 399 0.392
6399 359.944 399.938 6399 6.282
1000000 90 100 1600 1.571
 
radian degree gradian mil radian
------------------------------------------------
-2 -114.592 -127.324 -2037.183 -2
-1 -57.296 -63.662 -1018.592 -1
0 0 0 0 0
1 57.296 63.662 1018.592 1
2 114.592 127.324 2037.183 2
6.283 360.000 400.000 6400.000 6.283
16 196.732 218.592 3497.466 3.434
57.296 42.806 47.563 761.002 0.747
359 49.185 54.650 874.397 0.858
399 181.016 201.129 3218.063 3.159
6399 155.693 172.992 2767.877 2.717
1000000 339.513 377.237 6035.788 5.926
</pre>
 
=={{header|Factor}}==
Radians and degrees are already defined in the <code>units.si</code> vocabulary. Gradiens and mils are defined in terms of degrees. Conversions from unit to unit are handled by inverse functions; <code>[undo]</code> knows how to deconstruct units in terms of other units. (Assuming, of course, new units are defined entirely with words that have inverses.)
<langsyntaxhighlight lang="factor">USING: accessors combinators formatting inverse kernel math
math.constants quotations qw sequences units.si ;
IN: rosetta-code.angles
Line 641 ⟶ 1,500:
units [ .row ] cartesian-each ;
 
MAIN: angles</langsyntaxhighlight>
{{out}}
<pre>
Line 694 ⟶ 1,553:
1000000 radians 339.5131 377.2368 6035.7881 5.9256
</pre>
 
=={{header|FreeBASIC}}==
Provides a single function that does the normalising and converting. Supports D, G, M, R, T for degrees, gradians, mils, radians, and turns respectively.
<syntaxhighlight lang="freebasic">#define PI 3.1415926535897932384626433832795028842
#define INVALID -99999
 
function clamp( byval n as double, lo as double, hi as double ) as double
while n <= lo
n += (hi - lo)/2
wend
while n >= hi
n += (lo - hi)/2
wend
return n
end function
 
function anglenc( byval angle as double, byval source as string, byval targ as string ) as double
source = ucase(source)
targ = ucase(targ)
select case source
case "D":
angle = clamp(angle, -360, 360)
select case targ
case "D":
return angle
case "G":
return angle*10/9
case "M":
return angle*160/9
case "R":
return angle*PI/180
case "T":
return angle/360
case else
return INVALID
end select
case "G":
angle = clamp(angle, -400, 400)
select case targ
case "D":
return angle*9/10
case "G":
return angle
case "M":
return angle*16
case "R":
return angle*PI/200
case "T":
return angle/400
case else
return INVALID
end select
case "M":
angle = clamp(angle, -6400, 6400)
select case targ
case "D":
return angle*9/160
case "G":
return angle/16
case "M":
return angle
case "R":
return angle*PI/3200
case "T":
return angle/6400
case else
return INVALID
end select
case "R":
angle = clamp(angle, -2*PI, 2*PI)
select case targ
case "D":
return angle*180/PI
case "G":
return angle*200/PI
case "M":
return angle*3200/PI
case "R":
return angle
case "T":
return angle/(2*PI)
case else
return INVALID
end select
case "T":
angle = clamp(angle, -1, 1)
select case targ
case "D":
return angle*360
case "G":
return angle*400
case "M":
return angle*6400
case "R":
return angle*2*PI
case "T":
return angle
case else
return INVALID
end select
case else:
return INVALID
end select
end function
 
function clip( st as string, num as uinteger ) as string
if len(st)<num then return st
return left(st, num)
end function
 
dim as string scales = "DGMRT", source, targ
dim as double angles(12) = {-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000}
print "Angle Normalised Unit | D G M R T"
for k as ubyte = 0 to 11
for i as ubyte = 1 to 5
source = mid(scales,i,1)
print angles(k), clip(str(anglenc(angles(k), source, source )), 10), source, "|",
for j as ubyte = 1 to 5
targ = mid(scales, j, 1)
print clip(str(anglenc(angles(k), source, targ )), 10),
next j
print
next i
next k
</syntaxhighlight>
{{out}}
<pre>
Angle Normalised Unit | D G M R T
-2 -2 D | -2 -2.2222222 -35.555555 -0.0349065 -0.0055555
-2 -2 G | -1.8 -2 -32 -0.0314159 -0.005
-2 -2 M | -0.1125 -0.125 -2 -0.0019634 -0.0003125
-2 -2 R | -114.59155 -127.32395 -2037.1832 -2 -0.3183098
-2 0 T | 0 0 0 0 0
-1 -1 D | -1 -1.1111111 -17.777777 -0.0174532 -0.0027777
-1 -1 G | -0.9 -1 -16 -0.0157079 -0.0025
-1 -1 M | -0.05625 -0.0625 -1 -0.0009817 -0.0001562
-1 -1 R | -57.295779 -63.661977 -1018.5916 -1 -0.1591549
-1 0 T | 0 0 0 0 0
0 0 D | 0 0 0 0 0
0 0 G | 0 0 0 0 0
0 0 M | 0 0 0 0 0
0 0 R | 0 0 0 0 0
0 0 T | 0 0 0 0 0
1 1 D | 1 1.11111111 17.7777777 0.01745329 0.00277777
1 1 G | 0.9 1 16 0.01570796 0.0025
1 1 M | 0.05625 0.0625 1 0.00098174 0.00015625
1 1 R | 57.2957795 63.6619772 1018.59163 1 0.15915494
1 0 T | 0 0 0 0 0
2 2 D | 2 2.22222222 35.5555555 0.03490658 0.00555555
2 2 G | 1.8 2 32 0.03141592 0.005
2 2 M | 0.1125 0.125 2 0.00196349 0.0003125
2 2 R | 114.591559 127.323954 2037.18327 2 0.31830988
2 0 T | 0 0 0 0 0
6.2831853 6.2831853 D | 6.2831853 6.98131700 111.701072 0.10966227 0.01745329
6.2831853 6.2831853 G | 5.65486677 6.2831853 100.530964 0.09869604 0.01570796
6.2831853 6.2831853 M | 0.35342917 0.39269908 6.2831853 0.00616850 0.00098174
6.2831853 6.2831853 R | 359.999999 399.999999 6399.99999 6.2831853 0.99999999
6.2831853 0.28318530 T | 101.946708 113.274120 1812.38592 1.77930571 0.28318530
16 16 D | 16 17.7777777 284.444444 0.27925268 0.04444444
16 16 G | 14.4 16 256 0.25132741 0.04
16 16 M | 0.9 1 16 0.01570796 0.0025
16 3.43362938 R | 196.732472 218.591635 3497.46617 3.43362938 0.54647908
16 0 T | 0 0 0 0 0
57.2957795 57.2957795 D | 57.2957795 63.6619772 1018.59163 0.99999999 0.15915494
57.2957795 57.2957795 G | 51.5662015 57.2957795 916.732472 0.89999999 0.14323944
57.2957795 57.2957795 M | 3.22288759 3.58098621 57.2957795 0.05624999 0.00895246
57.2957795 0.74711173 R | 42.8063492 47.5626102 761.001764 0.74711173 0.11890652
57.2957795 0.29577950 T | 106.480620 118.311800 1892.98880 1.85843740 0.29577950
359 359 D | 359 398.888888 6382.22222 6.26573201 0.99722222
359 359 G | 323.1 359 5744 5.63915881 0.8975
359 359 M | 20.19375 22.4375 359 0.35244742 0.05609375
359 0.85843749 R | 49.1848451 54.6498279 874.397247 0.85843749 0.13662456
359 0 T | 0 0 0 0 0
399 39 D | 39 43.3333333 693.333333 0.68067840 0.10833333
399 399 G | 359.1 399 6384 6.26747734 0.99750000
399 399 M | 22.44375 24.9375 399 0.39171733 0.06234375
399 3.15932564 R | 181.016025 201.128917 3218.06267 3.15932564 0.50282229
399 0 T | 0 0 0 0 0
6399 279 D | 279 310 4960 4.86946861 0.775
6399 399 G | 359.1 399 6384 6.26747734 0.99750000
6399 6399 M | 359.94375 399.9375 6399 6.28220355 0.99984375
6399 2.71735729 R | 155.693104 172.992338 2767.87740 2.71735729 0.43248084
6399 0 T | 0 0 0 0 0
1000000 280 D | 280 311.111111 4977.77777 4.88692190 0.77777777
1000000 0 G | 0 0 0 0 0
1000000 1600 M | 90 100 1600 1.57079632 0.25
1000000 5.92562252 R | 339.513161 377.236846 6035.78954 5.92562252 0.94309211
1000000 0 T | 0 0 0 0 0</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 770 ⟶ 1,817:
fmt.Printf(ft, s(a), s(r2r(a)), s(r2d(a)), s(r2g(a)), s(r2m(a)))
}
}</langsyntaxhighlight>
 
{{out}}
Line 833 ⟶ 1,880:
=={{header|Groovy}}==
This solution creates the concept of an angular quantity with subclasses for different units of measure. Rather than creating individual functions (d2d, r2m, etc.) this solution uses inheritance, polymorphism, and operator overloading to implement the conversions and comparisons in a relatively natural way for idiomatic Groovy.
<langsyntaxhighlight lang="groovy">import java.lang.reflect.Constructor
 
abstract class Angle implements Comparable<? extends Angle> {
Line 902 ⟶ 1,949:
String unitName() { UNIT }
Radians(Number value = 0) { super(value) }
}</langsyntaxhighlight>
 
This category class allows Angles to interoperate more easily with Numbers:
<langsyntaxhighlight lang="groovy">class AngleCategory {
static Degrees getDeg(Number n) { new Degrees(n) }
static Gradians getGrad(Number n) { new Gradians(n) }
static Mils getMil(Number n) { new Mils(n) }
static Radians getRad(Number n) { new Radians(n) }
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">Number.metaClass.mixin AngleCategory
 
[ -2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000 ].each { rawAngle ->
Line 929 ⟶ 1,976:
assert 360.deg == 0.deg
assert 90.deg == 100.grad
assert Math.PI.rad == 3200.mil</langsyntaxhighlight>
 
{{out}}
Line 1,003 ⟶ 2,050:
1000000 1600.00000000 mil 90.00000000º 100.00000000 grad 1600.00000000 mil 1.57079633 rad
1000000 5.92562114 rad 339.51308232º 377.23675814 grad 6035.78813019 mil 5.92562114 rad</pre>
 
=={{header|Haskell}}==
The solution of this seemingly trivial task could be elegantly done by type classes. Each angle unit is represented as a distinct type, preventing from implicit combination of different units. Moreover, adding new units doesn't imply writing new transformers or normalizers.
 
Isomorphims between all angular types are defined via representation as turns, according to the fact that they all form the same topological space, isomorphic to open interval (-1, 1).
 
<syntaxhighlight lang="haskell">{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Text.Printf
class (Num a, Fractional a, RealFrac a) => Angle a where
fullTurn :: a -- value of the whole turn
mkAngle :: Double -> a
value :: a -> Double
fromTurn :: Double -> a
toTurn :: a -> Double
normalize :: a -> a
 
-- conversion of angles to rotations in linear case
fromTurn t = angle t * fullTurn
toTurn a = value $ a / fullTurn
 
-- normalizer for linear angular unit
normalize a = a `modulo` fullTurn
where
modulo x r | x == r = r
| x < 0 = signum x * abs x `modulo` r
| x >= 0 = x - fromInteger (floor (x / r)) * r
 
-- smart constructor
angle :: Angle a => Double -> a
angle = normalize . mkAngle
 
-- Two transformers differ only in the order of type application.
from :: forall a b. (Angle a, Angle b) => a -> b
from = fromTurn . toTurn
 
to :: forall b a. (Angle a, Angle b) => a -> b
to = fromTurn . toTurn</syntaxhighlight>
 
'''Instances of different angular units.'''
 
<syntaxhighlight lang="haskell">-- radians
newtype Rad = Rad Double
deriving (Eq, Ord, Num, Real, Fractional, RealFrac, Floating)
instance Show Rad where
show (Rad 0) = printf "∠0"
show (Rad r) = printf "∠%.3f" r
instance Angle Rad where
fullTurn = Rad 2*pi
mkAngle = Rad
value (Rad r) = r
-- degrees
newtype Deg = Deg Double
deriving (Eq, Ord, Num, Real, Fractional, RealFrac, Floating)
instance Show Deg where
show (Deg 0) = printf "0°"
show (Deg d) = printf "%.3g°" d
instance Angle Deg where
fullTurn = Deg 360
mkAngle = Deg
value (Deg d) = d
-- grads
newtype Grad = Grad Double
deriving (Eq, Ord, Num, Real, Fractional, RealFrac, Floating)
instance Show Grad where
show (Grad 0) = printf "0g"
show (Grad g) = printf "%.3gg" g
instance Angle Grad where
fullTurn = Grad 400
mkAngle = Grad
value (Grad g) = g
-- mils
newtype Mil = Mil Double
deriving (Eq, Ord, Num, Real, Fractional, RealFrac, Floating)
instance Show Mil where
show (Mil 0) = printf "0m"
show (Mil m) = printf "%.3gm" m
instance Angle Mil where
fullTurn = Mil 6400
mkAngle = Mil
value (Mil m) = m
 
 
-- example of non-linear angular unit
newtype Slope = Slope Double
deriving (Eq, Ord, Num, Real, Fractional, RealFrac, Floating)
instance Show Slope where
show (Slope 0) = printf "0%"
show (Slope m) = printf "%.g" (m * 100) ++ "%"
instance Angle Slope where
fullTurn = undefined
mkAngle = Slope
value (Slope t) = t
toTurn = toTurn @Rad . angle . atan . value
fromTurn = angle . tan . value . fromTurn @Rad
normalize = id</syntaxhighlight>
 
'''Examples'''
 
<pre>λ> angle 45 :: Deg
45.000°
 
λ> angle pi :: Rad
∠3.141
 
λ> angle 1000 :: Gon
200.000g
 
λ> normalize 450 :: Deg
90.000°
 
λ> normalize 450 :: Gon
50.000g
 
λ> (from :: Gon -> Rad) 100
∠1.571
 
λ> (from :: Rad -> Gon) $ (pi/2)
100.000g
 
λ> (from :: Deg -> Slope) 45
100%
 
λ> (from :: Slope -> Mil) 0.5
472.267m
 
λ> :set -XTypeApplications
 
λ> from @Gon @Deg (angle 100)
90.000°
 
λ> to @Gon (angle @Deg 45)
50.000g</pre>
 
The task assignment shows different possible type annotations.
 
<syntaxhighlight lang="haskell">main = do
let xs = [-2, -1, 0, 1, 2, 6.2831853,
16, 57.2957795, 359, 399, 6399, 1000000]
 
-- using `to` and `angle` with type application
putStrLn "converting to radians"
print $ to @Rad . angle @Rad <$> xs
print $ to @Rad . angle @Deg <$> xs
print $ to @Rad . angle @Grad <$> xs
print $ to @Rad . angle @Mil <$> xs
print $ to @Rad . angle @Slope <$> xs
 
-- using `from` with type application
putStrLn "\nconverting to degrees"
print $ from @Rad @Deg . angle <$> xs
print $ from @Deg @Deg . angle <$> xs
print $ from @Grad @Deg . angle <$> xs
print $ from @Mil @Deg . angle <$> xs
print $ from @Slope @Deg . angle <$> xs
 
-- using normalization for each unit
putStrLn "\nconverting to grads"
print $ to @Grad . normalize . Rad <$> xs
print $ to @Grad . normalize . Deg <$> xs
print $ to @Grad . normalize . Grad <$> xs
print $ to @Grad . normalize . Mil <$> xs
print $ to @Grad . normalize . Slope <$> xs
 
-- using implicit type annotation
putStrLn "\nconverting to mils"
print $ (from :: Rad -> Mil) . angle <$> xs
print $ (from :: Deg -> Mil) . angle <$> xs
print $ (from :: Grad -> Mil) . angle <$> xs
print $ (from :: Mil -> Mil) . angle <$> xs
print $ (from :: Slope -> Mil) . angle <$> xs</syntaxhighlight>
 
<pre>λ> main
converting to radians
[∠-2.000,∠-1.000,∠0,∠1.000,∠2.000,∠6.283,∠3.434,∠0.747,∠0.858,∠3.159,∠2.717,∠5.926]
[∠-0.035,∠-0.017,∠0,∠0.017,∠0.035,∠0.110,∠0.279,∠1.000,∠6.266,∠0.681,∠4.869,∠4.887]
[∠-0.031,∠-0.016,∠0,∠0.016,∠0.031,∠0.099,∠0.251,∠0.900,∠5.639,∠6.267,∠6.267,∠0]
[∠-0.002,∠-0.001,∠0,∠0.001,∠0.002,∠0.006,∠0.016,∠0.056,∠0.352,∠0.392,∠6.282,∠1.571]
[∠-1.107,∠-0.785,∠0,∠0.785,∠1.107,∠1.413,∠1.508,∠1.553,∠1.568,∠1.568,∠1.571,∠1.571]
 
converting to degrees
[-114.592°,-57.296°,0°,57.296°,114.592°,360.000°,196.732°,42.806°,49.185°,181.016°,155.693°,339.513°]
[-2.000°,-1.000°,0°,1.000°,2.000°,6.283°,16.000°,57.296°,359.000°,39.000°,279.000°,280.000°]
[-1.800°,-0.900°,0°,0.900°,1.800°,5.655°,14.400°,51.566°,323.100°,359.100°,359.100°,0°]
[-0.112°,-5.625e-2°,0°,5.625e-2°,0.112°,0.353°,0.900°,3.223°,20.194°,22.444°,359.944°,90.000°]
[-63.435°,-45.000°,0°,45.000°,63.435°,80.957°,86.424°,89.000°,89.840°,89.856°,89.991°,90.000°]
 
converting to grads
[-127.324g,-63.662g,0g,63.662g,127.324g,400.000g,218.592g,47.563g,54.650g,201.129g,172.992g,377.237g]
[-2.222g,-1.111g,0g,1.111g,2.222g,6.981g,17.778g,63.662g,398.889g,43.333g,310.000g,311.111g]
[-2.000g,-1.000g,0g,1.000g,2.000g,6.283g,16.000g,57.296g,359.000g,399.000g,399.000g,0g]
[-0.125g,-6.250e-2g,0g,6.250e-2g,0.125g,0.393g,1.000g,3.581g,22.438g,24.938g,399.938g,100.000g]
[-70.483g,-50.000g,0g,50.000g,70.483g,89.952g,96.026g,98.889g,99.823g,99.840g,99.990g,100.000g]
 
converting to mils
[-2037.183m,-1018.592m,0m,1018.592m,2037.183m,6400.000m,3497.466m,761.002m,874.397m,3218.063m,2767.877m,6035.788m]
[-35.556m,-17.778m,0m,17.778m,35.556m,111.701m,284.444m,1018.592m,6382.222m,693.333m,4960.000m,4977.778m]
[-32.000m,-16.000m,0m,16.000m,32.000m,100.531m,256.000m,916.732m,5744.000m,6384.000m,6384.000m,0m]
[-2.000m,-1.000m,0m,1.000m,2.000m,6.283m,16.000m,57.296m,359.000m,399.000m,6399.000m,1600.000m]
[-1127.732m,-800.000m,0m,800.000m,1127.732m,1439.234m,1536.421m,1582.224m,1597.163m,1597.447m,1599.841m,1599.999m]</pre>
 
=={{header|J}}==
given definitions which convert to and from the unit circle, using verbial power (^:) inverse (^: _1), and adverbs to make the sentences read better? or at least differently from other computer languages:
<syntaxhighlight lang="text">
TAU =: 2p1 NB. tauday.com
 
Line 1,024 ⟶ 2,287:
Mil =: adverb def 'normalize as_mil inv m'
Radian =: adverb def 'normalize as_radian inv m'
</syntaxhighlight>
</lang>
we can compose conversion sentences like
<syntaxhighlight lang="text">
as_degree 100 Gradian
90
</syntaxhighlight>
</lang>
Presuming the following additional definitions:
<syntaxhighlight lang="text">
NAMES =: > turn`degree`gradian`mil`radian
ALL =: as_turn`as_degree`as_gradian`as_mil`as_radian
Line 1,083 ⟶ 2,346:
|radian | _2 _1 0 1 2 6.28319 3.43363 0.747112 0.858437 3.15933 2.71736 5.92562|
+-------+---------------------------------------------------------------------------------------------------+
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.text.DecimalFormat;
 
// Title: Angles (geometric), normalization and conversion
Line 1,187 ⟶ 2,450:
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,241 ⟶ 2,504:
1000000.000000 radians = 339.5131 377.2368 6035.7881 5.9256
</pre>
=={{header|Javascript}}==
<syntaxhighlight lang="javascript">
/*****************************************************************\
| Expects an angle, an origin unit and a unit to convert to, |
| where in/out units are: |
| --------------------------------------------------------------- |
| 'D'/'d' ..... degrees 'M'/'d' ..... mils | |
| 'G'/'g' ..... gradians 'R'/'r' ..... radians |
| --------------------------------------------------------------- |
| example: convert 150 degrees to radians: |
| angleConv(150, 'd', 'r') |
\*****************************************************************/
function angleConv(deg, inp, out) {
inp = inp.toLowerCase();
out = out.toLowerCase();
const D = 360,
G = 400,
M = 6400,
R = 2 * Math.PI;
// normalazation
let minus = (deg < 0); // boolean
deg = Math.abs(deg);
switch (inp) {
case 'd': deg %= D; break;
case 'g': deg %= G; break;
case 'm': deg %= M; break;
case 'r': deg %= R;
}
// we use an internal conversion to Turns (full circle) here
let t;
switch (inp) {
case 'd': t = deg / D; break;
case 'g': t = deg / G; break;
case 'm': t = deg / M; break;
case 'r': t = deg / R;
}
// converting
switch (out) {
case 'd': t *= D; break;
case 'g': t *= G; break;
case 'm': t *= M; break;
case 'r': t *= R;
}
if (minus) return 0 - t;
return t;
}
 
// mass testing
let nums = [-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1e6],
units = 'dgmr'.split(''),
x, y, z;
for (x = 0; x < nums.length; x++) {
for (y = 0; y < units.length; y++) {
document.write(`
<p>
<b>${nums[x]}<sub>${units[y]}</sub></b><br>
`);
for (z = 0; z < units.length; z++)
document.write(`
= ${angleConv(nums[x], units[y], units[z])}<sub>${units[z]}</sub>
`);
}
}
</syntaxhighlight>
Output (without bolds and subs):
{{out}}<pre>
-2d
= -2d = -2.2222222222222223g = -35.55555555555556m = -0.03490658503988659r
 
-2g
= -1.8d = -2g = -32m = -0.031415926535897934r
 
-2m
= -0.1125d = -0.125g = -2m = -0.001963495408493621r
 
-2r
= -114.59155902616465d = -127.32395447351628g = -2037.1832715762605m = -2r
 
-1d
= -1d = -1.1111111111111112g = -17.77777777777778m = -0.017453292519943295r
 
-1g
= -0.9d = -1g = -16m = -0.015707963267948967r
 
-1m
= -0.05625d = -0.0625g = -1m = -0.0009817477042468104r
 
-1r
= -57.29577951308232d = -63.66197723675814g = -1018.5916357881302m = -1r
 
0d
= 0d = 0g = 0m = 0r
 
0g
= 0d = 0g = 0m = 0r
 
0m
= 0d = 0g = 0m = 0r
 
0r
= 0d = 0g = 0m = 0r
 
1d
= 1d = 1.1111111111111112g = 17.77777777777778m = 0.017453292519943295r
 
1g
= 0.9d = 1g = 16m = 0.015707963267948967r
 
1m
= 0.05625d = 0.0625g = 1m = 0.0009817477042468104r
 
1r
= 57.29577951308232d = 63.66197723675814g = 1018.5916357881302m = 1r
 
2d
= 2d = 2.2222222222222223g = 35.55555555555556m = 0.03490658503988659r
 
2g
= 1.8d = 2g = 32m = 0.031415926535897934r
 
2m
= 0.1125d = 0.125g = 2m = 0.001963495408493621r
 
2r
= 114.59155902616465d = 127.32395447351628g = 2037.1832715762605m = 2r
 
6.2831853d
= 6.2831853d = 6.981317000000001g = 111.70107200000001m = 0.10966227099790768r
 
6.2831853g
= 5.654866770000001d = 6.2831853g = 100.5309648m = 0.09869604389811691r
 
6.2831853m
= 0.35342917312500005d = 0.39269908125g = 6.2831853m = 0.006168502743632307r
 
6.2831853r
= 359.99999958864004d = 399.9999995429334g = 6399.999992686934m = 6.2831853r
 
16d
= 16d = 17.77777777777778g = 284.44444444444446m = 0.2792526803190927r
 
16g
= 14.4d = 16g = 256m = 0.25132741228718347r
 
16m
= 0.9d = 1g = 16m = 0.015707963267948967r
 
16r
= 196.73247220931714d = 218.59163578813016g = 3497.4661726100826m = 3.433629385640827r
 
57.2957795d
= 57.2957795d = 63.66197722222222g = 1018.5916355555555m = 0.9999999997716704r
 
57.2957795g
= 51.56620155d = 57.2957795g = 916.732472m = 0.8999999997945033r
 
57.2957795m
= 3.222887596875d = 3.58098621875g = 57.2957795m = 0.056249999987156456r
 
57.2957795r
= 42.80634926218226d = 47.56261029131362g = 761.0017646610179m = 0.7471117353837258r
 
359d
= 359d = 398.8888888888889g = 6382.222222222223m = 6.265732014659643r
 
359g
= 323.09999999999997d = 359g = 5744m = 5.639158813193679r
 
359m
= 20.193749999999998d = 22.4375g = 359m = 0.3524474258246049r
 
359r
= 49.184845196553994d = 54.649827996171105g = 874.3972479387377m = 0.8584374907635848r
 
399d
= 39d = 43.333333333333336g = 693.3333333333334m = 0.6806784082777886r
 
399g
= 359.1d = 399g = 6384m = 6.267477343911637r
 
399m
= 22.44375d = 24.9375g = 399m = 0.39171733399447733r
 
399r
= 181.0160257198469d = 201.12891746649657g = 3218.062679463945m = 3.1593256476860674r
 
6399d
= 279d = 310g = 4960m = 4.869468613064179r
 
6399g
= 359.1d = 399g = 6384m = 6.267477343911637r
 
6399m
= 359.94375d = 399.9375g = 6399m = 6.28220355947534r
 
6399r
= 155.69310421378557d = 172.9923380153173g = 2767.877408245077m = 2.717357291181216r
 
1000000d
= 280d = 311.11111111111114g = 4977.777777777778m = 4.886921905584122r
 
1000000g
= 0d = 0g = 0m = 0r
 
1000000m
= 90d = 100g = 1600m = 1.5707963267948966r
 
1000000r
= 339.51308232311027d = 377.2367581367892g = 6035.788130188627m = 5.925621140132833r
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren]]'''
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
'''Preliminaries'''
<syntaxhighlight lang="jq">### Formatting
 
# Right-justify but do not truncate
def rjustify(n):
tostring | length as $length | if n <= $length then . else " " * (n-$length) + . end;
 
# Attempt to align decimals so integer part is in a field of width n
def align($n):
tostring
| index(".") as $ix
| if $ix
then if $n < $ix then .
elif $ix then (.[0:$ix]|rjustify($n)) +.[$ix:]
else rjustify($n)
end
else . + ".0" | align($n)
end ;
 
# number of decimal places ($n>=0)
def fround($n):
pow(10;$n) as $p
| (. * $p | round ) / $p
| tostring
| index(".") as $ix
| ("0" * $n) as $zeros
| if $ix then . + $zeros | .[0 : $ix + $n + 1]
else . + "." + $zeros
end;
 
def hide_trailing_zeros:
tostring
| (capture("(?<x>[^.]*[.].)(?<y>00*$)") // null) as $capture
| if $capture
then $capture | (.x + (.y|gsub("0"; " ")))
else .
end;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang="jq"># Normalization as per the task requirements
def mod($y):
(if . < 0 then -$y else 0 end) as $adjust
| $adjust + . - ($y * ((. / $y)|floor));
 
def pi: (1|atan) * 4;
 
def d2d: mod(360);
def g2g: mod(400);
def m2m: mod(6400);
def r2r: mod(2*pi);
def d2g: d2d * 400 / 360;
def d2m: d2d * 6400 / 360;
def d2r: d2d * pi / 180;
def g2d: g2g * 360 / 400;
def g2m: g2g * 6400 / 400;
def g2r: g2g * pi / 200;
def m2d: m2m * 360 / 6400;
def m2g: m2m * 400 / 6400;
def m2r: m2m * pi / 3200;
def r2d: r2r * 180 / pi;
def r2g: r2r * 200 / pi;
def r2m: r2r * 3200 / pi;
 
def f1(a;b;c;d;e): "\(a|lpad(15)) \(b|lpad(15)) \(c|lpad(15)) \(d|lpad(15)) \(e|lpad(15))";
 
def f2(a;b;c;d;e):
def al: fround(7) | align(10)| hide_trailing_zeros;
"\(a|al) \(b|al) \(c|al) \(d|al) \(e|al)";
 
def angles: [-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000];
 
def task1:
f1( "degrees"; "normalized degs"; "gradians"; "mils"; "radians"),
(angles[]
| f2(.; d2d; d2g; d2m; d2r)) ;
 
def task2:
f1("gradians"; "normalized grds"; "degrees"; "mils"; "radians"),
(angles[]
| f2(.; g2g; g2d; g2m; g2r));
 
def task3:
f1("mils"; "normalized mils"; "degrees"; "gradians"; "radians"),
(angles[]
| f2(.; m2m; m2d; m2g; m2r));
 
def task4:
f1( "radians"; "normalized rads"; "degrees"; "gradians"; "mils"),
( angles[]
| f2(.; r2r; r2d; r2g; r2m) );
 
task1, "",
task2, "",
task3, "",
task4</syntaxhighlight>
{{out}}
<pre>
As for [[#Wren]]
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Formatting
 
d2d(d) = d % 360
Line 1,282 ⟶ 2,863:
 
testconversions([-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000])
</langsyntaxhighlight>{{out}}
<pre>
Number Units Degrees Gradians Mils Radians
Line 1,334 ⟶ 2,915:
1000000 radians 339.5130823 377.2367581 6035.7881302 5.9256211
</pre>
 
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.text.DecimalFormat as DF
 
const val DEGREE = 360.0
Line 1,390 ⟶ 2,970:
println("%15s %8s = %10s %10s %10s %10s".format(fa.format(a), units, fc.format(d), fc.format(g), fc.format(m), fc.format(r)))
}
}</langsyntaxhighlight>
{{out}}
See Java output
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">range = { degrees=360, gradians=400, mils=6400, radians=2.0*math.pi }
function convert(value, fromunit, tounit)
return math.fmod(value * range[tounit] / range[fromunit], range[tounit])
Line 1,411 ⟶ 2,991:
print(string.format("%15.7f %8s = %15.7f %15.7f %15.7f %15.7f", value, fromunit, d, g, m, r))
end
end</langsyntaxhighlight>
Optionally:
<langsyntaxhighlight lang="lua">-- if you care..
assert(convert(-360,"degrees","degrees") == 0.0)
assert(convert(360,"degrees","degrees") == 0.0)
Line 1,439 ⟶ 3,019:
function r2g(n) return convert(n,"radians","gradians") end
function r2m(n) return convert(n,"radians","mils") end
function r2r(n) return convert(n,"radians","radians") end</langsyntaxhighlight>
{{out}}
<pre> VALUE UNIT = DEGREES GRADIANS MILS RADIANS
Line 1,490 ⟶ 3,070:
1000000.0000000 mils = 90.0000000 100.0000000 1600.0000000 1.5707963
1000000.0000000 radians = 339.5130823 377.2367581 6035.7881302 5.9256211</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[NormalizeAngle, NormalizeDegree, NormalizeGradian, NormalizeMil, NormalizeRadian]
NormalizeAngle[d_, full_] := Module[{a = d},
If[Abs[a/full] > 4,
a = a - Sign[a] full (Quotient[Abs[a], full] - 4)
];
While[a < -full, a += full];
While[a > full, a -= full];
a
]
ClearAll[Degree2Gradian, Degree2Mil, Degree2Radian, Gradian2Degree, Gradian2Mil, Gradian2Radian, Mil2Degree, Mil2Gradian, Mil2Radian, Radian2Degree, Radian2Gradian, Radian2Mil]
NormalizeDegree[d_] := NormalizeAngle[d, 360]
NormalizeGradian[d_] := NormalizeAngle[d, 400]
NormalizeMil[d_] := NormalizeAngle[d, 6400]
NormalizeRadian[d_] := NormalizeAngle[d, 2 Pi]
Degree2Gradian[d_] := d 400/360
Degree2Mil[d_] := d 6400/360
Degree2Radian[d_] := d Pi/180
Gradian2Degree[d_] := d 360/400
Gradian2Mil[d_] := d 6400/400
Gradian2Radian[d_] := d 2 Pi/400
Mil2Degree[d_] := d 360/6400
Mil2Gradian[d_] := d 400/6400
Mil2Radian[d_] := d 2 Pi/6400
Radian2Degree[d_] := d 180/Pi
Radian2Gradian[d_] := d 400/(2 Pi)
Radian2Mil[d_] := d 6400/(2 Pi)
MapThread[Construct, {{Degree2Gradian, Degree2Mil, Degree2Radian,
Gradian2Degree, Gradian2Mil, Gradian2Radian, Mil2Degree,
Mil2Gradian, Mil2Radian, Radian2Degree, Radian2Gradian,
Radian2Mil}, {-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399,
6399, 1000000}}]</syntaxhighlight>
{{out}}
<pre>{-(20/9),-(160/9),0,9/10,32,0.098696,9/10,3.58099,(359 \[Pi])/3200,71820/\[Pi],1279800/\[Pi],3200000000/\[Pi]}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math
import strformat
 
Line 1,543 ⟶ 3,158:
echo "———————————————————————————————————————————————————————————————————————————————————"
for val in Values:
echo fmt"{val:15.7f} {r2r(val):15.7f} {r2d(val):15.7f} {r2g(val):15.7f} {r2m(val):15.7f}"</langsyntaxhighlight>
 
{{out}}
Line 1,608 ⟶ 3,223:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,641 ⟶ 3,256:
printf '%10g %-8s' . '%12g'x4 . "\n", $angle, ${$from}{name}, @results;
}
}</langsyntaxhighlight>
{{out}}
<pre> Angle Unit Degrees Gradians Mills Radians
Line 1,707 ⟶ 3,322:
=={{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
<!--<syntaxhighlight lang="phix">-->
<lang Phix>constant units = {"degrees","gradians","mils","radians"},
<span style="color: #008080;">constant</span> <span style="color: #000000;">units</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"degrees"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"gradians"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"mils"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"radians"</span><span style="color: #0000FF;">},</span>
turns = {1/360,1/400,1/6400,0.5/PI}
<span style="color: #000000;">turns</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">360</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">400</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">6400</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">/</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">}</span>
 
function convert(atom a, integer fdx, tdx)
<span style="color: #008080;">function</span> <span style="color: #000000;">convert</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">fdx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tdx</span><span style="color: #0000FF;">)</span>
return remainder(a*turns[fdx],1)/turns[tdx]
<span style="color: #008080;">return</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">turns</span><span style="color: #0000FF;">[</span><span style="color: #000000;">fdx</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">turns</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tdx</span><span style="color: #0000FF;">]</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
constant tests = {-2,-1,0,1,2,2*PI,16,57.2957795,359,399,6399,1000000}
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">57.2957795</span><span style="color: #0000FF;">,</span><span style="color: #000000;">359</span><span style="color: #0000FF;">,</span><span style="color: #000000;">399</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6399</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000000</span><span style="color: #0000FF;">}</span>
printf(1," angle unit %9s %9s %9s %9s\n",units)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" angle unit %9s %9s %9s %9s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">units</span><span style="color: #0000FF;">)</span>
for i=1 to length(tests) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for fdx=1 to length(units) do
<span style="color: #008080;">for</span> <span style="color: #000000;">fdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">units</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
printf(1,"%9g %-8s",{tests[i],units[fdx]})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%9g %-8s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">units</span><span style="color: #0000FF;">[</span><span style="color: #000000;">fdx</span><span style="color: #0000FF;">]})</span>
for tdx=1 to length(units) do
<span style="color: #008080;">for</span> <span style="color: #000000;">tdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">units</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
printf(1," %9g",convert(tests[i],fdx,tdx))
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %9g"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">convert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">fdx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tdx</span><span style="color: #0000FF;">))</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
puts(1,"\n")
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
puts(1,"\n")
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,791 ⟶ 3,408:
 
=={{header|Python}}==
===Python: Original===
<lang python>PI = 3.141592653589793
<syntaxhighlight lang="python">PI = 3.141592653589793
TWO_PI = 6.283185307179586
 
Line 1,825 ⟶ 3,443:
def rad2deg(a): return a * 180.0 / PI
def rad2grad(a): return a * 200.0 / PI
def rad2mil(a): return a * 3200.0 / PI</langsyntaxhighlight>
 
===Python: using tkinter===
<syntaxhighlight lang="python">
''' Python 3.6.5 code using Tkinter graphical user interface.
Angles (geometric), normalization and conversion challenge.
User enteres value for angle and selects a unit, then
presses 'Convert' button.
Values for that angle are shown in degrees, grads, mils
and radians.
'''
 
from tkinter import *
from tkinter import messagebox
import math
class Angle:
def __init__(self, gw):
self.window = gw
self.unit = StringVar()
self.unit.set(' ')
a1 = "(Enter 'Angle' & select 'Unit',"
a2 = "then click 'Convert')"
self.msga = a1 + '\n' + a2
 
# dictionary of functions to execute depending
# on which radio button was selected:
self.d_to_deg = {'d':self.d2d,
'g':self.g2d,
'm':self.m2d,
'r':self.r2d}
 
# top frame:
self.top_fr = Frame(gw,
width=600,
height=100,
bg='dodger blue')
self.top_fr.pack(fill=X)
 
self.hdg = Label(self.top_fr,
text=' Angle Conversion ',
font='arial 22 bold',
fg='navy',
bg='lemon chiffon')
self.hdg.place(relx=0.5, rely=0.5,
anchor=CENTER)
 
self.close_btn = Button(self.top_fr,
text='Quit',
bd=5,
bg='navy',
fg='lemon chiffon',
font='arial 12 bold',
command=self.close_window)
self.close_btn.place(relx=0.07, rely=0.5,
anchor=W)
 
self.clear_btn = Button(self.top_fr,
text='Clear',
bd=5,
bg='navy',
fg='lemon chiffon',
font='arial 12 bold',
command=self.clear_screen)
self.clear_btn.place(relx=0.92, rely=0.5,
anchor=E)
 
# bottom frame:
self.btm_fr = Frame(gw,
width=600,
height=500,
bg='lemon chiffon')
self.btm_fr.pack(fill=X)
self.msg = Label(self.btm_fr,
text=self.msga,
font='arial 16 bold',
fg='navy',
bg='lemon chiffon')
self.msg.place(relx=0.5, rely=0.1,
anchor=CENTER)
self.nbr_fr = LabelFrame(self.btm_fr,
text=' Angle ',
bg='dodger blue',
fg='navy',
bd=4,
relief=RIDGE,
font='arial 12 bold')
self.nbr_fr.place(relx=0.17, rely=0.27,
anchor=CENTER)
 
self.nbr_ent = Entry(self.nbr_fr,
justify='center',
font='arial 12 bold',
fg='navy',
bg='lemon chiffon',
bd=4,
width=10)
self.nbr_ent.grid(row=0, column=0,
padx=(8,8),
pady=(8,8))
 
self.su_fr = LabelFrame(self.btm_fr,
text=' Select Unit ',
bg='dodger blue',
fg='navy',
bd=8,
relief=RIDGE,
font='arial 12 bold')
self.su_fr.place(relx=0.48, rely=0.35,
anchor=CENTER)
 
self.radiod = Radiobutton(self.su_fr,
text='degree',
font='arial 12 bold',
fg='navy',
bg='dodger blue',
variable=self.unit,
value='d')
self.radiod.pack(side='top', anchor='w')
 
self.radiog = Radiobutton(self.su_fr,
text='gradian',
font='arial 12 bold',
fg='navy',
bg='dodger blue',
variable=self.unit,
value='g')
self.radiog.pack(side='top', anchor='w')
 
self.radiom = Radiobutton(self.su_fr,
text='mil',
font='arial 12 bold',
fg='navy',
bg='dodger blue',
variable=self.unit,
value='m')
self.radiom.pack(side='top', anchor='w')
 
self.radior = Radiobutton(self.su_fr,
text='radian',
font='arial 12 bold',
fg='navy',
bg='dodger blue',
variable=self.unit,
value='r')
self.radior.pack(side='top', anchor='w')
 
self.convert_btn = Button(self.btm_fr,
text='Convert',
width=14,
bd=5,
bg='dodger blue',
fg='navy',
font='arial 12 bold',
command=self.convert)
self.convert_btn.place(relx=0.93, rely=.25,
anchor=E)
 
self.res_fr = LabelFrame(self.btm_fr,
text=' Results ',
bg='dodger blue',
fg='navy',
bd=8,
width=230,
height=130,
relief=RIDGE,
font='arial 12 bold')
self.res_fr.place(relx=0.48, rely=0.74,
anchor=CENTER)
 
objh = 20 # widget height
lblw = 80 # label width
valw = 100 # value width
lblx = 15 # x-position of label in frame
valx = 100 # x-position of value in frame
objy = 5 # y-position of widget in frame
 
self.deg_lbl = Label(self.res_fr,
text=' degrees: ',
anchor=E,
font='"Noto Mono" 12 bold',
fg='navy',
bg='lemon chiffon')
self.deg_lbl.place(x=lblx, y=objy,
height=objh, width=lblw)
 
self.deg_val = Label(self.res_fr,
text=' ',
anchor=E,
padx = 3,
font='"Noto Mono" 12 bold',
fg='navy',
bg='lemon chiffon')
self.deg_val.place(x=valx, y=objy,
height=objh, width=valw)
 
objy = objy + objh # next row
self.grd_lbl = Label(self.res_fr,
text=' grads: ',
anchor=E,
font='"Noto Mono" 12 bold',
fg='navy',
bg='lemon chiffon')
self.grd_lbl.place(x=lblx, y=objy,
height=objh, width=lblw)
 
self.grd_val = Label(self.res_fr,
text=' ',
anchor=E,
padx = 3,
font='"Noto Mono" 12 bold',
fg='navy',
bg='lemon chiffon')
self.grd_val.place(x=valx, y=objy,
height=objh, width=valw)
 
objy = objy + objh # next row
self.mil_lbl = Label(self.res_fr,
text=' mils: ',
anchor=E,
font='"Noto Mono" 12 bold',
fg='navy',
bg='lemon chiffon')
self.mil_lbl.place(x=lblx, y=objy,
height=objh, width=lblw)
self.mil_val = Label(self.res_fr,
text=' ',
anchor=E,
padx = 3,
font='"Noto Mono" 12 bold',
fg='navy',
bg='lemon chiffon')
self.mil_val.place(x=valx, y=objy,
height=objh, width=valw)
 
objy = objy + objh # next row
self.rad_lbl = Label(self.res_fr,
text='radians: ',
anchor=E,
font='"Noto Mono" 12 bold',
fg='navy',
bg='lemon chiffon')
self.rad_lbl.place(x=lblx, y=objy,
height=objh, width=lblw)
 
self.rad_val = Label(self.res_fr,
text=' ',
anchor=E,
padx = 3,
font='"Noto Mono" 12 bold',
fg='navy',
bg='lemon chiffon')
self.rad_val.place(x=valx, y=objy,
height=objh, width=valw)
 
# process conversion request:
def convert(self):
# edit requested angle and unit:
try:
a = float(self.nbr_ent.get())
except:
self.err_msg('Angle entry must be numeric')
return
u = self.unit.get()
if u not in self.d_to_deg:
self.err_msg('Unit must be selected')
return
# convert request to degrees:
deg = self.d_to_deg[u](a)
 
# normalize:
self.deg = self.normalize(deg)
# convert to grads, mils, radians
self.grad = self.d2g(self.deg)
self.mil = self.d2m(self.deg)
self.rad = self.d2r(self.deg)
# show results
self.deg_val.config(text=self.format_angle(self.deg))
self.grd_val.config(text=self.format_angle(self.grad))
self.mil_val.config(text=self.format_angle(self.mil))
self.rad_val.config(text=self.format_angle(self.rad))
return
 
def d2d(self, a):
return a
 
def g2d(self, a):
return .9 * a
 
def r2d(self, a):
return 180 * a / math.pi
 
def m2d(self, a):
return .05625 * a
 
def normalize(self, a):
if a >= 0:
x = a % 360
else:
x = -(-a % 360)
if x == -0.0:
x = 0.0
return x
def d2g(self, a):
return 10 * a / 9
 
def d2m(self, a):
return 160 * a / 9
 
def d2r(self, a):
return math.pi * a / 180
 
def format_angle(self, a):
return f'{a:.4f}'
def err_msg(self, msg):
messagebox.showerror('Error Message', msg)
return
 
# restore screen to it's 'initial' state:
def clear_screen(self):
self.nbr_ent.delete(0, 'end')
self.unit.set(' ')
self.deg_val.config(text='')
self.grd_val.config(text='')
self.mil_val.config(text='')
self.rad_val.config(text='')
return
def close_window(self):
self.window.destroy()
 
# ************************************************
 
root = Tk()
root.title('ANGLES')
root.geometry('600x600+100+50')
root.resizable(False, False)
a = Angle(root)
root.mainloop()
 
# ************************************************
 
## I wish I could show a screenshot of the tkinter window
## to show how the input and output appear, but I don't know
## if that is possible here.
## I have processed all of the suggested angles and the
## results matched those from a few other languages on this
## page.
</syntaxhighlight>
 
=={{header|Racket}}==
Line 1,831 ⟶ 3,807:
{{trans|Common Lisp}}
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define (rem n m)
Line 1,879 ⟶ 3,855:
(displayln
(string-join (map report-angle '(-2 -1 0 1 2 6.2831853 16 57.2957795 359 399 6399 1000000))
"\n\n")))</langsyntaxhighlight>
 
{{out}}
Line 1,956 ⟶ 3,932:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>
<lang perl6>
my @units =
{ code => 'd', name => 'degrees' , number => 360 },
Line 1,985 ⟶ 3,961:
@results .fmt('%11g');
}
}</langsyntaxhighlight>
{{out}}
<pre> Angle Unit Degrees Gradians Mills Radians
Line 2,053 ⟶ 4,029:
''(Much of the complexity is due to the requirement that negative angles must normalize to a negative number.)''
 
<syntaxhighlight lang="raku" perl6line>sub postfix:<t>( $t ) { my $a = $t % 1 * τ; $t >=0 ?? $a !! $a - τ }
sub postfix:<d>( $d ) { my $a = $d % 360 * τ / 360; $d >=0 ?? $a !! $a - τ }
sub postfix:<g>( $g ) { my $a = $g % 400 * τ / 400; $g >=0 ?? $a !! $a - τ }
Line 2,074 ⟶ 4,050:
|($a.&f->t, $a.&f->d, $a.&f->g, $a.&f->m, $a.&f->r)».round(.00000001);
}
}</langsyntaxhighlight>
 
<pre> Quantity Unit turns degrees gradians mils radians
Line 2,161 ⟶ 4,137:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm normalizes an angle (in a scale), or converts angles from a scale to another.*/
numeric digits length( pi() ) - length(.) /*use the "length" of pi for precision.*/
parse arg x /*obtain optional arguments from the CL*/
Line 2,213 ⟶ 4,189:
say center(#o,23 ) center(@n #o,w7) center(#a,w7 ) center(#b,w7 ) center(#c,w7 )
say center('',23,_) center('',w7, _) center('',w7,_) center('',w7,_) center('',w7,_)
return /* '↑' seperator line.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,279 ⟶ 4,255:
=={{header|Ruby}}==
This Angles module responds to methods like r2d. Adding an element (like "h"=>24, for a clock-like angle system) to the BASES hash adds 9 more methods, totaling to 25 methods. None of these methods are actually implemented.
<langsyntaxhighlight lang="ruby">module Angles
BASES = {"d" => 360, "g" => 400, "m" => 6400, "r" => Math::PI*2 ,"h" => 24 }
Line 2,303 ⟶ 4,279:
puts
end
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:45ex"> d g m r h
Line 2,374 ⟶ 4,350:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::{
marker::PhantomData,
f64::consts::PI,
Line 2,449 ⟶ 4,425:
print_angles::<Mils>();
print_angles::<Radians>();
}</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 2,510 ⟶ 4,486:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
func normalize(_ f: Double, N: Double) -> Double {
Line 2,587 ⟶ 4,563:
 
print()
}</langsyntaxhighlight>
 
{{out}}
Line 2,646 ⟶ 4,622:
("1000000.0000", "1600.0000", "Mils", "90.0000", "100.0000", "1600.0000", "1.5708")
("1000000.0000", "5.9256", "Radians", "339.5132", "377.2368", "6035.7895", "5.9256")</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import math
import strconv
fn d2d(d f64) f64 { return math.mod(d, 360) }
fn g2g(g f64) f64 { return math.mod(g, 400) }
fn m2m(m f64) f64 { return math.mod(m, 6400) }
fn r2r(r f64) f64 { return math.mod(r, 2*math.pi) }
fn d2g(d f64) f64 { return d2d(d) * 400 / 360 }
fn d2m(d f64) f64 { return d2d(d) * 6400 / 360 }
fn d2r(d f64) f64 { return d2d(d) * math.pi / 180 }
fn g2d(g f64) f64 { return g2g(g) * 360 / 400 }
fn g2m(g f64) f64 { return g2g(g) * 6400 / 400 }
fn g2r(g f64) f64 { return g2g(g) * math.pi / 200 }
fn m2d(m f64) f64 { return m2m(m) * 360 / 6400 }
fn m2g(m f64) f64 { return m2m(m) * 400 / 6400 }
fn m2r(m f64) f64 { return m2m(m) * math.pi / 3200 }
fn r2d(r f64) f64 { return r2r(r) * 180 / math.pi }
fn r2g(r f64) f64 { return r2r(r) * 200 / math.pi }
fn r2m(r f64) f64 { return r2r(r) * 3200 / math.pi }
 
fn s(f f64) string {
wf := strconv.format_fl(f, strconv.BF_param{
len0: 15
len1: 7
positive: f>=0
}).split('.')
if wf[1] == '0000000' {
return "${wf[0]:7} "
}
mut le := wf[1].len
if le > 7 {
le = 7
}
return "${wf[0]:7}.${wf[1][..le]:-7}"
}
fn main() {
angles := [-2.0, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000]
println(" degrees normalized degs gradians mils radians")
for a in angles {
println('${s(a)} ${s(d2d(a))} ${s(d2g(a))} ${s(d2m(a))} ${s(d2r(a))}')
}
println("\n gradians normalized grds degrees mils radians")
for a in angles {
println('${s(a)} ${s(g2g(a))} ${s(g2d(a))} ${s(g2m(a))} ${s(g2r(a))}')
}
println("\n mils normalized mils degrees gradians radians")
for a in angles {
println('${s(a)} ${s(m2m(a))} ${s(m2d(a))} ${s(m2g(a))} ${s(m2r(a))}')
}
println("\n radians normalized rads degrees gradians mils ")
for a in angles {
println('${s(a)} ${s(r2r(a))} ${s(r2d(a))} ${s(r2g(a))} ${s(r2m(a))}')
}
}</syntaxhighlight>
 
{{out}}
<pre>Same as Go entry</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var d2d = Fn.new { |d| d % 360 }
Line 2,688 ⟶ 4,723:
for (a in angles) {
Fmt.print(f2, a, r2r.call(a), r2d.call(a), r2g.call(a), r2m.call(a))
}</langsyntaxhighlight>
 
{{out}}
Line 2,747 ⟶ 4,782:
6399.0 2.7173573 155.6931042 172.992338 2767.8774082
1000000.0 5.9256211 339.5130823 377.2367581 6035.7881302
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">def Pi = 3.14159265358979323846, N=12, Tab=9;
func real D2D; real D; return Mod(D, 360.);
func real G2G; real G; return Mod(G, 400.);
func real M2M; real M; return Mod(M, 6400.);
func real R2R; real R; return Mod(R, 2.*Pi);
func real D2G; real D; return 400. * D2D(D) / 360.;
func real D2M; real D; return 6400.* D2D(D) / 360.;
func real D2R; real D; return 2.*Pi* D2D(D) / 360.;
func real G2D; real G; return 360. * G2G(G) / 400.;
func real G2M; real G; return 6400.* G2G(G) / 400.;
func real G2R; real G; return 2.*Pi* G2G(G) / 400.;
func real M2D; real M; return 360. * M2M(M) / 6400.;
func real M2G; real M; return 400. * M2M(M) / 6400.;
func real M2R; real M; return 2.*Pi* M2M(M) / 6400.;
func real R2D; real R; return 360. * R2R(R) / (2.*Pi);
func real R2G; real R; return 400. * R2R(R) / (2.*Pi);
func real R2M; real R; return 6400.* R2R(R) / (2.*Pi);
 
real Angle; int I;
[Angle:=
[-2., -1., 0., 1., 2., 6.2831853, 16., 57.2957795, 359., 399., 6399., 1000000.];
Format(7, 7);
Text(0, "
Degrees Normalized Gradians Mils Radians
");
for I:= 0 to N-1 do
[RlOut(0, Angle(I)); ChOut(0, Tab);
RlOut(0, D2D(Angle(I))); ChOut(0, Tab);
RlOut(0, D2G(Angle(I))); ChOut(0, Tab);
RlOut(0, D2M(Angle(I))); ChOut(0, Tab);
RlOut(0, D2R(Angle(I))); CrLf(0);
];
Text(0, "
Gradians Normalized Degrees Mils Radians
");
for I:= 0 to N-1 do
[RlOut(0, Angle(I)); ChOut(0, Tab);
RlOut(0, G2G(Angle(I))); ChOut(0, Tab);
RlOut(0, G2D(Angle(I))); ChOut(0, Tab);
RlOut(0, G2M(Angle(I))); ChOut(0, Tab);
RlOut(0, G2R(Angle(I))); CrLf(0);
];
Text(0, "
Mils Normalized Degrees Gradians Radians
");
for I:= 0 to N-1 do
[RlOut(0, Angle(I)); ChOut(0, Tab);
RlOut(0, M2M(Angle(I))); ChOut(0, Tab);
RlOut(0, M2D(Angle(I))); ChOut(0, Tab);
RlOut(0, M2G(Angle(I))); ChOut(0, Tab);
RlOut(0, M2R(Angle(I))); CrLf(0);
];
Text(0, "
Radians Normalized Degrees Gradians Mils
");
for I:= 0 to N-1 do
[RlOut(0, Angle(I)); ChOut(0, Tab);
RlOut(0, R2R(Angle(I))); ChOut(0, Tab);
RlOut(0, R2D(Angle(I))); ChOut(0, Tab);
RlOut(0, R2G(Angle(I))); ChOut(0, Tab);
RlOut(0, R2M(Angle(I))); CrLf(0);
];
]</syntaxhighlight>
 
{{out}}
<pre>
Degrees Normalized Gradians Mils Radians
-2.0000000 -2.0000000 -2.2222222 -35.5555556 -0.0349066
-1.0000000 -1.0000000 -1.1111111 -17.7777778 -0.0174533
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 1.1111111 17.7777778 0.0174533
2.0000000 2.0000000 2.2222222 35.5555556 0.0349066
6.2831853 6.2831853 6.9813170 111.7010720 0.1096623
16.0000000 16.0000000 17.7777778 284.4444444 0.2792527
57.2957795 57.2957795 63.6619772 1018.5916356 1.0000000
359.0000000 359.0000000 398.8888889 6382.2222222 6.2657320
399.0000000 39.0000000 43.3333333 693.3333333 0.6806784
6399.0000000 279.0000000 310.0000000 4960.0000000 4.8694686
1000000.0000000 280.0000000 311.1111111 4977.7777778 4.8869219
Gradians Normalized Degrees Mils Radians
-2.0000000 -2.0000000 -1.8000000 -32.0000000 -0.0314159
-1.0000000 -1.0000000 -0.9000000 -16.0000000 -0.0157080
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 0.9000000 16.0000000 0.0157080
2.0000000 2.0000000 1.8000000 32.0000000 0.0314159
6.2831853 6.2831853 5.6548668 100.5309648 0.0986960
16.0000000 16.0000000 14.4000000 256.0000000 0.2513274
57.2957795 57.2957795 51.5662016 916.7324720 0.9000000
359.0000000 359.0000000 323.1000000 5744.0000000 5.6391588
399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
6399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
1000000.0000000 0.0000000 0.0000000 0.0000000 0.0000000
Mils Normalized Degrees Gradians Radians
-2.0000000 -2.0000000 -0.1125000 -0.1250000 -0.0019635
-1.0000000 -1.0000000 -0.0562500 -0.0625000 -0.0009817
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 0.0562500 0.0625000 0.0009817
2.0000000 2.0000000 0.1125000 0.1250000 0.0019635
6.2831853 6.2831853 0.3534292 0.3926991 0.0061685
16.0000000 16.0000000 0.9000000 1.0000000 0.0157080
57.2957795 57.2957795 3.2228876 3.5809862 0.0562500
359.0000000 359.0000000 20.1937500 22.4375000 0.3524474
399.0000000 399.0000000 22.4437500 24.9375000 0.3917173
6399.0000000 6399.0000000 359.9437500 399.9375000 6.2822036
1000000.0000000 1600.0000000 90.0000000 100.0000000 1.5707963
Radians Normalized Degrees Gradians Mils
-2.0000000 -2.0000000 -114.5915590 -127.3239545 -2037.1832716
-1.0000000 -1.0000000 -57.2957795 -63.6619772 -1018.5916358
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
1.0000000 1.0000000 57.2957795 63.6619772 1018.5916358
2.0000000 2.0000000 114.5915590 127.3239545 2037.1832716
6.2831853 6.2831853 359.9999996 399.9999995 6399.9999927
16.0000000 3.4336294 196.7324722 218.5916358 3497.4661726
57.2957795 0.7471117 42.8063493 47.5626103 761.0017647
359.0000000 0.8584375 49.1848452 54.6498280 874.3972479
399.0000000 3.1593256 181.0160257 201.1289175 3218.0626795
6399.0000000 2.7173573 155.6931042 172.9923380 2767.8774082
1000000.0000000 5.9256211 339.5130823 377.2367581 6035.7881302
</pre>
 
=={{header|zkl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="zkl">var [const]
tau=(0.0).pi*2,
units=Dictionary( // code:(name, units in circle)
Line 2,765 ⟶ 4,921:
})
})
;</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">codes:=units.keys;
println(" Angle Unit ",
codes.apply(fcn(k){ units[k][0] }).apply("%11s".fmt).concat(" "));
Line 2,777 ⟶ 4,933:
r.apply("%12g".fmt).concat(" ")));
}
}</langsyntaxhighlight>
{{out}}
<pre style="height:45ex">
3,021

edits