{"id":44,"date":"2006-07-05T14:55:36","date_gmt":"2006-07-05T19:55:36","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=44"},"modified":"2018-05-01T10:54:41","modified_gmt":"2018-05-01T15:54:41","slug":"when-is-a-numeric-result-not-a-number","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/07\/05\/when-is-a-numeric-result-not-a-number\/","title":{"rendered":"When is a Numeric Result Not a Number?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Quick answer: when the result is a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/nan.html\"><tt>NaN<\/tt><\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">NaNs in Arithmetic<\/a><\/li>\r\n         <li><a href=\"#3\">NaNs as Placeholders<\/a><\/li>\r\n         <li><a href=\"#4\">How to Find or Compare to NaN<\/a><\/li>\r\n         <li><a href=\"#10\">NaN issues<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>NaNs in Arithmetic<a name=\"1\"><\/a><\/h3>\r\n   <p>MATLAB has followed the <a title=\"http:\/\/grouper.ieee.org\/groups\/754\/ (link no longer working)\">IEEE 754: Standard for Binary Floating-Point Arithmetic<\/a> for doing arithmetic from the early days, and included tools and functionality to achieve similar results on machines that\r\n      did not comform to the IEEE standards, e.g., Digital Equipment Corporations VAX computers.  In addition, we made choices in\r\n      MATLAB to allow division by 0 rather than causing an error on machine architectures that could handle this behavior (allowed\r\n      in the IEEE standard).  In allowing division by 0, however, we not only encounter cases in which we divide nonzero positive\r\n      numbers and negative numbers by 0 (yielding <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/inf.html\"><tt>Inf<\/tt><\/a> and <tt>-Inf<\/tt> respectively, but we also encounter computations that result in undefined mathematical outcomes such as\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">inf-inf<\/pre><pre style=\"font-style:oblique\">ans =\r\n   NaN\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">0\/0<\/pre><pre style=\"font-style:oblique\">Warning: Divide by zero.\r\nans =\r\n   NaN\r\n<\/pre><h3>NaNs as Placeholders<a name=\"3\"><\/a><\/h3>\r\n   <p>In addition to allowing <tt>NaN<\/tt> as the result of numeric operations, we've also encouraged their use for marking information such as missing data. If we\r\n      were gathering some data for some specific times, and missed collecting one of the values, we might end up with data that\r\n      look like this:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">ts = 0:5;\r\nvals = [0.2 4.3 -1.6 NaN -2.0 1.2]\r\nplot(ts,vals,<span style=\"color: #A020F0\">'m*-'<\/span>)<\/pre><pre style=\"font-style:oblique\">vals =\r\n  Columns 1 through 5 \r\n    0.2000    4.3000   -1.6000       NaN   -2.0000\r\n  Column 6 \r\n    1.2000\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/44\/inanity_01.png\"> <h3>How to Find or Compare to NaN<a name=\"4\"><\/a><\/h3>\r\n   <p>According to the IEEE standard, <tt>NaN<\/tt> is not a number and not equal to anything, including itself.  So when looking for <tt>NaN<\/tt> values in an array, you can't do the \"obvious\"\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">vals == NaN<\/pre><pre style=\"font-style:oblique\">ans =\r\n     0     0     0     0     0     0\r\n<\/pre><p>As you can see, the comparison is always <tt>false<\/tt>.  So how do we look for them?  Depending on what you are doing, you will find these functions helpful:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/isnan.html\"><tt>isnan<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/isequalwithequalnans.html\"><tt>isequalwithequalnans<\/tt><\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Though a mouthful to say or write, <tt>isequalwithequalnans<\/tt> allows you to carry out comparisons between arrays matching values and shape, without allowing scalar expansion.\r\n   <\/p>\r\n   <p>Another way to make look for <tt>NaN<\/tt> is frequently cited on the MATLAB newsgroup.  See <a href=\"\">this thread and comments<\/a> by Paul Bodin for an example.  The idea here is that since\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">NaN == NaN<\/pre><pre style=\"font-style:oblique\">ans =\r\n     0\r\n<\/pre><p>always produces a <tt>false<\/tt> result, we can find NaNs simply by looking for places in which the value doesn't equal itself.  These are NaNs, and other\r\n      values are not.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">ourNaNs = vals(vals~=vals)<\/pre><pre style=\"font-style:oblique\">ourNaNs =\r\n   NaN\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">nonNaNs = vals(vals==vals)<\/pre><pre style=\"font-style:oblique\">nonNaNs =\r\n    0.2000    4.3000   -1.6000   -2.0000    1.2000\r\n<\/pre><p>My preference is to use <tt>isnan<\/tt> instead because I find the code much more readable when I revisit it later.  However, in some cases, depending on the amount\r\n      of NaN values (and perhaps other considerations), the constructs just above are sometimes faster. Here are a couple of additional\r\n      links to newsgroup threads talking more about this.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"http:\/\/tinyurl.com\/beo79\">http:\/\/tinyurl.com\/beo79<\/a><\/li>\r\n         <li><a href=\"http:\/\/tinyurl.com\/e38sc\">http:\/\/tinyurl.com\/e38sc<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>NaN issues<a name=\"10\"><\/a><\/h3>\r\n   <p>Some users find themselves experiencing collisions between <tt>NaN<\/tt> for missing data vs. <tt>NaN<\/tt> as a result of undefined numerical operations.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Have you encountered such collisions?<\/li>\r\n         <li>Do you find yourself wishing for more placeholder values?<\/li>\r\n         <li>What else would you like to use <tt>NaN<\/tt> or something similar for?\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Let's hear your thoughts.\r\n   <\/p>\r\n   <p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br>\r\n      Published with MATLAB&reg; 7.2<br><\/p>\r\n<\/div>\r\n<!--\r\n##### SOURCE BEGIN #####\r\n%% When is a Numeric Result Not a Number?\r\n% Quick answer: when the result is a \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/nan.html |NaN|>.\r\n% \r\n\r\n%% NaNs in Arithmetic\r\n% MATLAB has followed the  \r\n% <http:\/\/grouper.ieee.org\/groups\/754\/ IEEE 754: Standard for Binary Floating-Point Arithmetic>\r\n% for doing arithmetic from the early days, and included tools and\r\n% functionality to achieve similar results on machines that did not comform\r\n% to the IEEE standards, e.g., Digital Equipment Corporations VAX\r\n% computers.  In addition, we made choices in MATLAB to allow division by 0\r\n% rather than causing an error on machine architectures that could handle \r\n% this behavior (allowed in the IEEE standard).  In allowing\r\n% division by 0, however, we not only encounter cases in which we divide\r\n% nonzero positive numbers and negative numbers by 0 (yielding\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/inf.html |Inf|> \r\n% and |-Inf| respectively, but we also encounter computations that result\r\n% in undefined mathematical outcomes such as\r\ninf-inf\r\n%%\r\n0\/0\r\n%% NaNs as Placeholders\r\n% In addition to allowing |NaN| as the result of numeric operations, we've\r\n% also encouraged their use for marking information such as missing data.\r\n% If we were gathering some data for some specific times, and missed\r\n% collecting one of the values, we might end up with data that look like\r\n% this:\r\nts = 0:5;\r\nvals = [0.2 4.3 -1.6 NaN -2.0 1.2]\r\nplot(ts,vals,'m*-')\r\n\r\n%% How to Find or Compare to NaN\r\n% According to the IEEE standard, |NaN| is not a number and not equal to\r\n% anything, including itself.  So when looking for |NaN| values in an\r\n% array, you can't do the \"obvious\"\r\nvals == NaN\r\n%%\r\n% As you can see, the comparison is always |false|.  So how do we look for\r\n% them?  Depending on what you are doing, you will find these functions\r\n% helpful:\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/isnan.html |isnan|> \r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/isequalwithequalnans.html |isequalwithequalnans|> \r\n%\r\n% Though a mouthful to say or write, |isequalwithequalnans| allows you to\r\n% carry out comparisons between arrays matching values and shape, without\r\n% allowing scalar expansion.\r\n\r\n%% \r\n% Another way to make look for |NaN| is frequently cited on the MATLAB\r\n% newsgroup.  See \r\n% < this thread and comments>\r\n% by Paul Bodin for an example.  The idea here is that since\r\nNaN == NaN\r\n%%\r\n% always produces a |false| result, we can find |NaN|s simply by looking\r\n% for places in which the value doesn't equal itself.  These are |NaN|s,\r\n% and other values are not.\r\nourNaNs = vals(vals~=vals)\r\n%%\r\nnonNaNs = vals(vals==vals) \r\n%%\r\n% My preference is to use |isnan| instead because I find the code much more\r\n% readable when I revisit it later.  However, in some cases, depending on\r\n% the amount of NaN values (and perhaps other considerations), the\r\n% constructs just above are sometimes faster.\r\n% Here are a couple of additional links to newsgroup threads talking more\r\n% about this.\r\n%\r\n% * <http:\/\/tinyurl.com\/beo79>\r\n% * <http:\/\/tinyurl.com\/e38sc> \r\n%\r\n%% NaN issues\r\n% Some users find themselves experiencing collisions between |NaN| for\r\n% missing data vs. |NaN| as a result of undefined numerical operations.\r\n% \r\n% * Have you encountered such collisions?\r\n% * Do you find yourself wishing for more placeholder values?\r\n% * What else would you like to use |NaN| or something similar for?\r\n%\r\n% Let's hear <http:?p=44\/Respond your thoughts>.\r\n##### SOURCE END #####\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Quick answer: when the result is a NaN.\r\n      \r\n   \r\n   Contents\r\n   \r\n      \r\n         NaNs in Arithmetic\r\n... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/07\/05\/when-is-a-numeric-result-not-a-number\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,8],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/44"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":2830,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/44\/revisions\/2830"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}