I was given this interview question recently:
Given a 12-hour analog clock, compute in degree the smaller angle between the hour and minute hands. Be as precise as you can.
I’m wondering what’s the simplest, most readable, most precise algorithm is. Solution in any language is welcome (but do explain it a bit if you think it’s necessary).
It turns out that Wikipedia does have the best answer:
Basically:
0.5degrees per minute6degrees per minuteProblem solved.
And precision isn’t a concern because the fractional part is either
.0or.5, and in the range of0..360, all of these values are exactly representable indouble.