Division by Zero: Infinite, NaN or Zero Division Error?

Why is there no 1/0 == Inf in every language? Is this not the most mathematically correct answer?

All languages ​​that I know are capable of expressing both Infinite and NaN values, so why did they decide to throw an error or return NaN? Does it just make life harder for scientific application developers ?; -)

Update: Maybe we should close this question because I thought incorrectly that 1f/0f == Float.NaN in Java. But I was wrong: it returns Float.Infinity . That was my main confusion; the fact that some languages ​​throw errors is understandable if no language returns NaN.

+4
source share
9 answers

Besides the fact that 1/0 == inf is mathematically very doubtful, the simple reason why it does not work in most programming languages ​​is that 1/0 performs integer division almost everywhere (exceptions exist).

The result is an integer, and there is no way to encode "infinity" in integer form. There is a floating point number, so floating point division actually gives an infinite value in most languages.

The same is true for NaN: while the IEEE floating point standard defines a bit pattern that represents a NaN value, integers do not have that value; thus, such values ​​simply cannot be represented as an integer.

+17
source

As long as the 1 / n limit tends to infinity when n approaches zero, the reason that 1/0 1 / 0 <> Inf is that 1/0 is undefined (mathematical definition!).

+3
source

Is this not the most mathematically correct answer?

No, because in mathematics, division by zero is simply undefined, and infinity is usually not a value (or does not matter).

The reason why not all languages ​​/ libraries return NaN is because (a) zero division is often the result of a programmer error, since it should not occur at all in mathematically rigorous algorithms and (b) processors can process it by going into state exceptions, therefore, conversion to NaN will require processing of such states, which means that the division becomes even more expensive than it is already (compared, for example, with summation).

+3
source

Why trolling? But I will bite. This may vary depending on how you create your statements, but the most common way to define division is to simply functional inverse multiplication. That is, c = a / b is defined as c, which is a single number, such that c * b = a.

Now consider c = 1/0. Does c exist such that c * 0 = 1? Of course, not inside R or C. What if we introduce infinity? You may have a special case in which it says 0 * Infinity = 1. But then you break up a bunch of nice properties of the multiplication operator. That is, we would like 2 * (0 * Infinity) = 2 * 1 = 2. But we also want an associative property. So, (2 * 0) * Infinity = 0 * Infinity = 1.

In the general case, the field cannot be expanded so that in any case it has a multiplicative inverse 0 that preserves the desired properties.

However, I suppose you entered the classic 1/0 = Infinity for trolling only. The next question is why languages ​​do not recognize that 0.9 relay is not equal to 1.

+2
source

How about 0/0 or -1/0 ? What to do if you make a mistake? It is not recommended to signal zero division of the program with the result Inf .

+1
source

Floating point operations can detect several exceptional conditions and respond in several ways:

  • Setting a status flag that can be subsequently tested.
  • Create a trap right away.

The first mode of operation provides high performance, while the second mode allows you to immediately notify about potentially dangerous operations.

IEEE 754 defines some normal values ​​for the result of operations that throw exceptions (for example: a final non-zero number and a divisor 0 → correctly signed INFINITY; 0/0 → Quiet NaN).

The goal of IEEE 754 with traps is that there will be user mode handlers that could check the operation, operands (to exclude Invalid Operation and Divide by Zero) or the result (to overflow, flaws and inaccurate exceptions) and exception flags and return a new user result.

In fact, some languages ​​do not even support access to exception state flags, not to mention setting up hook traps.

Even C provides very primitive support for trap handlers; C99 does not even define a portable way to set the capture mode, requiring, for example, passing some value defined for implementation to fesetenv() . Implementations typically raise SIGFPE, and the user will typically need to access a processor dependent state to implement the IEEE-754 capture processor.

+1
source

Since while 1/0 can sometimes be approximated as INF or some other similar value, it is not formally defined as such. On the same front, 10 / 0 < 20 / 0 ? or 0 / 0 .

0
source

Subsection a | b a | b itself is not defined for b = 0 . Therefore, the implementation of this operation compares this case with a special meaning in order to express this concept.

0
source

In the area of ​​interaction with Java, I see this.

 Welcome to DrJava. Working directory is /Users/morrison/Desktop/PhotoPuzzle > int top = 1; > int bottom = 0; > top/bottom java.lang.ArithmeticException: / by zero > double topFloat = 1; > double bottomFloat = 0; > topFloat/bottomFloat Infinity > 

Floating-point numbers are inaccurate and can be very close to zero. Creating an integer zero is probably considered as a goof programmer. These are probably two different behaviors that you see.

0
source

All Articles