n Mod -m: Max vs. Matlab

Today I was working on a little acoustics simulation patcher in Cycling 74’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’t working properly. After getting rid of my own home-made bugs, it still wasn’t working…

Turns out that there seems to be a disagreement in the code community about how to do the modulo of a negative number.

The best indication of the problem I was facing is found on this page, 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.

Matlab’s results
Max’s results, including using the gen object.

The results are:

Language13 mod 3-13 mod 313 mod -3-13 mod -13
Matlab12-2-1
Max1-11-1

This means that Matlab behaves like Python, using the formula

mod(a, n) = a – n * floor(a / n)

whereas Max behaves like C and Java.

So, if you, like me, move back and forth between Matlab and Max, beware!

what i learned last week

1. hitler thought that berlin would become the world capital re-named germania. he planned a massive architectural project for this transformation that included a triumphal arch based on (but much bigger than) the one in paris, and a volkshalle (or “people’s hall”) that was a dome based on (but much, much bigger than) the pantheon in rome. hitler’s architect, albert speer, wasn’t convinced that the swampland that berlin was built on could withstand the weight of the triumphal arch, so he devised an experiment to find out. this consisted of a large block of concrete (called the schwerbelastungsk√∂rper) that was constructed on the proposed site of the arch to measure how much, and how quickly, it would sink. turned out that it sank too fast, so the conclusion was that the arch wouldn’t work – it was just too heavy. interestingly, the block is still sitting there, and is now a historic site.

2. there was a little ice age that consisted of three dips in temperature world-wide between the 16th and the 19th centuries.

3. global warming might result in global cooling. if we have enough global warning, then the polar ice caps melt. this causes an increase in fresh water in the north atlantic ocean, which could result in a re-routing of ocean currents, potentially resulting in a repeat of the little ice age (see above…).

4. people who are raised from birth in bilingual environments are better able to shift attention, organise, prioritise and avoid falling into habitual responses. see here.

5. maurice jarre died.