The Integer Division Operator In ColdFusion

Posted October 11, 2010 at 9:43 AM by Ben Nadel

Tags: ColdFusion

Last week, I was talking to my esteemed colleague, Ryan Jeffords, about a mathematical expression that used the "\" operator. I had thought that this operator was simply a division operator that had been mistyped. However, it turns out that the backwards-division operator is actually an operator unto itself: integer division. I can't believe I didn't know about one of the core ColdFusion operators. And, to make matters more depressing, it's been available since at least ColdFusion 7 (trying to find livedocs pre-CF7 is not fun).

The integer division operator (\) works by removing the decimal portion from the resultant division value. It's basically like calling the fix() function on the outcome. To demonstrate this integer division operator, take a look at the following ColdFusion code:

  • <!--- Standard division operator. --->
  • 9.2 / 4 = #(9.2 / 4)#<br />
  • -9.2 / 4 = #(-9.2 / 4)#<br />
  •  
  • <br />
  •  
  • <!--- Integer division operator. --->
  • 9.2 \ 4 = #(9.2 \ 4)#<br />
  • -9.2 \ 4 = #(-9.2 \ 4)#<br />
  •  
  • <br />
  •  
  • <!--- Standard division with the Fix function. --->
  • fix( 9.2 / 4 ) = #fix( 9.2 / 4 )#<br />
  • fix( -9.2 / 4 ) = #fix( -9.2 / 4 )#<br />

Here, I am using the standard division operator, the integer division operator, and then a combination of standard division and fix() function. When we run the above code, we get the following output:

9.2 / 4 = 2.3
-9.2 / 4 = -2.3

9.2 \ 4 = 2
-9.2 \ 4 = -2

fix( 9.2 / 4 ) = 2
fix( -9.2 / 4 ) = -2

As you can see, the integer division operator takes the results of standard division and then removes the decimal place. This produces the same result as fix()'ing the standard division result.

As a practical use case for such division, take a look at the following example that combines ColdFusion's modulus operator with the integer division operator:

  • <!--- Set a number of minutes that we will break down. --->
  • <cfset totalMinutes = 97 />
  •  
  • <!---
  • Break total minutes down into Hours and Minutes using the
  • interger division and the modulus operator respectively.
  • --->
  • #(totalMinutes \ 60)# hours, #(totalMinutes % 60)# minutes

Here, we are breaking the total number of minutes into hours and minutes. Integer-dividing the total minutes by 60 gives us the number of whole hours. And, taking the modulus of 60 gives us the remaining minutes. When we run the above code, we get the following output:

1 hours, 37 minutes

You may notice that I am using % instead of MOD for the modulus operator. When I was looking this integer division operator up in the documentation, I was happy to be reminded that as of ColdFusion 8, the modulus operator can be either MOD or the more programmer-friendly % sign.

I've been a huge fan of the fix() function for the way it cleaves the decimal place from a given number. However, I am sure that I'll now be able to simplify at least some of my code using ColdFusion's integer division operator. Moments like this are always a reminder that it pays to just read the manual.




Reader Comments

Oct 11, 2010 at 10:06 AM // reply »
92 Comments

Very nice! Sad that it took this long to get that in, but way cool to know about it. Thanks for bringing it to my attention Ben!


Oct 11, 2010 at 10:12 AM // reply »
11,238 Comments

@Andy,

Heck yeah - I'm pretty exciting about this too.


Oct 11, 2010 at 10:23 AM // reply »
1 Comments

This is actually a very sexy way to handle something like this.


Oct 11, 2010 at 10:40 AM // reply »
66 Comments

good to know ... and if anyone ever doubts your 'geekness' simply point them to this post! only a true geek would get uber-excited over something like this. hehe.


Oct 11, 2010 at 10:52 AM // reply »
11,238 Comments

@Gabriel,

Thanks - agreed.

@Steve,

Ha ha, gotta keep my street cred going ;)


Oct 11, 2010 at 11:05 AM // reply »
16 Comments

I found this by accident one time by using the wrong slash. Spent hours debugging because I want the decimal places LOL.


Oct 11, 2010 at 1:15 PM // reply »
14 Comments

Another very good piece of information Ben.. Thanks a Lot..


Oct 11, 2010 at 2:34 PM // reply »
70 Comments

@Ben,

Yes, integer division rounds down any remainder. Which brings up an interesting tangential topic:

Have you ever heard the term "covered quotient"? It refers to an integer division that rounds up any remainder. For example, suppose your parking lot charges $3 an hour or any fraction thereof. If you stay there 61 minutes, you're charged for 2 hours.

If you have an integer division operator, the way to get a covered quotient is to add (divisor - 1) to the dividend, and then divide. So in the case of the parking lot example, the cfscript calculation is as follows:

  • Hours = (Minutes + 59) \ 60;

Another interesting tangential topic is the difference between true modulo arithmetic and that performed by computers. Only math majors know the difference, and we long ago gave it up as a lost cause. The difference is apparent when the quotient is negative. In true modulo arithmetic, -3 mod 10 is 7. Computers usually say that it's -3.

Anytime you need more trivia, let me know! :-)


Oct 11, 2010 at 2:39 PM // reply »
7 Comments

Excellent post. Who would ever guess that such an operator existed. This is something I can use. Thanks for the real life example.


