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

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 122:
{{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)}
Line 232:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang=AutoHotkey"autohotkey">testAngles := [-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000]
 
result .= "Degrees Degrees Gradians Mils Radians`n"
Line 360:
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f ANGLES_(GEOMETRIC)_NORMALIZATION_AND_CONVERSION.AWK
# gawk starts showing discrepancies at test case number 7 when compared with C#
Line 491:
 
=={{header|C}}==
<syntaxhighlight lang="c">#define PI 3.141592653589793
#define TWO_PI 6.283185307179586
 
Line 532:
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
 
public static class Angles
Line 646:
=={{header|C++}}==
{{libheader|Boost}}
<syntaxhighlight lang="cpp">#include <functional>
#include <iostream>
#include <iomanip>
Line 753:
or (angles) for auto-test.
VAL* is unnormalized.
<syntaxhighlight lang="lisp">(defun DegToDeg (a) (rem a 360))
(defun GradToGrad (a) (rem a 400))
(defun MilToMil (a) (rem a 6400))
Line 859:
{{libheader| System.Math}}
{{Trans|Go}}
<syntaxhighlight lang=Delphi"delphi">
program normalization_and_conversion;
 
Line 990:
=={{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.)
<syntaxhighlight lang="factor">USING: accessors combinators formatting inverse kernel math
math.constants quotations qw sequences units.si ;
IN: rosetta-code.angles
Line 1,070:
=={{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
 
Line 1,257:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 1,394:
=={{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.
<syntaxhighlight lang="groovy">import java.lang.reflect.Constructor
 
abstract class Angle implements Comparable<? extends Angle> {
Line 1,466:
 
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 Gradians getGrad(Number n) { new Gradians(n) }
Line 1,474:
 
Test:
<syntaxhighlight lang="groovy">Number.metaClass.mixin AngleCategory
 
[ -2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000 ].each { rawAngle ->
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).
 
<syntaxhighlight lang="haskell">{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
Line 1,608:
'''Instances of different angular units.'''
 
<syntaxhighlight lang="haskell">-- radians
newtype Rad = Rad Double
deriving (Eq, Ord, Num, Real, Fractional, RealFrac, Floating)
Line 1,716:
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]
Line 1,783:
=={{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,803:
</syntaxhighlight>
we can compose conversion sentences like
<syntaxhighlight lang="text">
as_degree 100 Gradian
90
</syntaxhighlight>
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,863:
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.text.DecimalFormat;
 
// Title: Angles (geometric), normalization and conversion
Line 2,019:
</pre>
=={{header|Javascript}}==
<syntaxhighlight lang=javaScript"javascript">
/*****************************************************************\
| Expects an angle, an origin unit and a unit to convert to, |
Line 2,235:
 
'''Preliminaries'''
<syntaxhighlight lang="jq">### Formatting
 
# Right-justify but do not truncate
Line 2,275:
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang="jq"># Normalization as per the task requirements
def mod($y):
(if . < 0 then -$y else 0 end) as $adjust
Line 2,338:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Formatting
 
d2d(d) = d % 360
Line 2,432:
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">import java.text.DecimalFormat as DF
 
const val DEGREE = 360.0
Line 2,489:
 
=={{header|Lua}}==
<syntaxhighlight 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 2,507:
end</syntaxhighlight>
Optionally:
<syntaxhighlight lang="lua">-- if you care..
assert(convert(-360,"degrees","degrees") == 0.0)
assert(convert(360,"degrees","degrees") == 0.0)
Line 2,586:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">ClearAll[NormalizeAngle, NormalizeDegree, NormalizeGradian, NormalizeMil, NormalizeRadian]
NormalizeAngle[d_, full_] := Module[{a = d},
If[Abs[a/full] > 4,
Line 2,621:
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim"nim">import math
import strformat
 
Line 2,737:
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 2,836:
=={{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"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: #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,923:
=={{header|Python}}==
===Python: Original===
<syntaxhighlight lang="python">PI = 3.141592653589793
TWO_PI = 6.283185307179586
 
Line 2,960:
 
===Python: using tkinter===
<syntaxhighlight lang="python">
''' Python 3.6.5 code using Tkinter graphical user interface.
Angles (geometric), normalization and conversion challenge.
Line 3,321:
{{trans|Common Lisp}}
 
<syntaxhighlight lang="racket">#lang racket
 
(define (rem n m)
Line 3,446:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang=perl6"raku" line>
my @units =
{ code => 'd', name => 'degrees' , number => 360 },
Line 3,543:
''(Much of the complexity is due to the requirement that negative angles must normalize to a negative number.)''
 
<syntaxhighlight lang=perl6"raku" line>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 3,651:
 
=={{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.*/
parse arg x /*obtain optional arguments from the CL*/
Line 3,769:
=={{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.
<syntaxhighlight lang="ruby">module Angles
BASES = {"d" => 360, "g" => 400, "m" => 6400, "r" => Math::PI*2 ,"h" => 24 }
Line 3,864:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::{
marker::PhantomData,
f64::consts::PI,
Line 4,000:
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">import Foundation
 
func normalize(_ f: Double, N: Double) -> Double {
Line 4,139:
=={{header|Vlang}}==
{{trans|Go}}
<syntaxhighlight lang="vlang">import math
import strconv
fn d2d(d f64) f64 { return math.mod(d, 360) }
Line 4,199:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var d2d = Fn.new { |d| d % 360 }
Line 4,299:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"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.);
Line 4,421:
=={{header|zkl}}==
{{trans|Raku}}
<syntaxhighlight lang="zkl">var [const]
tau=(0.0).pi*2,
units=Dictionary( // code:(name, units in circle)
Line 4,436:
})
;</syntaxhighlight>
<syntaxhighlight lang="zkl">codes:=units.keys;
println(" Angle Unit ",
codes.apply(fcn(k){ units[k][0] }).apply("%11s".fmt).concat(" "));
10,327

edits