portal entry

select a category, or use search below
(searches all categories and all time range)

Integer Division operator

| View in Portal
July 30, 2019 09:19:12 PM GMT
<p>Using the Integer Division operator</p>
<p>The post <a rel="nofollow" href="https://coldfusion.adobe.com/2019/07/integer-division-operator/">Integer Division operator</a> appeared first on <a rel="nofollow" href="https://coldfusion.adobe.com">ColdFusion</a>.</p>
Labels: Blog, ColdFusion, Modern CFML, 2018, blog, modern cfml


<p>Interesting thoughts, John. But I do think there’s more to the matter than it may seem.</p><p>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?” <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f642.png" alt="??" class="wp-smiley" style="height: 1em; max-height: 1em;" /> He may have offered a reason. And he may reply here.</p><p>That said, I know you have only good intentions so perhaps you just found this a technically interesting matter. <img style="margin-left: 3px; margin-right: 3px; vertical-align: middle; -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none;" src="https://cfimages.adobe.com/wp-content/plugins/adobeid/emoji/icons/1F642.png" width="18" height="18" />  Since you got me interested, I did a little digging</p><p>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”.</p><p>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.</p><p>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 <strong>divisor</strong> (the 7 above), so it seems we would conclude that CF’s modulus operation is indeed “floored”.</p><p>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.</p><p>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!) <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f642.png" alt="??" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p><p>Hope that’s helpful. (And if I got anything wrong, my apologies. It's morning as I write.)</p>
Comment by Charlie Arehart
2207 | August 01, 2019 03:06:32 PM GMT
<p>Hi Charlie,</p><p>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. <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f642.png" alt="??" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p><p>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.</p><p>I would update the post to reflect the above, but then it has to go back through Adobe's moderation before being re-published.</p>
Comment by aliaspooryorik
2209 | August 01, 2019 04:18:36 PM GMT
Understood. We're both just trying to help. :-)
Comment by Charlie Arehart
2212 | August 02, 2019 02:24:01 AM GMT