NaN values
In some cases error handling by error codes is not the best choice, in particular if operations return undefined values on account of unusual, but in principle possible input data, or if values are to be excluded from the processing.
The IEEE 754 standard defines symbolic values of the category NaN (Not a Number) for these purposes. In the following situations these are generated or taken into account in the TwinCAT 3 Condition Monitoring library:
- If insufficient valid values are available for a statistical evaluation, the result is output with NaN.
- If certain values are to be excluded from a statistical evaluation, this is achieved by the function block ignoring NaN values at the input.
- If the input vector of the frequency analysis of a time series contains one or more NaN values, the entire output vector is filled with NaN. This property can be used to mark results as undefined in case a gap in the input signal leads to jumps in the time series. Because it is not possible to calculate correct spectra on the basis of fragmented time series.
The ability of a function block to create NaN values is noted in its description.
The following points rank among the main features of NaN values:
- All arithmetic operations that use NaN as input data return NaN as the result.
- All relational operators =, !=, > < >= <= always return the value False if at least one of the operands is NaN.
- The standard C functions
isnan()
or_isnan()
or the PLC functions LrealIsNaN() and RealIsNaN (Tc2_Utilities library) return the value True if the argument has the value NaN. - The expression
isnan(a)
is equivalent to the expression!(a == a)
orNOT(a = a)
.
The fact that NaN values reproduce themselves when used in further calculations is advantageous in that invalid values cannot be overlooked.
CAUTION | |
Software malfunctions NaN values may only be used in PLC libraries, in particular as control values in functions for Motion Control and for drive control, if they are expressly approved! Otherwise NaN values can lead to potentially dangerous malfunctions of the software concerned! |
CAUTION | |
Floating point exceptions If NaNs are to be used and processed in the application, the FP exceptions must be switched off. Otherwise, comparisons with NaN can lead to an exception, which will cause a stop of the runtime and possible machine damage. |
Further explanations on the option to switch the FP exceptions off and on can be found in chapter Task settings.