Oct 11, 2010 at 5:08 PM // reply »
2 Comments

I just want to throw out there that I needed to do integer division the other day and I tried the '\' on a whim. I was really glad to see that it worked, and glad to see the post about it!


Oct 11, 2010 at 8:18 PM // reply »
11,238 Comments

@Tim,

Ha ha, I could definitely see this being a source of head-bashing if you had no idea that the operator exists. I could imagine myself just being like, "WTF!! Why aren't you working!!!!!".

@Naveen,

Thanks my man.

@Steve,

I have to believe that this "covered quotient" concept was never heard of until cell phone companies and parking garages needed a way to charge people more money :)

In all seriousness, I have never heard of it. Is this something that some languages have an operator for (that you know of)?

As far as the modulus operator, I had no idea that there was even this divergence. In fact, when you get into negative numbers, my understanding of how mod should work definitely becomes a bit fuzzy.

@Don, @Aaron,

Definitely a cool operator.


Oct 11, 2010 at 9:06 PM // reply »
270 Comments

@Ben,

I was a systems programmer for quite a while, meaning that for, like, 5 years or more, I coded in nothing but assembly language. One of the things you have to do at the hardware level is allocate blocks of memory or blocks of disk space, both of which occur in multiples of a fixed block size. So if you have something that you have to store somewhere, you have to allocate a covered quotient of the minimum block size.

For example, suppose you have to allocate memory in chunks of 1024 bytes. You have to read a file into memory. Before you begin, you know from the directory entry that the file contains 12345 bytes. How many 1K buffers do you need to allocate to read the file into memory?

(12345 + 1023) \ 1024 = 13 core blocks of 1K each.

The trick of adding (divisor - 1) before doing the integer division comes from the observation that computers do integer arithmetic faster than they do anything else. It's faster to add a constant and divide by a constant (2 instructions) than to divide by a constant and then do a comparison and branch instruction ("if") to determine whether to add 1 to the result (3 or 4 instructions). And yes, assembly language coders really do optimize right down to the number of hardware instructions.

Well anyway, now you know why it's called the "covered quotient". It's the quotient that's sufficient to cover the amount of data you need to hold.


Oct 11, 2010 at 9:12 PM // reply »
11,238 Comments

@Steve,

5 years of assembly language?!? And to think I had trouble with one semester of it :) I have to agree, though, assembly programmers definitely do spend a ton of time optimizing the code. I remember one of the most frustrating exercises that we had to do on a regular basis was look at a block of commands and figure out how to arrange them so that we didn't have to use "noop" commands.

That stuff is only a vague memory at this point; but, I remember that some commands took two clock cycles; so, there was opportunity to sneak an unrelated command right after it (to fill up the clock cycle as a more efficient noop).... or something to that effect.

As painful as it was, the assembly teacher was probably one of the happiest teachers that I had in all of my schooling.


Oct 11, 2010 at 11:26 PM // reply »
1 Comments

This is also a handy shortcut for truncating a real to an int.

adjustedPriceInCents = (priceInCents * discount) \ 1


Oct 12, 2010 at 1:52 AM // reply »
70 Comments

I'll bet you're keeping all of those rounded-down fractional pennies, aren't-cha. :-)


Oct 12, 2010 at 12:56 PM // reply »
45 Comments

@Ben

It's been there since at least version 2.0 (the first version I used). Like some of the other commenters, I discovered it accidentally while working on my first CF app :)


Oct 12, 2010 at 2:46 PM // reply »
25 Comments

Yeah, assembler and FORTRAN (and a bunch of other languages) have integer division. It is one of several gotchas on old FORTRAN tests. They slipped in integer division and those who didn't pay attention always got weird results that took a lot of hair pulling time to figure out. It is very powerful when you need it.


Oct 13, 2010 at 9:02 AM // reply »
70 Comments

@All,

Roland's right. Integer division has been in CF for a long time.

The biggest thing I got out of taking the CF certified developer's test was studying for it. That's where you pick up on things you don't commonly use and may have slipped under your radar.

On the subject of operators, there are also a bunch of bit operators that most people ignore or are unaware of, that may be useful in certain circumstances. In particular, you can use each bit of a 32-bit integer to represent a day of the month, for example. I've used that capability in scheduling apps. Or you can use it to keep a bunch of boolean options in only one database column and not have to keep making requests of your database group for more columns all the time. In a sense, days of the month, represented as bits, are an example of many booleans. It's a space versus time trade-off.

Just tossing that out there as an idea.


Oct 14, 2010 at 10:45 AM // reply »
11,238 Comments

@Jon, @Steve,

Ha ha, this must be how the scam in Office Space worked - integer division :)

@Roland,

Dangy! for that long? I must have seen it before then, but probably didn't have enough of an understanding of programming at the time to see why something like this would be useful. I know I'll never forget it now.

@Roger,

I always find those kinds of tests sneaky. The CF7 certification test had lots of questions like - When you run this code, what will happen? It's odd because it's such a different mindset than writing code. But speaking to @Steve's point, having to prepare for tests like that definitely forces you to learn about aspects of the language that you might have been aware of.

@Steve,

Bits as days - very interesting! Bit-wise operations are one of those things that I feel I need to "dust off" every time I try to use them. For some reason, that kind of logic won't cement in my head over the long term.



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools