{"id":6353,"date":"2021-01-19T21:30:40","date_gmt":"2021-01-19T19:30:40","guid":{"rendered":"http:\/\/www.tonmeister.ca\/wordpress\/?p=6353"},"modified":"2021-01-19T21:31:35","modified_gmt":"2021-01-19T19:31:35","slug":"n-mod-m-max-vs-matlab","status":"publish","type":"post","link":"https:\/\/www.tonmeister.ca\/wordpress\/2021\/01\/19\/n-mod-m-max-vs-matlab\/","title":{"rendered":"n Mod -m: Max vs. Matlab"},"content":{"rendered":"\n<p>Today I was working on a little acoustics simulation patcher in <a href=\"http:\/\/www.cycling74.com\">Cycling 74&#8217;s Max<\/a>, and part of the code required the use of a modulo function. No problem, right?<\/p>\n\n\n\n<p>Problem. I originally wrote the code in <a href=\"http:\/\/www.mathworks.com\">Matlab<\/a>, and I was porting it to Max; and the numbers just weren&#8217;t working properly. After getting rid of my own home-made bugs, it still wasn&#8217;t working&#8230;<\/p>\n\n\n\n<p>Turns out that there seems to be a disagreement in the code community about how to do the modulo of a negative number.<\/p>\n\n\n\n<p>The best indication of the problem I was facing is found on <a href=\"https:\/\/torstencurdt.com\/tech\/posts\/modulo-of-negative-numbers\/\">this page<\/a>, where you can see that different languages come up with different answers for -13 mod 3 and 13 mod -3. The problem is that neither Max nor Matlab are in the list. So: here are the results of those two, to add to the list.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/matlab_mod.png\" alt=\"\" class=\"wp-image-6354\" width=\"219\" height=\"436\" srcset=\"https:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/matlab_mod.png 219w, https:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/matlab_mod-151x300.png 151w\" sizes=\"auto, (max-width: 219px) 100vw, 219px\" \/><figcaption>Matlab&#8217;s results<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/max_mod-1024x470.png\" alt=\"\" class=\"wp-image-6355\" width=\"1024\" height=\"470\" srcset=\"https:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/max_mod-1024x470.png 1024w, https:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/max_mod-300x138.png 300w, https:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/max_mod-768x352.png 768w, https:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/max_mod.png 1184w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Max&#8217;s results, including using the gen object.<\/figcaption><\/figure><\/div>\n\n\n\n<p>The results are:<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table><tbody><tr><td><strong>Language<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>13 mod 3<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>-13 mod 3<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>13 mod -3<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>-13 mod -13<\/strong><\/td><\/tr><tr><td>Matlab<\/td><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td class=\"has-text-align-center\" data-align=\"center\">-2<\/td><td class=\"has-text-align-center\" data-align=\"center\">-1<\/td><\/tr><tr><td>Max<\/td><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">-1<\/td><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">-1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This means that Matlab behaves like Python, using the formula<\/p>\n\n\n\n<p class=\"has-text-align-center\">mod(a, n) = a &#8211; n * floor(a \/ n)<\/p>\n\n\n\n<p>whereas Max behaves like C and Java.<\/p>\n\n\n\n<p>So, if you, like me, move back and forth between Matlab and Max, beware!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I was working on a little acoustics simulation patcher in Cycling 74&#8217;s Max, and part of the code required the use of a modulo function. No problem, right? Problem. I originally wrote the code in Matlab, and I was porting it to Max; and the numbers just weren&#8217;t working properly. After getting rid of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[35,42,26],"tags":[],"class_list":["post-6353","post","type-post","status-publish","format-standard","hentry","category-computers","category-math","category-what-i-learned"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p48hIM-1Et","_links":{"self":[{"href":"https:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/posts\/6353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=6353"}],"version-history":[{"count":3,"href":"https:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/posts\/6353\/revisions"}],"predecessor-version":[{"id":6358,"href":"https:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/posts\/6353\/revisions\/6358"}],"wp:attachment":[{"href":"https:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=6353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=6353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=6353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}