Some identities involving integer division
I have worked out quite some identities involving integer division while writing other articles, and I decided to write them down for reference.
The variables m, n, d are assumed to be integers (with d=0) throughout this article, whereas x is a real variable.
Rounding up and down
When we divide unsigned integers n
and d
, the result gets rounded down. That is, n / d
evaluates to ⌊dn⌋. If we want to round up the quotient, we can do it as (n + d - 1) / d
. Here, we are using the following identity:
⌊dn+d−1⌋=⌈dn⌉
The relation between the floor and the ceil function is that
⌊−x⌋=−⌈x⌉
Rounding to multiples of d
To round down an integer n to the nearest multiple of d, we can use the expression d⌊dn⌋. Alternatively, we can use modular arithmetic to write this expression as
n−modd(n)
Likewise, we can round up an integer n to the nearest multiple of d with the expression d⌈dn⌉. With modular arithmetic we can write this as
n+modd(−n)
Getting rid of floor/ceil
Sometimes, it is useful to get rid of the floor or ceiling function from an expression. From the results in the last section we can derive the following identities which allow you to do this.
For the floor function, we have:
⌊dn⌋=dn−modd(n)
And for the ceil function:
⌈dn⌉=dn+modd(−n)
Sum identity
I write qn=⌊dn⌋, pn=modd(n), so that we have n=dqn+pn.
By working out ⌊dm+n⌋=⌊d(dqm+pm)+(dqn+pn)⌋ we can prove that
qn+m=qn+qm+⌊dpn+pm⌋
Likewise, we can write sn=⌈dn⌉, rn=modd(−n), so that n=dsn−r−n.
Then, we can work out ⌈dm+n⌉=⌈d(dsm−rm)+(dsn−rn)⌉ and get the identity
sn+m=sn+sm−⌊drn+rm⌋
Product identity
Again, I write qn=⌊dn⌋, pn=modd(n), so that we have n=dqn+pn.
By working out and simplifying the expression ⌊dmn⌋=⌊d(dqm+pm)(dqn+pn)⌋ we can prove that
qnm=dqmqn+pmqn+qmpn+⌊dpnpm⌋
Again writing sn=⌈dn⌉, rn=modd(−n), we have n=dsn−r−n.
Now we can work out ⌈dmn⌉=⌈d(dsm+rm)(dsn+rn)⌉ and get the identity
snm=dsmsn−(rmsn+smrn)+⌈drnrm⌉