{"id":2629,"date":"2010-04-29T11:02:34","date_gmt":"2010-04-29T10:02:34","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=2629"},"modified":"2013-03-15T08:32:23","modified_gmt":"2013-03-15T07:32:23","slug":"when-cant-matlab-add-up","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=2629","title":{"rendered":"When can&#8217;t MATLAB add up?"},"content":{"rendered":"<p>I have got a nice, shiny 64bit version of <a href=\"http:\/\/www.mathworks.com\/\">MATLAB<\/a> running on my nice, shiny 64bit Linux machine and so, naturally, I wanted to be able to use 64 bit integers when the need arose.\u00a0 Sadly, MATLAB had other ideas.\u00a0 On MATLAB 2010a:<\/p>\n<pre>a=int64(10);\r\nb=int64(20);\r\na+b\r\n??? Undefined function or method 'plus' for input arguments of type 'int64'.<\/pre>\n<p>It doesn&#8217;t like any of the other operators either<\/p>\n<pre>&gt;&gt; a-b\r\n??? Undefined function or method 'minus' for input arguments of type 'int64'.\r\n\r\n&gt;&gt; a*b\r\n??? Undefined function or method 'mtimes' for input arguments of type 'int64'.\r\n\r\n&gt;&gt; a\/b\r\n??? Undefined function or method 'mrdivide' for input arguments of type 'int64'.<\/pre>\n<p>At first I thought that there was something wrong with my MATLAB installation but it turns out that this behaviour is expected and documented. At the time of writing, the <a href=\"http:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/ref\/int8.html\">MATLAB documentation<\/a> contains the lines<\/p>\n<pre>Note   Only the lower order integer data types support math operations.\r\nMath operations are not supported for int64 and uint64.<\/pre>\n<p>So, there you have it. When can&#8217;t MATLAB add up? When you ask it to add 64 bit integers!<\/p>\n<p><strong>Update:<\/strong> Just had an email from someone who points out that <a href=\"http:\/\/www.gnu.org\/software\/octave\/\">Octave<\/a> (Free MATLAB Clone) can handle 64bit integers just fine<\/p>\n<pre>octave:1&gt; a=int64(10);\r\noctave:2&gt; b=int64(20);\r\noctave:3&gt; a+b\r\nans = 30\r\noctave:4&gt;<\/pre>\n<p><strong>Update 2:<\/strong> Since I got <a href=\"http:\/\/tech.slashdot.org\/story\/10\/05\/02\/2038255\/MATLAB-Cant-Manipulate-64-Bit-Integers\">slashdotted<\/a>, people have been asking why I (or, more importantly, the user I was helping) needed 64bit integers. Well, we were using the <a href=\"http:\/\/www.nag.co.uk\/numeric\/MB\/start.asp\">NAG Toolbox for MATLAB<\/a> which is a MATLAB interface to the NAG Fortran library. Some of its routines require integer arguments and on a 64bit machine these must be 64bit integers. We just needed to do some basic arithmetic on them before passing them to the toolbox and discovered that we couldn&#8217;t. The work-around was simple &#8211; use int32s for the arithmetic and then convert to int64 at the end so no big deal really. I was simply surprised that arithmetic wasn&#8217;t supported for int64s directly &#8211; hence this post.<\/p>\n<p><strong>Update 3: <\/strong>In the comments, someone pointed out that there is a package on the File Exchange that adds <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24725\">basic arithmetic support for 64bit integers<\/a>.\u00a0 I&#8217;ve not tried it myself though so can&#8217;t comment on its quality.<\/p>\n<p><strong>Update 4: <\/strong>Someone has made me aware of an interesting discussion concerning this topic on MATLAB Central a few years back: <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/newsreader\/view_thread\/154955\">http:\/\/www.mathworks.com\/matlabcentral\/newsreader\/view_thread\/154955<\/a><\/p>\n<p><strong>Update 5 (14th September 2010): MATLAB 2010b now has support for 64 bit integers.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have got a nice, shiny 64bit version of MATLAB running on my nice, shiny 64bit Linux machine and so, naturally, I wanted to be able to use 64 bit integers when the need arose.\u00a0 Sadly, MATLAB had other ideas.\u00a0 On MATLAB 2010a: a=int64(10); b=int64(20); a+b ??? Undefined function or method &#8216;plus&#8217; for input arguments [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,11],"tags":[],"class_list":["post-2629","post","type-post","status-publish","format-standard","hentry","category-math-software","category-matlab"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-Gp","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2629"}],"version-history":[{"count":19,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2629\/revisions"}],"predecessor-version":[{"id":4881,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2629\/revisions\/4881"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}