| Bytes | Lang | Time | Link |
|---|---|---|---|
| 031 | AWK | 250321T170720Z | xrs |
| 032 | Raku Perl 6 rakudo | 250321T165518Z | xrs |
| 074 | Tcl | 181227T234859Z | sergiol |
| 035 | Scala | 150726T035240Z | gilad ho |
| 044 | Matlab | 150725T134139Z | Luis Men |
| 025 | dc | 150725T211405Z | Digital |
| 026 | J | 150725T092848Z | Legendre |
| 017 | Pyth | 150725T082856Z | isaacg |
| 044 | PHP | 150725T064641Z | rink.att |
| 018 | Pyth | 150725T062121Z | grovesNL |
| 043 | JavaScript ES6 | 150725T063634Z | rink.att |
| 021 | CJam | 150725T063515Z | Peter Ta |
| 030 | Pyth 25 | 150725T061440Z | Winny |
| 034 | Python 2 | 150725T060627Z | Sp3000 |
| 035 | Python 2 | 150725T055124Z | xnor |
| 031 | Mathematica | 150725T055406Z | alephalp |
Raku (Perl 6) (rakudo), 32 bytes
->\x{say x%180 min-x%180," deg"}
Seemed to be tightest algo around.
Tcl, 74 bytes
proc R a {puts [expr [set a $a%360]>90?$a>270?360-$a:abs($a-180):$a]\ deg}
Scala, 40 35 Characters
(a:Int)⇒s"${90-(a%180-90).abs} deg"
Matlab, 44
Using an anonymous function:
f=@(x)[num2str(min(mod([x -x],180))) ' deg']
Example:
>> f=@(x)[num2str(min(mod([x -x],180))) ' deg']
f =
@(x)[num2str(min(mod([x,-x],180))),' deg']
>> f(70)
ans =
70 deg
>> f(210)
ans =
30 deg
>> f(-1000)
ans =
80 deg
dc, 25
90?90+d*v180%-d*vn[ deg]p
Note dc uses _ as a -ve sign instead of -.
Test output:
$ for test in 70 135 210 _60 91 610 _1000; do dc -e'90?90+d*v180%-d*vn[ deg]p' <<< $test ; done
70 deg
45 deg
30 deg
60 deg
89 deg
70 deg
80 deg
$
PHP, 44 bytes
Takes one command line parameter. Again PHP suffers from the same issue as JavaScript.
<?=90-abs(($argv[1]%180+180)%180-90)+' deg';
Using the canonical solution requires 53 bytes (the degrees and radians conversion takes a lot of characters):
<?=rad2deg(abs(asin(sin(deg2rad($argv[1])))))+' deg';
JavaScript (ES6), 43 bytes
Similar to Sp3000's answer, though the modulo is quite lengthy due to the behaviour in JavaScript.
f=x=>90-Math.abs((x%180+180)%180-90)+' deg'
Demo
Code is rewritten in ES5 for browser compatibility:
function f(x){return 90-Math.abs((x%180+180)%180-90)+' deg'}
// Snippet stuff
console.log = function(x){document.body.innerHTML += x + '<br>'};
[70,135,210,-60,91,610,-1000].map(function(v){console.log(f(v))});
CoffeeScript, 45 bytes
f=(x)->90-Math.abs((x%180+180)%180-90)+' deg'
Pyth 25 30 bytes
Painfully uncreative solution, using trig functions. Permalink
+.R.t.a.t.t.tQ7Z3 6?%Q1Q0" deg
Any suggestions welcome.
Python 2, 34 bytes
lambda x:`90-abs(x%180-90)`+' deg'
Using "%d deg" string formatting would be longer because of the needed parentheses.
Python 2, 35
lambda x:`min(x%180,-x%180)`+' deg'
The smaller of the angle and its negative modulo 180. Effectively converts the angle to the range from 0 to 180 and takes the distance to the closer of the endpoints. Note that -x%180 groups as (-x)%180.
Same length with string formatting for Python 3:
lambda x:'%d deg'%min(x%180,-x%180)
Mathematica, 22 31
ToString@Abs@Mod[#,180,-90]deg&