Testing variables for ~ equal

cpedw

Senior Member
Is there a neat way to test if 2 variables are different by less than a small amount? For my application I need to be able to test across a rollover as well.

The application is an MSF clock. This normally returns time in hours and minutes. I convert that to total minutes, a word variable mn in range 0-1440. When the MSF clock receiver fails, I use the 14M2 TIME to increment mn as the minutes pass. I know it's not very accurate but it generally performs well enough - the clock receiver never fails for more than a handful of minutes.

Very occasionally, in spite of parity checking the results, the MSF clock returns the wrong time. I hope to apply a sanity check to each MSF-returned time by comparing new mn to old mn. Because of inaccuracy in TIME, it's feasible that new mn could be a bit different from old mn + 1.

Arithmetically, I want to test if |oldmn-newmn|<smalldiff where smalldiff is ~5. My maths isn't up to describing the test when oldmn and/or newmn is near to 0 or 1440.
 

AllyCat

Senior Member
Hi,

You could subtract one variable from the other (it doesn't matter which), add 5 (or whatever smalldiff you require) and divide by 10 (or twice smalldiff). Then if the result is equal to zero they were within the small difference. To work with a rollover, I think you can add 1445 instead, calculate the modulus (remainder) by using // 1440 before the division and comparison.

Code:
w1 = w2 - w3 + 1445 // 1440 / 10
if w1 = 0 then
   sertxd("Within 5 minutes")
endif
Cheers, Alan.
 

cpedw

Senior Member
Many thanks for that. My rudimentary tests show that it works exactly as I specified.
Here's a version that would be easily adapted for other criteria:
Code:
SYMBOL test  = b1
SYMBOL oldmn = w1
SYMBOL newmn = w2

SYMBOL diff  = 4
SYMBOL diff2 = 2*diff

SYMBOL rollo = 1440


oldmn = 0
newmn = 1438

    test = oldmn - newmn + rollo + diff // rollo / diff2
    sertxd(#newmn," ")
    if test = 0 then
       sertxd("Within ",#diff," minutes")
    endif
 
Top