Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the result of divide by zero?

To be clear, I am not looking for NaN or infinity, or asking what the answer to x/0 should be. What I'm looking for is this:

Based on how division is performed in hardware (I do not know how it is done), if division were to be performed with a divisor of 0, and the processor just chugged along happily through the operation, what would come out of it?

I realize this is highly dependent on the dividend, so for a concrete answer I ask this: What would a computer spit out if it followed its standard division operation on 42 / 0?

Update:

I'll try to be a little clearer. I'm asking about the actual operations done with the numbers at the bit level to reach a solution. The result of the operation is just bits. NaN and errors/exceptions come into play when the divisor is discovered to be zero. If the division actually happened, what bits would come out?

like image 633
yoozer8 Avatar asked Nov 06 '11 23:11

yoozer8


5 Answers

It might just not halt. Integer division can be carried out in linear time through repeated subtraction: for 7/2, you can subtract 2 from 7 a total of 3 times, so that’s the quotient, and the remainder (modulus) is 1. If you were to supply a dividend of 0 to an algorithm like that, unless there were a mechanism in place to prevent it, the algorithm would not halt: you can subtract 0 from 42 an infinite number of times without ever getting anywhere.

From a type perspective, this should be intuitive. The result of an undefined computation or a non-halting one is ⊥ (“bottom”), the undefined value inhabiting every type. Division by zero is not defined on the integers, so it should rightfully produce ⊥ by raising an error or failing to terminate. The former is probably preferable. ;)

Other, more efficient (logarithmic time) division algorithms rely on series that converge to the quotient; for a dividend of 0, as far as I can tell, these will either fail to converge (i.e., fail to terminate) or produce 0. See Division on Wikipedia.

Floating-point division similarly needs a special case: to divide two floats, subtract their exponents and integer-divide their significands. Same underlying algorithm, same problem. That’s why there are representations in IEEE-754 for positive and negative infinity, as well as signed zero and NaN (for 0/0).

like image 55
Jon Purdy Avatar answered Nov 11 '22 00:11

Jon Purdy


For processors that have an internal "divide" instruction, such as the x86 with div, the CPU actually causes a software interrupt if one attempts to divide by zero. This software interrupt is usually caught by the language runtime and translated into an appropriate "divide by zero" exception.

like image 24
Greg Hewgill Avatar answered Nov 11 '22 01:11

Greg Hewgill


Hardware dividers typically use a pipelined long division structure.

Assuming we're talking about integer division for now (as opposed to floating-point); the first step in long division is to align the most-significant ones (before attempting to subtract the divisor from the dividend). Clearly, this is undefined in the case of 0, so who knows what the hardware would do. If we assume it does something sane, the next step is to perform log(n) subtractions (where n is the number of bit positions). For every subtraction that results in a positive result, a 1 is set in the output word. So the output from this step would be an all-1s word.

Floating-point division requires three steps:

  • Taking the difference of the exponents
  • Fixed-point division of the mantissas
  • Handling special cases

0 is represented by all-0s (both the mantissa and the exponent). However, there's always an implied leading 1 in the mantissa, so if we weren't treating this representation as a special case, it would just look and act like an extremely small power of 2.

like image 45
Oliver Charlesworth Avatar answered Nov 11 '22 00:11

Oliver Charlesworth


It depends on the implementation. IEE standard 754 floating point[1] defines signed infinity values, so in theory that should be the result of divide by zero. The hardware simply sets a flag if the demoninator is zero on a division operation. There is no magic to it.

Some erroneous (read x86) architectures throw a trap if they hit a divide by zero which is in theory, from a mathematical point of view, a cop out.

[1] http://en.wikipedia.org/wiki/IEEE_754-2008

like image 2
Deleted Avatar answered Nov 10 '22 23:11

Deleted


It would be an infinite loop. Typically, division is done through continuous subtraction, just like multiplication is done via continual addition.

So, zero is special cased since we all know what the answer is anyway.

like image 1
Will Hartung Avatar answered Nov 11 '22 00:11

Will Hartung