sydan
sydan

Reputation: 298

Fast lightweight method of detecting skipped values in a cyclic sequence

I have a single value that, over time, increases from 1 to 6 and then returns to 1. So over several frames the sequence seen would be:

1, 2, 3, 4, 5, 6, 1, 2, 3, 4...

If a hardware device is defective one of those numbers may not read correctly and so the sequence may look like this:

1, 2, 6, 4, 5, 6, 1, 2, 6, 4...

What is a fast, short method of detecting this inconsistency?

Note:

Upvotes: 1

Views: 47

Answers (1)

Janne Karila
Janne Karila

Reputation: 25207

You can use the modulo operator:

(current_value - previous_value + 6) mod 6 == 1

The +6 is there to ensure a positive argument to mod. Different languages treat negative values differently, but if -5 mod 6 evaluates to 1 in yours, you can omit the +6.

Upvotes: 1

Related Questions