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:
The post Integer Division operator appeared first on ColdFusion.