Integer Division operator

July 30, 2019
I try to bend the internet to my will.
Wizard 31 posts
Followers: 19 people
3

Integer Division operator

I try to bend the internet to my will.
Wizard 31 posts
Followers: 19 people
July 30, 2019

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:

https://cffiddle.org/app/file?filepath=1a9ca0d0-b592-4f1a-9c36-29c0bd35b6ef/1bf59ce0-2f36-4fca-8324-30be8e286f6a/ff78c5cb-7cc9-4b07-a245-ce0c57de3bf1.cfm

Comments (3)
2019-08-01 16:18:36
2019-08-01 16:18:36

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.

Like
(1)
>
aliaspooryorik
's comment
2019-08-02 02:24:01
2019-08-02 02:24:01
>
aliaspooryorik
's comment

Understood. We’re both just trying to help. 🙂

Like
2019-08-01 15:06:32
2019-08-01 15:06:32

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.)

Like
Add your comment