Quantcast
Channel: ColdFusion – ColdFusion
Viewing all articles
Browse latest Browse all 100

Integer Division operator

$
0
0

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

The post Integer Division operator appeared first on ColdFusion.


Viewing all articles
Browse latest Browse all 100

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>