There is simple unsigned integer binary division algorithm:
Code: Select all
N := dividend D := divider Q := 0 // result R := 0 // remainder for i := n − 1 .. 0 do // n - bits number R := R << 1 R(0) := N(i) ; operator(i) = i-th bit if R >= D then R := R − D Q(i) := 1 end end
And it has interesting feature also: it can divide by zero. And this process has absolutely the same count of steps like with any other divider.
It produces results with all binary digits set to 1, that is 'closest to infinity'.
At the same time it produces remainder equal to dividend.
This is tricky thing because if we check result by formula of integer division check we get: (111...1111 * 0 + remainder = dividend) == true.
However such a remainder violates exact definition of math remainder, but algorithm do really tries to get as meaningful result as possible!
And yes, I know that real math doesn't allow division by zero under any circumstances (Numbephile youtube channel has good explanation why for example).
But my question is: could this answer be useful in computer applications?
One example: what if we calculate count of segments/lines of circle for CAD system with extreme and wrong zero size of segment: we'll get count of segments which cannot be allocated in all computer memory (size_t in C++ = MAX_UNSIGNED_INT), so it's not if ( zero ... ), but malloc function will fail and we'll get rid of one unncececery check/condition. Sounds self-consistent...