Given two times on a clock, I need to calculate the smallest time difference between them. At first this seems obvious in cases like time difference between 1:00 and 3:00 or 6:00 and 11:00.
$f(1,3) = 2$
$f(6,11) = 5$
But what about cases that goes past 12:00? Like difference between 8:00 and 1:00 or 10:00 and 2:00?
$f(8,1) = 5$ # in base 12
$f(10,2) = 4$ # in base 12
I realize that this is the same problem as finding the smallest angle between two angles on a circle. From what I've figured out the answer is something involving the inverse cosine, but I'm not sure how to relate that from base 360 to base 24. I know in computer science, the arctangent is used, but I can't find much.
$f(350,20) = 30$ # in base 360
All in all, I need a trig based function to calculate the smallest difference between two circular data.
Consider data with a period of $p$. For example a clock's minute reading would have $p=60$, angles on a circle would have $p=2\pi$ or $p=360$, etc.
The function for which you search is: $$f(a, b) = \frac p{2\pi}\arccos\left(\cos\left(\frac{2\pi(a-b)}p\right)\right).$$
Alternatively, you could use a non-trig-based version (which might run faster on a computer): $$D(a, b) = \frac p2-\left|\frac p2-((a-b)\text{ mod }p)\right|.$$
Feel free to ask for clarification or explanation.