Angles (geometric), normalization and conversion: Difference between revisions
Content added Content deleted
(Added XPL0 example.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 122: | Line 122: | ||
{{trans|Nim}} |
{{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 normd(x) {R fmod(x, 360)} |
||
Line 166: | Line 166: | ||
print(‘───────────────────────────────────────────────────────────────────────────────────’) |
print(‘───────────────────────────────────────────────────────────────────────────────────’) |
||
L(val) values |
L(val) values |
||
print(‘#7.7 #7.7 #7.7 #7.7 #7.7’.format(val, normr(val), r2d(val), r2g(val), r2m(val)))</ |
print(‘#7.7 #7.7 #7.7 #7.7 #7.7’.format(val, normr(val), r2d(val), r2g(val), r2m(val)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 232: | Line 232: | ||
=={{header|AutoHotkey}}== |
=={{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" |
result .= "Degrees Degrees Gradians Mils Radians`n" |
||
Line 301: | Line 301: | ||
return Rad2Rad(Rad) * 3200 / (π:=3.141592653589793) |
return Rad2Rad(Rad) * 3200 / (π:=3.141592653589793) |
||
} |
} |
||
;-------------------------------------------------------</ |
;-------------------------------------------------------</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Degrees Degrees Gradians Mils Radians |
<pre>Degrees Degrees Gradians Mils Radians |
||
Line 360: | Line 360: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang=AWK> |
||
# syntax: GAWK -f ANGLES_(GEOMETRIC)_NORMALIZATION_AND_CONVERSION.AWK |
# syntax: GAWK -f ANGLES_(GEOMETRIC)_NORMALIZATION_AND_CONVERSION.AWK |
||
# gawk starts showing discrepancies at test case number 7 when compared with C# |
# gawk starts showing discrepancies at test case number 7 when compared with C# |
||
Line 424: | Line 424: | ||
function radian2gradian(angle) { return(angle * 200 / pi) } |
function radian2gradian(angle) { return(angle * 200 / pi) } |
||
function radian2mil(angle) { return(angle * 3200 / pi) } |
function radian2mil(angle) { return(angle * 3200 / pi) } |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 491: | Line 491: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang=c>#define PI 3.141592653589793 |
||
#define TWO_PI 6.283185307179586 |
#define TWO_PI 6.283185307179586 |
||
Line 529: | Line 529: | ||
double rad2deg(double a) {return a * 180 / PI;} |
double rad2deg(double a) {return a * 180 / PI;} |
||
double rad2grad(double a) {return a * 200 / PI;} |
double rad2grad(double a) {return a * 200 / PI;} |
||
double rad2mil(double a) {return a * 3200 / PI;}</ |
double rad2mil(double a) {return a * 3200 / PI;}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang=csharp>using System; |
||
public static class Angles |
public static class Angles |
||
Line 595: | Line 595: | ||
public static double RadToGrad(double angle) => angle * 200 / Math.PI; |
public static double RadToGrad(double angle) => angle * 200 / Math.PI; |
||
public static double RadToMil(double angle) => angle * 3200 / Math.PI; |
public static double RadToMil(double angle) => angle * 3200 / Math.PI; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:30ex;overflow:scroll"> |
<pre style="height:30ex;overflow:scroll"> |
||
Line 646: | Line 646: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{libheader|Boost}} |
{{libheader|Boost}} |
||
< |
<syntaxhighlight lang=cpp>#include <functional> |
||
#include <iostream> |
#include <iostream> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 713: | Line 713: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> ┌───────────────────┐ |
<pre> ┌───────────────────┐ |
||
Line 753: | Line 753: | ||
or (angles) for auto-test. |
or (angles) for auto-test. |
||
VAL* is unnormalized. |
VAL* is unnormalized. |
||
< |
<syntaxhighlight lang=lisp>(defun DegToDeg (a) (rem a 360)) |
||
(defun GradToGrad (a) (rem a 400)) |
(defun GradToGrad (a) (rem a 400)) |
||
(defun MilToMil (a) (rem a 6400)) |
(defun MilToMil (a) (rem a 6400)) |
||
Line 781: | Line 781: | ||
(format t "Grad | ~15f | ~15f | ~15f | ~15f | ~15f~%" a (GradToDeg a) (GradToGrad a) (GradToMil a) (GradToRad a)) |
(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 "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))))</ |
(format t "Rad | ~15f | ~15f | ~15f | ~15f | ~15f~%~%" a (RadToDeg a) (RadToGrad a) (RadToMil a) (RadToRad a))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[CLISP]> (angles) |
<pre>[CLISP]> (angles) |
||
Line 859: | Line 859: | ||
{{libheader| System.Math}} |
{{libheader| System.Math}} |
||
{{Trans|Go}} |
{{Trans|Go}} |
||
< |
<syntaxhighlight lang=Delphi> |
||
program normalization_and_conversion; |
program normalization_and_conversion; |
||
Line 986: | Line 986: | ||
writeln(format(ft, [s(a), s(r2r(a)), s(r2d(a)), s(r2g(a)), s(r2m(a))])); |
writeln(format(ft, [s(a), s(r2r(a)), s(r2d(a)), s(r2g(a)), s(r2m(a))])); |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{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.) |
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.) |
||
< |
<syntaxhighlight lang=factor>USING: accessors combinators formatting inverse kernel math |
||
math.constants quotations qw sequences units.si ; |
math.constants quotations qw sequences units.si ; |
||
IN: rosetta-code.angles |
IN: rosetta-code.angles |
||
Line 1,014: | Line 1,014: | ||
units [ .row ] cartesian-each ; |
units [ .row ] cartesian-each ; |
||
MAIN: angles</ |
MAIN: angles</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,070: | Line 1,070: | ||
=={{header|FreeBASIC}}== |
=={{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. |
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 |
#define INVALID -99999 |
||
Line 1,191: | Line 1,191: | ||
next i |
next i |
||
next k |
next k |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,257: | Line 1,257: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang=go>package main |
||
import ( |
import ( |
||
Line 1,331: | Line 1,331: | ||
fmt.Printf(ft, s(a), s(r2r(a)), s(r2d(a)), s(r2g(a)), s(r2m(a))) |
fmt.Printf(ft, s(a), s(r2r(a)), s(r2d(a)), s(r2g(a)), s(r2m(a))) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,394: | Line 1,394: | ||
=={{header|Groovy}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang=groovy>import java.lang.reflect.Constructor |
||
abstract class Angle implements Comparable<? extends Angle> { |
abstract class Angle implements Comparable<? extends Angle> { |
||
Line 1,463: | Line 1,463: | ||
String unitName() { UNIT } |
String unitName() { UNIT } |
||
Radians(Number value = 0) { super(value) } |
Radians(Number value = 0) { super(value) } |
||
}</ |
}</syntaxhighlight> |
||
This category class allows Angles to interoperate more easily with Numbers: |
This category class allows Angles to interoperate more easily with Numbers: |
||
< |
<syntaxhighlight lang=groovy>class AngleCategory { |
||
static Degrees getDeg(Number n) { new Degrees(n) } |
static Degrees getDeg(Number n) { new Degrees(n) } |
||
static Gradians getGrad(Number n) { new Gradians(n) } |
static Gradians getGrad(Number n) { new Gradians(n) } |
||
static Mils getMil(Number n) { new Mils(n) } |
static Mils getMil(Number n) { new Mils(n) } |
||
static Radians getRad(Number n) { new Radians(n) } |
static Radians getRad(Number n) { new Radians(n) } |
||
}</ |
}</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang=groovy>Number.metaClass.mixin AngleCategory |
||
[ -2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000 ].each { rawAngle -> |
[ -2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000 ].each { rawAngle -> |
||
Line 1,490: | Line 1,490: | ||
assert 360.deg == 0.deg |
assert 360.deg == 0.deg |
||
assert 90.deg == 100.grad |
assert 90.deg == 100.grad |
||
assert Math.PI.rad == 3200.mil</ |
assert Math.PI.rad == 3200.mil</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,570: | Line 1,570: | ||
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). |
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 TypeApplications #-} |
||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
||
Line 1,604: | Line 1,604: | ||
to :: forall b a. (Angle a, Angle b) => a -> b |
to :: forall b a. (Angle a, Angle b) => a -> b |
||
to = fromTurn . toTurn</ |
to = fromTurn . toTurn</syntaxhighlight> |
||
'''Instances of different angular units.''' |
'''Instances of different angular units.''' |
||
< |
<syntaxhighlight lang=haskell>-- radians |
||
newtype Rad = Rad Double |
newtype Rad = Rad Double |
||
deriving (Eq, Ord, Num, Real, Fractional, RealFrac, Floating) |
deriving (Eq, Ord, Num, Real, Fractional, RealFrac, Floating) |
||
Line 1,675: | Line 1,675: | ||
toTurn = toTurn @Rad . angle . atan . value |
toTurn = toTurn @Rad . angle . atan . value |
||
fromTurn = angle . tan . value . fromTurn @Rad |
fromTurn = angle . tan . value . fromTurn @Rad |
||
normalize = id</ |
normalize = id</syntaxhighlight> |
||
'''Examples''' |
'''Examples''' |
||
Line 1,716: | Line 1,716: | ||
The task assignment shows different possible type annotations. |
The task assignment shows different possible type annotations. |
||
< |
<syntaxhighlight lang=haskell>main = do |
||
let xs = [-2, -1, 0, 1, 2, 6.2831853, |
let xs = [-2, -1, 0, 1, 2, 6.2831853, |
||
16, 57.2957795, 359, 399, 6399, 1000000] |
16, 57.2957795, 359, 399, 6399, 1000000] |
||
Line 1,750: | Line 1,750: | ||
print $ (from :: Grad -> Mil) . angle <$> xs |
print $ (from :: Grad -> Mil) . angle <$> xs |
||
print $ (from :: Mil -> Mil) . angle <$> xs |
print $ (from :: Mil -> Mil) . angle <$> xs |
||
print $ (from :: Slope -> Mil) . angle <$> xs</ |
print $ (from :: Slope -> Mil) . angle <$> xs</syntaxhighlight> |
||
<pre>λ> main |
<pre>λ> main |
||
Line 1,801: | Line 1,801: | ||
Mil =: adverb def 'normalize as_mil inv m' |
Mil =: adverb def 'normalize as_mil inv m' |
||
Radian =: adverb def 'normalize as_radian inv m' |
Radian =: adverb def 'normalize as_radian inv m' |
||
</syntaxhighlight> |
|||
</lang> |
|||
we can compose conversion sentences like |
we can compose conversion sentences like |
||
<lang> |
<lang> |
||
as_degree 100 Gradian |
as_degree 100 Gradian |
||
90 |
90 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Presuming the following additional definitions: |
Presuming the following additional definitions: |
||
<lang> |
<lang> |
||
Line 1,860: | Line 1,860: | ||
|radian | _2 _1 0 1 2 6.28319 3.43363 0.747112 0.858437 3.15933 2.71736 5.92562| |
|radian | _2 _1 0 1 2 6.28319 3.43363 0.747112 0.858437 3.15933 2.71736 5.92562| |
||
+-------+---------------------------------------------------------------------------------------------------+ |
+-------+---------------------------------------------------------------------------------------------------+ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang=java>import java.text.DecimalFormat; |
||
// Title: Angles (geometric), normalization and conversion |
// Title: Angles (geometric), normalization and conversion |
||
Line 1,964: | Line 1,964: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,019: | Line 2,019: | ||
</pre> |
</pre> |
||
=={{header|Javascript}}== |
=={{header|Javascript}}== |
||
< |
<syntaxhighlight lang=javaScript> |
||
/*****************************************************************\ |
/*****************************************************************\ |
||
| Expects an angle, an origin unit and a unit to convert to, | |
| Expects an angle, an origin unit and a unit to convert to, | |
||
Line 2,081: | Line 2,081: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output (without bolds and subs): |
Output (without bolds and subs): |
||
{{out}}<pre> |
{{out}}<pre> |
||
Line 2,235: | Line 2,235: | ||
'''Preliminaries''' |
'''Preliminaries''' |
||
< |
<syntaxhighlight lang=jq>### Formatting |
||
# Right-justify but do not truncate |
# Right-justify but do not truncate |
||
Line 2,273: | Line 2,273: | ||
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''The Task''' |
'''The Task''' |
||
< |
<syntaxhighlight lang=jq># Normalization as per the task requirements |
||
def mod($y): |
def mod($y): |
||
(if . < 0 then -$y else 0 end) as $adjust |
(if . < 0 then -$y else 0 end) as $adjust |
||
Line 2,330: | Line 2,330: | ||
task2, "", |
task2, "", |
||
task3, "", |
task3, "", |
||
task4</ |
task4</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,338: | Line 2,338: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang=julia>using Formatting |
||
d2d(d) = d % 360 |
d2d(d) = d % 360 |
||
Line 2,377: | Line 2,377: | ||
testconversions([-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000]) |
testconversions([-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000]) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Number Units Degrees Gradians Mils Radians |
Number Units Degrees Gradians Mils Radians |
||
Line 2,432: | Line 2,432: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=scala>import java.text.DecimalFormat as DF |
||
const val DEGREE = 360.0 |
const val DEGREE = 360.0 |
||
Line 2,484: | Line 2,484: | ||
println("%15s %8s = %10s %10s %10s %10s".format(fa.format(a), units, fc.format(d), fc.format(g), fc.format(m), fc.format(r))) |
println("%15s %8s = %10s %10s %10s %10s".format(fa.format(a), units, fc.format(d), fc.format(g), fc.format(m), fc.format(r))) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
See Java output |
See Java output |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang=lua>range = { degrees=360, gradians=400, mils=6400, radians=2.0*math.pi } |
||
function convert(value, fromunit, tounit) |
function convert(value, fromunit, tounit) |
||
return math.fmod(value * range[tounit] / range[fromunit], range[tounit]) |
return math.fmod(value * range[tounit] / range[fromunit], range[tounit]) |
||
Line 2,505: | Line 2,505: | ||
print(string.format("%15.7f %8s = %15.7f %15.7f %15.7f %15.7f", value, fromunit, d, g, m, r)) |
print(string.format("%15.7f %8s = %15.7f %15.7f %15.7f %15.7f", value, fromunit, d, g, m, r)) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
Optionally: |
Optionally: |
||
< |
<syntaxhighlight lang=lua>-- if you care.. |
||
assert(convert(-360,"degrees","degrees") == 0.0) |
assert(convert(-360,"degrees","degrees") == 0.0) |
||
assert(convert(360,"degrees","degrees") == 0.0) |
assert(convert(360,"degrees","degrees") == 0.0) |
||
Line 2,533: | Line 2,533: | ||
function r2g(n) return convert(n,"radians","gradians") end |
function r2g(n) return convert(n,"radians","gradians") end |
||
function r2m(n) return convert(n,"radians","mils") end |
function r2m(n) return convert(n,"radians","mils") end |
||
function r2r(n) return convert(n,"radians","radians") end</ |
function r2r(n) return convert(n,"radians","radians") end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> VALUE UNIT = DEGREES GRADIANS MILS RADIANS |
<pre> VALUE UNIT = DEGREES GRADIANS MILS RADIANS |
||
Line 2,586: | Line 2,586: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang=Mathematica>ClearAll[NormalizeAngle, NormalizeDegree, NormalizeGradian, NormalizeMil, NormalizeRadian] |
||
NormalizeAngle[d_, full_] := Module[{a = d}, |
NormalizeAngle[d_, full_] := Module[{a = d}, |
||
If[Abs[a/full] > 4, |
If[Abs[a/full] > 4, |
||
Line 2,616: | Line 2,616: | ||
Mil2Gradian, Mil2Radian, Radian2Degree, Radian2Gradian, |
Mil2Gradian, Mil2Radian, Radian2Degree, Radian2Gradian, |
||
Radian2Mil}, {-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, |
Radian2Mil}, {-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, |
||
6399, 1000000}}]</ |
6399, 1000000}}]</syntaxhighlight> |
||
{{out}} |
{{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> |
<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}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang=Nim>import math |
||
import strformat |
import strformat |
||
Line 2,672: | Line 2,672: | ||
echo "———————————————————————————————————————————————————————————————————————————————————" |
echo "———————————————————————————————————————————————————————————————————————————————————" |
||
for val in Values: |
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}"</ |
echo fmt"{val:15.7f} {r2r(val):15.7f} {r2d(val):15.7f} {r2g(val):15.7f} {r2m(val):15.7f}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,737: | Line 2,737: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang=perl>use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 2,770: | Line 2,770: | ||
printf '%10g %-8s' . '%12g'x4 . "\n", $angle, ${$from}{name}, @results; |
printf '%10g %-8s' . '%12g'x4 . "\n", $angle, ${$from}{name}, @results; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> Angle Unit Degrees Gradians Mills Radians |
<pre> Angle Unit Degrees Gradians Mills Radians |
||
Line 2,836: | Line 2,836: | ||
=={{header|Phix}}== |
=={{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 |
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>--> |
||
<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> |
<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> |
||
<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> |
<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> |
||
Line 2,856: | Line 2,856: | ||
<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> |
<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> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,923: | Line 2,923: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python: Original=== |
===Python: Original=== |
||
< |
<syntaxhighlight lang=python>PI = 3.141592653589793 |
||
TWO_PI = 6.283185307179586 |
TWO_PI = 6.283185307179586 |
||
Line 2,957: | Line 2,957: | ||
def rad2deg(a): return a * 180.0 / PI |
def rad2deg(a): return a * 180.0 / PI |
||
def rad2grad(a): return a * 200.0 / PI |
def rad2grad(a): return a * 200.0 / PI |
||
def rad2mil(a): return a * 3200.0 / PI</ |
def rad2mil(a): return a * 3200.0 / PI</syntaxhighlight> |
||
===Python: using tkinter=== |
===Python: using tkinter=== |
||
< |
<syntaxhighlight lang=python> |
||
''' Python 3.6.5 code using Tkinter graphical user interface. |
''' Python 3.6.5 code using Tkinter graphical user interface. |
||
Angles (geometric), normalization and conversion challenge. |
Angles (geometric), normalization and conversion challenge. |
||
Line 3,315: | Line 3,315: | ||
## results matched those from a few other languages on this |
## results matched those from a few other languages on this |
||
## page. |
## page. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Line 3,321: | Line 3,321: | ||
{{trans|Common Lisp}} |
{{trans|Common Lisp}} |
||
< |
<syntaxhighlight lang=racket>#lang racket |
||
(define (rem n m) |
(define (rem n m) |
||
Line 3,369: | Line 3,369: | ||
(displayln |
(displayln |
||
(string-join (map report-angle '(-2 -1 0 1 2 6.2831853 16 57.2957795 359 399 6399 1000000)) |
(string-join (map report-angle '(-2 -1 0 1 2 6.2831853 16 57.2957795 359 399 6399 1000000)) |
||
"\n\n")))</ |
"\n\n")))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,446: | Line 3,446: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
< |
<syntaxhighlight lang=perl6> |
||
my @units = |
my @units = |
||
{ code => 'd', name => 'degrees' , number => 360 }, |
{ code => 'd', name => 'degrees' , number => 360 }, |
||
Line 3,475: | Line 3,475: | ||
@results .fmt('%11g'); |
@results .fmt('%11g'); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> Angle Unit Degrees Gradians Mills Radians |
<pre> Angle Unit Degrees Gradians Mills Radians |
||
Line 3,543: | Line 3,543: | ||
''(Much of the complexity is due to the requirement that negative angles must normalize to a negative number.)'' |
''(Much of the complexity is due to the requirement that negative angles must normalize to a negative number.)'' |
||
< |
<syntaxhighlight lang=perl6>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:<d>( $d ) { my $a = $d % 360 * τ / 360; $d >=0 ?? $a !! $a - τ } |
||
sub postfix:<g>( $g ) { my $a = $g % 400 * τ / 400; $g >=0 ?? $a !! $a - τ } |
sub postfix:<g>( $g ) { my $a = $g % 400 * τ / 400; $g >=0 ?? $a !! $a - τ } |
||
Line 3,564: | Line 3,564: | ||
|($a.&f->t, $a.&f->d, $a.&f->g, $a.&f->m, $a.&f->r)».round(.00000001); |
|($a.&f->t, $a.&f->d, $a.&f->g, $a.&f->m, $a.&f->r)».round(.00000001); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre> Quantity Unit turns degrees gradians mils radians |
<pre> Quantity Unit turns degrees gradians mils radians |
||
Line 3,651: | Line 3,651: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight 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.*/ |
numeric digits length( pi() ) - length(.) /*use the "length" of pi for precision.*/ |
||
parse arg x /*obtain optional arguments from the CL*/ |
parse arg x /*obtain optional arguments from the CL*/ |
||
Line 3,703: | Line 3,703: | ||
say center(#o,23 ) center(@n #o,w7) center(#a,w7 ) center(#b,w7 ) center(#c,w7 ) |
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,_) |
say center('',23,_) center('',w7, _) center('',w7,_) center('',w7,_) center('',w7,_) |
||
return /* '↑' seperator line.*/</ |
return /* '↑' seperator line.*/</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
Line 3,769: | Line 3,769: | ||
=={{header|Ruby}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang=ruby>module Angles |
||
BASES = {"d" => 360, "g" => 400, "m" => 6400, "r" => Math::PI*2 ,"h" => 24 } |
BASES = {"d" => 360, "g" => 400, "m" => 6400, "r" => Math::PI*2 ,"h" => 24 } |
||
Line 3,793: | Line 3,793: | ||
puts |
puts |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style="height:45ex"> d g m r h |
<pre style="height:45ex"> d g m r h |
||
Line 3,864: | Line 3,864: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang=rust>use std::{ |
||
marker::PhantomData, |
marker::PhantomData, |
||
f64::consts::PI, |
f64::consts::PI, |
||
Line 3,939: | Line 3,939: | ||
print_angles::<Mils>(); |
print_angles::<Mils>(); |
||
print_angles::<Radians>(); |
print_angles::<Radians>(); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:30ex;overflow:scroll"> |
<pre style="height:30ex;overflow:scroll"> |
||
Line 4,000: | Line 4,000: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang=swift>import Foundation |
||
func normalize(_ f: Double, N: Double) -> Double { |
func normalize(_ f: Double, N: Double) -> Double { |
||
Line 4,077: | Line 4,077: | ||
print() |
print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,139: | Line 4,139: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang=vlang>import math |
||
import strconv |
import strconv |
||
fn d2d(d f64) f64 { return math.mod(d, 360) } |
fn d2d(d f64) f64 { return math.mod(d, 360) } |
||
Line 4,191: | Line 4,191: | ||
println('${s(a)} ${s(r2r(a))} ${s(r2d(a))} ${s(r2g(a))} ${s(r2m(a))}') |
println('${s(a)} ${s(r2r(a))} ${s(r2d(a))} ${s(r2g(a))} ${s(r2m(a))}') |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,199: | Line 4,199: | ||
{{trans|Go}} |
{{trans|Go}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang=ecmascript>import "/fmt" for Fmt |
||
var d2d = Fn.new { |d| d % 360 } |
var d2d = Fn.new { |d| d % 360 } |
||
Line 4,237: | Line 4,237: | ||
for (a in angles) { |
for (a in angles) { |
||
Fmt.print(f2, a, r2r.call(a), r2d.call(a), r2g.call(a), r2m.call(a)) |
Fmt.print(f2, a, r2r.call(a), r2d.call(a), r2g.call(a), r2m.call(a)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,299: | Line 4,299: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang=XPL0>def Pi = 3.14159265358979323846, N=12, Tab=9; |
||
func real D2D; real D; return Mod(D, 360.); |
func real D2D; real D; return Mod(D, 360.); |
||
func real G2G; real G; return Mod(G, 400.); |
func real G2G; real G; return Mod(G, 400.); |
||
Line 4,361: | Line 4,361: | ||
RlOut(0, R2M(Angle(I))); CrLf(0); |
RlOut(0, R2M(Angle(I))); CrLf(0); |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,421: | Line 4,421: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang=zkl>var [const] |
||
tau=(0.0).pi*2, |
tau=(0.0).pi*2, |
||
units=Dictionary( // code:(name, units in circle) |
units=Dictionary( // code:(name, units in circle) |
||
Line 4,435: | Line 4,435: | ||
}) |
}) |
||
}) |
}) |
||
;</ |
;</syntaxhighlight> |
||
< |
<syntaxhighlight lang=zkl>codes:=units.keys; |
||
println(" Angle Unit ", |
println(" Angle Unit ", |
||
codes.apply(fcn(k){ units[k][0] }).apply("%11s".fmt).concat(" ")); |
codes.apply(fcn(k){ units[k][0] }).apply("%11s".fmt).concat(" ")); |
||
Line 4,447: | Line 4,447: | ||
r.apply("%12g".fmt).concat(" "))); |
r.apply("%12g".fmt).concat(" "))); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:45ex"> |
<pre style="height:45ex"> |