# Find the reference angle

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: 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:

## 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!

## Test Cases

70      ->  70 deg
135     ->  45 deg
210     ->  30 deg
-60     ->  60 deg
91      ->  89 deg
610     ->  70 deg
-1000   ->  80 deg

• What do we do on input of 90? – lirtosiast Jul 25 '15 at 5:51
• @ThomasKwa it would be 90 from either direction. – Maltysen Jul 25 '15 at 5:53
• 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). – lirtosiast Jul 25 '15 at 6:02
• Is the input always an integer? – lirtosiast Jul 25 '15 at 6:08
• Since non-integer angles are allowed, you should include one in the test cases. – Dennis Jul 25 '15 at 13:32

# Pyth, 17 bytes

+hS%R180,Q_Q" deg


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

Try it online.

## Pyth, 18 bytes

+.a-%+QK90yKK" deg


Try it online.

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

# 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.

## CJam (21 bytes)

180qd1$%z_@@-e<" deg"  Online demo # 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$


# Mathematica, 22 31

ToString@Abs@Mod[#,180,-90]deg&

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

## 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)


# Pyth 25 30 bytes

Painfully uncreative solution, using trig functions. Permalink

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


Any suggestions welcome.

• Got it down to 28 by taking the .t calls and putting them in a fold: +.R.t.a.tF[Q7Z3)6?%Q1Q0" deg – Maltysen Jul 25 '15 at 8:13
• Neat. I noticed it doesn't appear to work with a floating point input: 90.5 => 90.0 deg, though. – Winny Jul 25 '15 at 18:08
• That's because yours doesn't either: pyth.herokuapp.com/… – Maltysen Jul 25 '15 at 18:11
• 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 – Winny Jul 25 '15 at 18:23

## 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'

• 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. – Peter Taylor Jul 25 '15 at 6:38
• Replacing the first 180 with (t=180) then replacing the following 180s with t would probably save a few bytes – Downgoat Jul 25 '15 at 16:10
• @vihan1086 It's the same – rink.attendant.6 Jul 25 '15 at 20:33

# J, 26 bytes

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


Same as xnor's method.

## 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

• This doesn't append the ' deg' at the end though. (Did not downvote.) – Legendre Jul 25 '15 at 16:16
• @Legendre Oops. Thanks! Corrected – Luis Mendo Jul 26 '15 at 2:24

(a:Int)⇒s"${90-(a%180-90).abs} deg"  # 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!

## PHP, 44 bytes

Takes one command line parameter. Again PHP suffers from the same issue as JavaScript.

<?=90-abs(($argv%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)))))+' deg';

• Doesn't PHP use . for concatenation? – Downgoat Jul 26 '15 at 3:57
• @vihan1086 Yes, PHP uses .. Downvoted until it is fixed – Ismael Miguel Jul 26 '15 at 11:33