Algorithm Request: Comparison against a range that wraps
| Author | Message | ||||
| vegipete Guru Joined: 29/01/2013 Location: CanadaPosts: 1182 |
Does anyone have or can figure out an _elegant_ algorithm for determining if two values are within a certain range of each other when the allowable range wraps around? For example, suppose I want to know if two times are within 5 minutes of each other. Say 12:30 and and 12:27. "IF ABS(time1 - time2) < deltatime THEN true" works fine except... it fails with, say, 12:58 and 1:02 (assuming a 12 hour clock.) I have something with 5 IF statements, plus some MODs, which just strikes me as ugly. Is there a better way? Visit Vegipete's *Mite Library for cool programs. |
||||