{"id":66,"date":"2009-09-18T16:00:15","date_gmt":"2009-09-18T16:00:15","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2009\/09\/18\/how-do-i-test-for-nan-in-simulink-r2009b-new\/"},"modified":"2009-09-18T16:30:35","modified_gmt":"2009-09-18T16:30:35","slug":"how-do-i-test-for-nan-in-simulink-r2009b-new","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2009\/09\/18\/how-do-i-test-for-nan-in-simulink-r2009b-new\/","title":{"rendered":"How do I test for NaN in Simulink R2009b? (NEW!)"},"content":{"rendered":"<style>\r\n<!--\r\n \/* Font Definitions *\/\r\n @font-face\r\n\t{font-family:Wingdings;\r\n\tpanose-1:5 0 0 0 0 0 0 0 0 0;}\r\n@font-face\r\n\t{font-family:\"Cambria Math\";\r\n\tpanose-1:2 4 5 3 5 4 6 3 2 4;}\r\n@font-face\r\n\t{font-family:Calibri;\r\n\tpanose-1:2 15 5 2 2 2 4 3 2 4;}\r\n@font-face\r\n\t{font-family:Tahoma;\r\n\tpanose-1:2 11 6 4 3 5 4 4 2 4;}\r\n \/* Style Definitions *\/\r\n p.MsoNormal, li.MsoNormal, div.MsoNormal\r\n\t{margin-top:0in;\r\n\tmargin-right:0in;\r\n\tmargin-bottom:10.0pt;\r\n\tmargin-left:0in;\r\n\tline-height:115%;\r\n\tfont-size:11.0pt;\r\n\tfont-family:\"Calibri\",\"sans-serif\";}\r\na:link, span.MsoHyperlink\r\n\t{color:blue;\r\n\ttext-decoration:underline;}\r\na:visited, span.MsoHyperlinkFollowed\r\n\t{color:purple;\r\n\ttext-decoration:underline;}\r\npre\r\n\t{mso-style-link:\"HTML Preformatted Char\";\r\n\tmargin:0in;\r\n\tmargin-bottom:.0001pt;\r\n\tfont-size:10.0pt;\r\n\tfont-family:\"Courier New\";\r\n\tcolor:#1122AA;}\r\np.MsoAcetate, li.MsoAcetate, div.MsoAcetate\r\n\t{mso-style-link:\"Balloon Text Char\";\r\n\tmargin:0in;\r\n\tmargin-bottom:.0001pt;\r\n\tfont-size:8.0pt;\r\n\tfont-family:\"Tahoma\",\"sans-serif\";}\r\np.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph\r\n\t{margin-top:0in;\r\n\tmargin-right:0in;\r\n\tmargin-bottom:10.0pt;\r\n\tmargin-left:.5in;\r\n\tline-height:115%;\r\n\tfont-size:11.0pt;\r\n\tfont-family:\"Calibri\",\"sans-serif\";}\r\np.MsoListParagraphCxSpFirst, li.MsoListParagraphCxSpFirst, div.MsoListParagraphCxSpFirst\r\n\t{margin-top:0in;\r\n\tmargin-right:0in;\r\n\tmargin-bottom:0in;\r\n\tmargin-left:.5in;\r\n\tmargin-bottom:.0001pt;\r\n\tline-height:115%;\r\n\tfont-size:11.0pt;\r\n\tfont-family:\"Calibri\",\"sans-serif\";}\r\np.MsoListParagraphCxSpMiddle, li.MsoListParagraphCxSpMiddle, div.MsoListParagraphCxSpMiddle\r\n\t{margin-top:0in;\r\n\tmargin-right:0in;\r\n\tmargin-bottom:0in;\r\n\tmargin-left:.5in;\r\n\tmargin-bottom:.0001pt;\r\n\tline-height:115%;\r\n\tfont-size:11.0pt;\r\n\tfont-family:\"Calibri\",\"sans-serif\";}\r\np.MsoListParagraphCxSpLast, li.MsoListParagraphCxSpLast, div.MsoListParagraphCxSpLast\r\n\t{margin-top:0in;\r\n\tmargin-right:0in;\r\n\tmargin-bottom:10.0pt;\r\n\tmargin-left:.5in;\r\n\tline-height:115%;\r\n\tfont-size:11.0pt;\r\n\tfont-family:\"Calibri\",\"sans-serif\";}\r\nspan.BalloonTextChar\r\n\t{mso-style-name:\"Balloon Text Char\";\r\n\tmso-style-link:\"Balloon Text\";\r\n\tfont-family:\"Tahoma\",\"sans-serif\";}\r\nspan.HTMLPreformattedChar\r\n\t{mso-style-name:\"HTML Preformatted Char\";\r\n\tmso-style-link:\"HTML Preformatted\";\r\n\tfont-family:\"Courier New\";\r\n\tcolor:#1122AA;}\r\nspan.ln1\r\n\t{mso-style-name:ln1;\r\n\tcolor:#888888;\r\n\tfont-style:italic;}\r\nspan.ct1\r\n\t{mso-style-name:ct1;\r\n\tcolor:#117755;\r\n\tfont-style:italic;}\r\nspan.dt1\r\n\t{mso-style-name:dt1;\r\n\tcolor:#112266;}\r\nspan.pp1\r\n\t{mso-style-name:pp1;\r\n\tcolor:#992211;}\r\nspan.kw1\r\n\t{mso-style-name:kw1;\r\n\tcolor:#112266;}\r\n.MsoPapDefault\r\n\t{margin-bottom:10.0pt;\r\n\tline-height:115%;}\r\n@page Section1\r\n\t{size:8.5in 11.0in;\r\n\tmargin:1.0in 1.0in 1.0in 1.0in;}\r\ndiv.Section1\r\n\t{page:Section1;}\r\n \/* List Definitions *\/\r\n ol\r\n\t{margin-bottom:0in;}\r\nul\r\n\t{margin-bottom:0in;}\r\n-->\r\n<\/style>\r\n\r\n<p>Back in February 2009 I posted about <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/2009\/02\/04\/how-do-i-test-for-nan-in-my-model\/\">how\r\nto test for NaN in Simulink<\/a>.\u00a0 The approach I talked about was more of a\r\nlogical experiment based on the special properties of NaN than an ideal\r\nsoftware solution.\u00a0 In Simulink R2009b the <a\r\nhref=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/simulink\/slref\/relationaloperator.html\">Relational\r\nOperator block<\/a> got an upgrade to include isNaN.\u00a0 Let\u2019s see how it works.<\/p>\r\n\r\n<p><strong>Relational Operator Block Upgrade<\/strong><\/p>\r\n\r\n<p>The Relational Operator is often used to compare signals and\r\ntest for greater than (&gt;), equals (==), and more.\u00a0 In R2009b it has the\r\nfollowing new single input modes: isNaN, isFinite, and isInf<strong>.<\/strong><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/isNaNBlock.png\" alt=\"The Relational Operator in Simulink R2009b has the isNaN operation.\"><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/relOpParameters.png\" alt=\"The parameters for the relational operator block in Simulink R2009b\"><\/p>\r\n\r\n<p>These functions are familiar to those who program in MATLAB\r\nand they work the same way.\u00a0 When the input signal has the properties we are\r\ntesting for, the block outputs true.<\/p>\r\n\r\n<p><strong>How does it work?<\/strong><\/p>\r\n\r\n<p>I talked to my colleague Omar who upgraded the block and he\r\nshared with me some key points.\u00a0 He mentioned that the block handles:<\/p>\r\n\r\n<p><ul>\r\n        <li>real and complex signals<\/li>\r\n        <li>any data type supported by Simulink including fixed point types<\/li>\r\n        <li>endianness of your platform<\/li>\r\n    <\/ul>\r\n<\/p>\r\n\r\n<p>These new operators are only meaningful with data types that\r\ncan represent Inf and NaN.\u00a0\u00a0 <em>So how does it work with non-floating point\r\ntypes?<\/em>\u00a0 If you pass in an integer, or a fixed-point type that cannot\r\nrepresent NaN of Inf, the block returns false.\u00a0 This is a key point for\r\nworkflows where you might find yourself switching the data types in your design.\u00a0\r\nThis often happens when selecting proper fixed-point data type properties.\u00a0 If\r\nyou are running simulations to gather ideal double precision results, you may\r\nneed to handle Inf and NaN.\u00a0 When converting the model to fixed-point for implementation,\r\nthe algorithm does not require any modifications.<\/p>\r\n\r\n<p><strong>Generated Code<\/strong><\/p>\r\n\r\n<p>The code generated for the isNaN function looks like this:<\/p>\r\n\r\n<p style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><em><span style='font-size:10.0pt;font-family:\"Courier New\";color:#888888'>\u00a0\u00a0\r\n34\u00a0\u00a0 <\/span><\/em><span style='font-size:10.0pt;font-family:\"Courier New\";\r\ncolor:#1122AA'>\u00a0\u00a0<\/span><em><span style='font-size:10.0pt;font-family:\"Courier New\";\r\ncolor:#117755'>\/* Outport: \u2018<\/span><\/em><em><span style='font-size:10.0pt;\r\nfont-family:\"Courier New\";color:#117755'>&lt;Root&gt;\/Out1<\/span><\/em><em><span\r\nstyle='font-size:10.0pt;font-family:\"Courier New\";color:#117755'>\u2019\r\nincorporates:<\/span><\/em><br>\r\n\r\n<em><span style='font-size:10.0pt;font-family:\"Courier New\";color:#888888'>\u00a0\u00a0\r\n35\u00a0\u00a0 <\/span><\/em><em><span style='font-size:10.0pt;font-family:\"Courier New\";\r\ncolor:#117755'>\u00a0\u00a0\u00a0*\u00a0 Inport: \u2018<\/span><\/em><em><span style='font-size:10.0pt;\r\nfont-family:\"Courier New\";color:#117755'>&lt;Root&gt;\/In1<\/span><\/em><em><span\r\nstyle='font-size:10.0pt;font-family:\"Courier New\";color:#117755'>\u2019<\/span><\/em><br>\r\n\r\n<em><span style='font-size:10.0pt;font-family:\"Courier New\";color:#888888'>\u00a0\u00a0\r\n36\u00a0\u00a0 <\/span><\/em><em><span style='font-size:10.0pt;font-family:\"Courier New\";\r\ncolor:#117755'>\u00a0\u00a0\u00a0*\u00a0 RelationalOperator: \u2018<\/span><\/em><em><span style='font-size:10.0pt;\r\nfont-family:\"Courier New\";color:#117755'>&lt;Root&gt;\/Relational Operator<\/span><\/em><em><span\r\nstyle='font-size:10.0pt;font-family:\"Courier New\";color:#117755'>\u2019<\/span><\/em><br>\r\n\r\n<em><span style='font-size:10.0pt;font-family:\"Courier New\";color:#888888'>\u00a0\u00a0\r\n37\u00a0\u00a0 <\/span><\/em><em><span style='font-size:10.0pt;font-family:\"Courier New\";\r\ncolor:#117755'>\u00a0\u00a0\u00a0*\/<\/span><\/em><br>\r\n\r\n<em><span style='font-size:10.0pt;font-family:\"Courier New\";color:#888888'>\u00a0\u00a0\r\n38\u00a0\u00a0 <\/span><\/em><span style='font-size:10.0pt;font-family:\"Courier New\";\r\ncolor:#1122AA'>\u00a0\u00a0isNaNCode_Y.Out1 = <\/span><span style='font-size:10.0pt;font-family:\r\n\"Courier New\"'>rtIsNaN<\/span><span style='font-size:10.0pt;font-family:\r\n\"Courier New\";color:#1122AA'>(isNaNCode_U.In1);<\/span><br>\r\n\r\n\r\n\r\n<p>The function rtIsNaN is part of rt_nonfinite.c:<\/p>\r\n\r\n<pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span class=ln1>\u00a0\u00a0 61\u00a0\u00a0 <\/span><span class=ct1>\/* Test if value is not a number *\/<\/span><\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span\r\nclass=ln1>\u00a0\u00a0 62\u00a0\u00a0 <\/span><span style='color:#1122AA'>boolean_T<\/span> rtIsNaN(<span style='color:#1122AA'>real_T<\/span> value)<\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span\r\nclass=ln1>\u00a0\u00a0 63\u00a0\u00a0 <\/span><strong>{<\/strong><\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span class=ln1>\u00a0\u00a0 64\u00a0\u00a0 <\/span><\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span\r\nclass=ln1>\u00a0\u00a0\u00a065\u00a0\u00a0 <\/span><span style='color:#992211'>#<span class=pp1>if<\/span><\/span> <span\r\nclass=pp1>defined<\/span>(_MSC_VER) &amp;&amp; (_MSC_VER &lt;= 1200)<\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span\r\nclass=ln1>\u00a0\u00a0 66\u00a0\u00a0 <\/span><\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span class=ln1>\u00a0\u00a0\u00a067\u00a0\u00a0 <\/span>\u00a0\u00a0<span\r\nclass=ct1>\/* For MSVC 6.0, use a compiler specific comparison function *\/<\/span><\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span\r\nclass=ln1>\u00a0\u00a0 68\u00a0\u00a0 <\/span>\u00a0\u00a0<span class=kw1>return<\/span> _isnan(value)? 1U:0U;<\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span\r\nclass=ln1>\u00a0\u00a0 69\u00a0\u00a0 <\/span><\/pre><pre><span class=ln1>\u00a0\u00a0\u00a070\u00a0\u00a0 <\/span><span\r\nstyle='color:#992211'>#<span class=pp1>else<\/span><\/span><\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span\r\nclass=ln1>\u00a0\u00a0 71\u00a0\u00a0 <\/span><\/pre><pre><span class=ln1>\u00a0\u00a0\u00a072\u00a0\u00a0 <\/span>\u00a0\u00a0<span\r\nclass=kw1>return<\/span>((value!=value) ? 1U : 0U);<\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span class=ln1>\u00a0\u00a0 73\u00a0\u00a0 <\/span><\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span\r\nclass=ln1>\u00a0\u00a0\u00a074\u00a0\u00a0 <\/span><span style='color:#992211'>#<span class=pp1>endif<\/span><\/span><\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span\r\nclass=ln1>\u00a0\u00a0 75\u00a0\u00a0 <\/span><\/pre><pre style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal'><span class=ln1>\u00a0\u00a0\u00a076\u00a0\u00a0 <\/span><strong>}<\/strong><\/pre>\r\n\r\n\r\n\r\n<p><strong>Further Optimization using Target Function Library<\/strong><\/p>\r\n\r\n<p>If your embedded target environment has a special function\r\nfor handling tests for NaN, Inf or Finiteness you can use the <a\r\nhref=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/ecoder\/ug\/brc_o1j.html\">Target\r\nFunction Library<\/a> to replace the call to rtIsNaN with that call.<\/p>\r\n\r\n<p><strong>What do you think?<\/strong><\/p>\r\n\r\n<p>Will you use the isInf, isNaN and isFinite mode of this\r\nblock?\u00a0 Leave a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=66&amp;#comment\">comment\r\nhere<\/a> to tell me how.<\/p>","protected":false},"excerpt":{"rendered":"<p>\r\n\r\n\r\n\r\nBack in February 2009 I posted about how\r\nto test for NaN in Simulink.\u00a0 The approach I talked about was more of a\r\nlogical experiment based on the special properties of NaN than an... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2009\/09\/18\/how-do-i-test-for-nan-in-simulink-r2009b-new\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[76,16],"tags":[104,94,441],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/66"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=66"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/66\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}