# Integer Division operator

Using the Integer Division operator

I was reading Matthew J. Clemente’s great blog post What is the Modulus Operator? A Short Guide with Practical Use Cases which shows this formula:

a - ( n * floor( a / n ))

It struck me that you could write the above using the Integer Division operator like so:

a - (n * ( a \ n ))

The outcome is exactly the same. The `\`

operator here is returning the integer value part of a division sum. For example:

6 \ 3 // result: 2 5 \ 2 // result: 2 5 \ 3 // result: 1 1 \ 2 // result: 0

Here’s some sample code to prove it:

writeDump([ 6 \ 3, // result: 2 5 \ 2, // result: 2 5 \ 3, // result: 1 1 \ 2 // result: 0 ]); function mod1(a, n) { return a - (n * floor( a / n ) ); } function mod2(a, n) { return a - (n * ( a \ n ) ); } a = 100; n = 7; writeDump({ "mod1": mod1(a,n), "mod2": mod2(a,n) }); a = 10; n = 5; writeDump({ "mod1": mod1(a,n), "mod2": mod2(a,n) });

You can run the above using this link:

Hi Charlie,

I wasn’t meaning to critique Matt’s code at all. I just looked at it and thought – I think that you could use an Integer Division operator – and then wondered if other people were aware of it. Great observation of the outcome with negative numbers. 🙂

Matt – if you are reading (or anyone else) – your post and code is great – it just inspired me to have a play around with another way of writing it. This post is not meant to be a “better” or “correct” way, just an example of using the integer division operator.

I would update the post to reflect the above, but then it has to go back through Adobe’s moderation before being re-published.

Interesting thoughts, John. But I do think there’s more to the matter than it may seem.

My first thought on seeing this was I wondered if you may have just wanted instead to add a comment on his post, asking “Hey, Matt. Did you really need to add the floor in that example?” 🙂 He may have offered a reason. And he may reply here.

That said, I know you have only good intentions so perhaps you just found this a technically interesting matter. Since you got me interested, I did a little digging

If you check out that wikipedia link he offers, it has more on the math behind modulo, including a discussion of how some language implementations of modulo use “truncated division” while others use “floored”.

The difference is said to do with handling of negative numbers. The former approach keeps the result the same sign as the dividend, while the latter keeps it the same sign as the divisor. And indeed, Matt hinted at this when he said that dealing with negative numbers would be outside the scope of the article.

But I went ahead and changed your example to use a negative number for each of the dividend and divisor (100/-7 and -100/7), and in each case the result has the same sign as that of the **divisor** (the 7 above), so it seems we would conclude that CF’s modulus operation is indeed “floored”.

More important, the result is also numerically different than your division example, so that while what you say is true for positive numbers (that the result is the same if you use floor or not to simulate it), it is not so for negative numbers.

Granted, for most it may not matter, but in case someone DID somehow use negative numbers for some clever use of mod operations (which clever uses were indeed the point of his post), then the “difference” would matter (pun intended!) 🙂

Hope that’s helpful. (And if I got anything wrong, my apologies. It’s morning as I write.)

You must be logged in to post a comment.