

\section{Variables Bounds checks}

Variables read are often expected to fall within a certain range.
A voltage reading for instance might be expected to be, say 2.5V.
It may be necessary to check this periodically.
Because of niose and acceptable drift factors of components as they age
expecting it to read exactly 2.5V would be impractical, and would
probably cause a nuisance failure at some time in the future.

The solution to this is to apply a range, or a plus minus acceptable value.

$$ diff = signal - expected $$

The absolute value of this difference can be used and compared to
the acceptable range.

The C ABS macro is useful for this.

\begin{verbatim}
 #define ABS(x) if (x > 0) : (x) : (-x)
\end{verbatim}

Care must be taken however when passing parameters.

For instance this may look acceptable in C

\begin{verbatim}
 if (ABS(signal - expected) > THRESHOLD )
    raise_error();
\end{verbatim}

It expands to

\begin{verbatim}
  if ( signal - expected ? (signal - expected) : -(signal - expected) > THRESHOLD )
     raise_error();
\end{verbatim}

What ths has done is put \textbf{-(signal - expected) > THRESHOLD} as the final argument to the macro.

The C operator greater than, $>$, binds higher than than $?:$ so the results you will get will
not be what you expect. The correct way to perform  put the ABS call in brackets.

\begin{verbatim}
 if ( (ABS(signal - expected)) > THRESHOLD )
    raise_error();
\end{verbatim}