17
\$\begingroup\$

This is a fairly simple question. According to this random website I found — Web Archive, a reference angle is the acute angle formed by the terminal side of the given angle and the x-axis. You have to write a program to find these.

I don't remember anything from algebra 2, what does this mean?

Angles are usually given in standard form, which is measured by placing one side of the angle and measuring to the other side, counterclockwise, like so:

Standard form

This will be your input. Your output will be the reference angle of this. You can think of this as basically the smallest distance from the terminal side to the x-axis. Except it's an angle, not a distance. Here are some examples:

Quad 2 ref Quad 1 ref

Quad 3 ref Quad 4 ref

Clarifications

  • All the defaults on inputs and submissions.
  • Angles is in degrees.
  • Negative angles, non-integer angles, and angles > 360 deg are allowed.
  • Output in form 60 deg.
  • This is , so shortest code in bytes wins!

Another helpful link.

Test Cases

70      ->  70 deg
135     ->  45 deg
210     ->  30 deg
-60     ->  60 deg
91      ->  89 deg
610     ->  70 deg
-1000   ->  80 deg
\$\endgroup\$
  • 1
    \$\begingroup\$ What do we do on input of 90? \$\endgroup\$ – lirtosiast Jul 25 '15 at 5:51
  • 2
    \$\begingroup\$ @ThomasKwa it would be 90 from either direction. \$\endgroup\$ – Maltysen Jul 25 '15 at 5:53
  • 1
    \$\begingroup\$ A short solution is abs(arcsin(sin(input)))+" deg", though I don't know which language would win (it would need to support degrees and string operations). \$\endgroup\$ – lirtosiast Jul 25 '15 at 6:02
  • \$\begingroup\$ Is the input always an integer? \$\endgroup\$ – lirtosiast Jul 25 '15 at 6:08
  • 6
    \$\begingroup\$ Since non-integer angles are allowed, you should include one in the test cases. \$\endgroup\$ – Dennis Jul 25 '15 at 13:32

14 Answers 14

7
\$\begingroup\$

Pyth, 17 bytes

+hS%R180,Q_Q" deg

Coincidentally, this is equivalent to @xnor's answer.

Try it online.

\$\endgroup\$
6
\$\begingroup\$

Pyth, 18 bytes

+.a-%+QK90yKK" deg

Try it online.

\$\endgroup\$
  • \$\begingroup\$ got it down to 19 using your algo but with inline assign, feel free to use: pyth.herokuapp.com/… \$\endgroup\$ – Maltysen Jul 25 '15 at 6:44
  • \$\begingroup\$ @Maltysen: Thanks, found another improvement as well \$\endgroup\$ – grovesNL Jul 25 '15 at 6:47
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

CJam (21 bytes)

180qd1$%z_@@-e<" deg"

Online demo

\$\endgroup\$
4
\$\begingroup\$

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
$
\$\endgroup\$
3
\$\begingroup\$

Mathematica, 22 31

ToString@Abs@Mod[#,180,-90]deg&
\$\endgroup\$
  • 1
    \$\begingroup\$ @LegionMammal978 If I just multiply it by deg, 0 deg will become 0. \$\endgroup\$ – alephalpha Jul 26 '15 at 4:18
  • 1
    \$\begingroup\$ Since you don't care about the output type, you can save 2 bytes like this: Row@{Abs@Mod[#,180,-90],deg}& \$\endgroup\$ – LegionMammal978 Jul 26 '15 at 10:05
3
\$\begingroup\$

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)
\$\endgroup\$
3
\$\begingroup\$

Pyth 25 30 bytes

Painfully uncreative solution, using trig functions. Permalink

+.R.t.a.t.t.tQ7Z3 6?%Q1Q0" deg

Any suggestions welcome.

\$\endgroup\$
  • 1
    \$\begingroup\$ Got it down to 28 by taking the .t calls and putting them in a fold: +.R.t.a.tF[Q7Z3)6?%Q1Q0" deg \$\endgroup\$ – Maltysen Jul 25 '15 at 8:13
  • \$\begingroup\$ Neat. I noticed it doesn't appear to work with a floating point input: 90.5 => 90.0 deg, though. \$\endgroup\$ – Winny Jul 25 '15 at 18:08
  • \$\begingroup\$ That's because yours doesn't either: pyth.herokuapp.com/… \$\endgroup\$ – Maltysen Jul 25 '15 at 18:11
  • \$\begingroup\$ Interesting catch. With input -60.5 => 60.5 deg but with 60.5 => 60.0 deg. Looks like an issue with Pyth -- Example using pyth/macros.py \$\endgroup\$ – Winny Jul 25 '15 at 18:23
1
\$\begingroup\$

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'
\$\endgroup\$
  • 5
    \$\begingroup\$ I assume it was a design decision by a PDP hardware engineer which is responsible for the fact that several decades later we still have very few languages which implement % sensibly. \$\endgroup\$ – Peter Taylor Jul 25 '15 at 6:38
  • \$\begingroup\$ Replacing the first 180 with (t=180) then replacing the following 180s with t would probably save a few bytes \$\endgroup\$ – Downgoat Jul 25 '15 at 16:10
  • \$\begingroup\$ @vihan1086 It's the same \$\endgroup\$ – rink.attendant.6 Jul 25 '15 at 20:33
1
\$\begingroup\$

J, 26 bytes

m=:180&|
' deg',~m@-":@<.m

Same as xnor's method.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • 4
    \$\begingroup\$ This doesn't append the ' deg' at the end though. (Did not downvote.) \$\endgroup\$ – Legendre Jul 25 '15 at 16:16
  • \$\begingroup\$ @Legendre Oops. Thanks! Corrected \$\endgroup\$ – Luis Mendo Jul 26 '15 at 2:24
1
\$\begingroup\$

Scala, 40 35 Characters

(a:Int)⇒s"${90-(a%180-90).abs} deg"
\$\endgroup\$
0
\$\begingroup\$

Tcl, 83 bytes

proc R a {puts [expr [set a $a%360]>90?$a>180?$a>270?360-$a:$a-180:180-$a:$a]\ deg}

Try it online!

\$\endgroup\$
-1
\$\begingroup\$

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';
\$\endgroup\$
  • 1
    \$\begingroup\$ Doesn't PHP use . for concatenation? \$\endgroup\$ – Downgoat Jul 26 '15 at 3:57
  • \$\begingroup\$ @vihan1086 Yes, PHP uses .. Downvoted until it is fixed \$\endgroup\$ – Ismael Miguel Jul 26 '15 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.