{"id":292,"date":"2009-01-12T15:27:13","date_gmt":"2009-01-12T15:27:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2009\/01\/12\/how-to-be-top-mathematician\/"},"modified":"2009-01-12T15:27:13","modified_gmt":"2009-01-12T15:27:13","slug":"how-to-be-top-mathematician","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2009\/01\/12\/how-to-be-top-mathematician\/","title":{"rendered":"How to be Top Mathematician"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p>I consider myself to be a pretty good cook, but I am no Top Chef.  On Top Chef the contestants have to cook their ingredients perfectly, which requires single-degree control of the temperature on their stoves and cook times exact down to seconds.  For me, I&#8217;m competing only for survival and not money so I&#8217;ll take delicious and good enough over perfect; if it comes out a little rubbery, so what?<\/p>\n<p>Computers, on the other hand, don&#8217;t have a native concept of &#8220;good enough.&#8221;  Computers demand perfection and precision. Fortunately programming languages (such as MATLAB) provide easy ways for us to easily achieve a certain level of precision and repeatably.<\/p>\n<p>   <\/introduction><\/p>\n<p>It&#8217;s not always easy to determine the precision you&#8217;re using. For instance to those unfamiliar with IEEE floating point representation, the following might have surprising results:\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">0.1 + 0.1 + 0.1 - 0.3<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n  5.5511e-017\r\n\r\n<\/pre>\n<p>Why do we get this result, when we expect 0? At first glance the two numbers look exactly the same:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">0.1 + 0.1 + 0.1\r\n0.3<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n    0.3000\r\n\r\n\r\nans =\r\n\r\n    0.3000\r\n\r\n<\/pre>\n<p>Fortunately the Command Window has a special function to help us understand what is going on. The <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/format.html\"><tt>format<\/tt><\/a> function controls how numbers are represented in Command Window output.<\/p>\n<p>At first we might try long format.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format <span style=\"color: #A020F0\">long<\/span>\r\n0.1 + 0.1 + 0.1\r\n0.3<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n   0.300000000000000\r\n\r\n\r\nans =\r\n\r\n   0.300000000000000\r\n\r\n<\/pre>\n<p>The long format gives us lots of extra digits, but that still does not show us the difference between these two numbers. To see the difference, we need to look at the actual 64-bit floating point representation.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format <span style=\"color: #A020F0\">hex<\/span>\r\n0.1 + 0.1 + 0.1\r\n0.3<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n   3fd3333333333334\r\n\r\n\r\nans =\r\n\r\n   3fd3333333333333\r\n\r\n<\/pre>\n<p>You can see that these two numbers are close but not exactly the same. The reason for this is to represent numbers on the computer, we have to sample an infinite number of numbers between 10^-308 to 10^+308, leading to round-off. This round-off error is measured by the difference between two consecutive numbers, using the magical function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/eps.html\"><tt>eps<\/tt><\/a>. There is a great explanation of this in <a href=\"https:\/\/www.mathworks.com\/support\/tech-notes\/1100\/1108.html\">technical note 1108<\/a>.  This subtle difference between a typed and calculated number can be quite important in program flow, such as with <tt>if<\/tt>, <tt>for<\/tt>, and <tt>while<\/tt> statements. Number representation is also quite important for file I\/O.<\/p>\n<p>The most basic text file operations are done using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/fprintf.html\"><tt>fprintf<\/tt><\/a>. We can see that using the default floating point representation we do not get back the same exact numbers that we started with. If these numbers were coefficients, we could destabilize a system before we even begin analysis. <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format <span style=\"color: #A020F0\">short<\/span>\r\nA = rand(5);\r\nfid = fopen(<span style=\"color: #A020F0\">'data.dat'<\/span>,<span style=\"color: #A020F0\">'w'<\/span>);\r\nfprintf(fid,<span style=\"color: #A020F0\">'%f '<\/span>,A);\r\nfclose(fid);\r\nfid = fopen(<span style=\"color: #A020F0\">'data.dat'<\/span>,<span style=\"color: #A020F0\">'r'<\/span>);\r\nA2 = fscanf(fid,<span style=\"color: #A020F0\">'%f '<\/span>,[5 5]);\r\nfclose(fid);\r\nmax(A-A2)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n  1.0e-006 *\r\n\r\n    0.4303    0.4098    0.2859    0.2926    0.4377\r\n\r\n<\/pre>\n<p>Once again, the day is saved with hex notation. There&#8217;s a little trickiness here due to my choice of reading in characters through <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/fscanf.html\"><tt>fscanf<\/tt><\/a>. If you run this bit at home, compare the two text files. The first one is nice and human readable, but the second one has the best precision for a computer.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fid = fopen(<span style=\"color: #A020F0\">'data2.dat'<\/span>,<span style=\"color: #A020F0\">'w'<\/span>);\r\nfprintf(fid,<span style=\"color: #A020F0\">'%bx '<\/span>,A);\r\nfclose(fid);\r\nfid = fopen(<span style=\"color: #A020F0\">'data2.dat'<\/span>,<span style=\"color: #A020F0\">'r'<\/span>);\r\nA3 = fscanf(fid,<span style=\"color: #A020F0\">'%c '<\/span>,[16 inf]);\r\nfclose(fid);\r\nA3 = hex2num(A3');\r\nA3 = reshape(A3,5,5);\r\nmax(A-A3)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     0     0     0     0     0\r\n\r\n<\/pre>\n<p>This issue can happen with other text file formats as well, such as CSV. If you&#8217;re on windows, Microsoft Excel provides a good balance because the files are easily readable, but the data is stored in binary.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">csvwrite(<span style=\"color: #A020F0\">'data.csv'<\/span>,A);\r\nA4 = csvread(<span style=\"color: #A020F0\">'data.csv'<\/span>);\r\nmax(A-A4)\r\n\r\nxlswrite(<span style=\"color: #A020F0\">'data.xls'<\/span>,A)\r\nA5 = xlsread(<span style=\"color: #A020F0\">'data.xls'<\/span>);\r\nmax(A-A5)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n  1.0e-005 *\r\n\r\n    0.4092    0.3846    0.2146    0.4540    0.4040\r\n\r\n\r\nans =\r\n\r\n     0     0     0     0     0\r\n\r\n<\/pre>\n<p>Precision considerations are not just for the command line functions. They should be kept in mind when importing\/exporting from any of our GUIs and from Simulink models. For example, take one of my favorite toolboxes, the  <a href=\"https:\/\/www.mathworks.com\/products\/filterdesign\/\">Filter Design Toolbox<\/a>. Back when I worked in tech support, I helped a customer through this very issue. After exporting his coefficients and importing them into a different tool, he saw a wildly different filter. Below I&#8217;ve captured the export window, where there are several different options for output format.  Most of our toolbox GUIs have similar options, and in cases where there is not, there is usually a command line function to set the desired precision.\n   <\/p>\n<div align=\"center\"><img decoding=\"async\"  border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/mike_katz_top_math\/fdaexport.jpg\" alt=\"FDA Tool export options\"><\/div>\n<p>If you are only going to and from MATLAB, for best results when saving data, use a MAT-file with the save and load commands. For this you can use the export button in the Workspace Browser. <\/p>\n<div align=\"center\"><img decoding=\"async\"  border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/mike_katz_top_math\/wkspce_save.jpg\" alt=\"Workspace Browser save button\"><\/div>\n<p>In keeping with the previous series, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/save.html\"><tt>save<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/load.html\"><tt>load<\/tt><\/a> also have a nice ASCII option:\n<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">save <span style=\"color: #A020F0\">data.mat<\/span> <span style=\"color: #A020F0\">-ascii<\/span> <span style=\"color: #A020F0\">-double<\/span> <span style=\"color: #A020F0\">A<\/span>\r\nA6 = load(<span style=\"color: #A020F0\">'data.mat'<\/span>,<span style=\"color: #A020F0\">'-ascii'<\/span>);\r\nmax(A-A6)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     0     0     0     0     0\r\n\r\n<\/pre>\n<p>A final fun floating point subtlety; there are two different but equivalent zeroes.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format <span style=\"color: #A020F0\">hex<\/span>\r\nzero = 0\r\nminuszero = -0<\/pre>\n<pre style=\"font-style:oblique\">\r\nzero =\r\n\r\n   0000000000000000\r\n\r\n\r\nminuszero =\r\n\r\n   8000000000000000\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I consider myself to be a pretty good cook, but I am no Top Chef.  On Top Chef the contestants have to cook their ingredients perfectly, which requires single-degree control of the temperature&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2009\/01\/12\/how-to-be-top-mathematician\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,12],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/292"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}