Guy on Simulink

Simulink & Model-Based Design

How do I test for NaN in Simulink R2009b? (NEW!)

Back in February 2009 I posted about how to test for NaN in Simulink.  The approach I talked about was more of a logical experiment based on the special properties of NaN than an ideal software solution.  In Simulink R2009b the Relational Operator block got an upgrade to include isNaN.  Let’s see how it works.

Relational Operator Block Upgrade

The Relational Operator is often used to compare signals and test for greater than (>), equals (==), and more.  In R2009b it has the following new single input modes: isNaN, isFinite, and isInf.

The Relational Operator in Simulink R2009b has the isNaN operation.

The parameters for the relational operator block in Simulink R2009b

These functions are familiar to those who program in MATLAB and they work the same way.  When the input signal has the properties we are testing for, the block outputs true.

How does it work?

I talked to my colleague Omar who upgraded the block and he shared with me some key points.  He mentioned that the block handles:

  • real and complex signals
  • any data type supported by Simulink including fixed point types
  • endianness of your platform

These new operators are only meaningful with data types that can represent Inf and NaN.   So how does it work with non-floating point types?  If you pass in an integer, or a fixed-point type that cannot represent NaN of Inf, the block returns false.  This is a key point for workflows where you might find yourself switching the data types in your design.  This often happens when selecting proper fixed-point data type properties.  If you are running simulations to gather ideal double precision results, you may need to handle Inf and NaN.  When converting the model to fixed-point for implementation, the algorithm does not require any modifications.

Generated Code

The code generated for the isNaN function looks like this:

   34     /* Outport: ‘<Root>/Out1’ incorporates:
   35      *  Inport: ‘<Root>/In1
   36      *  RelationalOperator: ‘<Root>/Relational Operator
   37      */
   38     isNaNCode_Y.Out1 = rtIsNaN(isNaNCode_U.In1);

The function rtIsNaN is part of rt_nonfinite.c:

   61   /* Test if value is not a number */
   62   boolean_T rtIsNaN(real_T value)
   63   {
   64   
   65   #if defined(_MSC_VER) && (_MSC_VER <= 1200)
   66   
   67     /* For MSVC 6.0, use a compiler specific comparison function */
   68     return _isnan(value)? 1U:0U;
   69   
   70   #else
   71   
   72     return((value!=value) ? 1U : 0U);
   73   
   74   #endif
   75   
   76   }

Further Optimization using Target Function Library

If your embedded target environment has a special function for handling tests for NaN, Inf or Finiteness you can use the Target Function Library to replace the call to rtIsNaN with that call.

What do you think?

Will you use the isInf, isNaN and isFinite mode of this block?  Leave a comment here to tell me how.

|
  • print
  • send email

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。