In JavaScript, why does zero divided by zero return NaN, but any other divided by zero return Infinity?
Asked Answered
H

5

64

It seems to me that the code

console.log(1 / 0)

should return NaN, but instead it returns Infinity. However this code:

console.log(0 / 0)

does return NaN. Can someone help me to understand the reasoning for this functionality? Not only does it seem to be inconsistent, it also seems to be wrong, in the case of x / 0 where x !== 0

Horde answered 16/9, 2013 at 22:23 Comment(3)
#3215620Stack
There's a decent tutorial hereWorkable
There are an infinite number of zeroes in any number, so x / 0 === Infinity seems logical to me.Cockleshell
T
43

Because that's how floating-point is defined (more generally than just Javascript). See for example:

Crudely speaking, you could think of 1/0 as the limit of 1/x as x tends to zero (from the right). And 0/0 has no reasonable interpretation at all, hence NaN.

Tamboura answered 16/9, 2013 at 22:26 Comment(0)
M
12

In addition to answers based on the mathematical concept of zero, there is a special consideration for floating point numbers. Every underflow result, every non-zero number whose absolute magnitude is too small to represent as a non-zero number, is represented as zero.

0/0 may really be 1e-500/1e-600, or 1e-600/1e-500, or many other ratios of very small values.

The actual ratio could be anything, so there is no meaningful numerical answer, and the result should be a NaN.

Now consider 1/0. It does not matter whether the 0 represents 1e-500 or 1e-600. Regardless, the division would overflow and the correct result is the value used to represent overflows, Infinity.

Mink answered 17/9, 2013 at 2:25 Comment(3)
Language is important here. Small numbers are not represented as zero, and zero does not represent small numbers. This phrasing leads people to think incorrectly about floating-point approximations. Floating-point division may return zero for operations whose mathematical result is tiny, but that is because floating-point division is defined to return the representable value nearest the exact value, not because it is defined to approximately represent the quotient. Each floating-point value represents exactly one number, not an interval.Musset
@EricPostpischil Yes and no. Yes, IEEE 754 floating point arithmetic is well defined, with a definite result for each calculation, and a single value for each finite number. No, because that system, in isolation, would be useless. It gets its practical usefulness from its ability to produce, in many cases, good approximations to the result of a scientific or engineering calculation that is actually defined in terms of real number arithmetic. I'll think about how to reword my answer to distinguish properly between real number arithmetic and floating point arithmetic.Mink
@PatriciaShanahan: The way I like to think of it is that a floating-point value represents something that will be regarded as an exact number "going forward", but in many cases will represent the result of an operation whose exact numerical result was within 1/2ulp of the forward-going value. If a, b, and c are within an order of magnitude of each other, and one wants to display the sum accurate to four significant figures, knowing that a+b+c will be a value within 1ppm of the correct one [actual precision in fact being better] may be more helpful than a precise error term.Nayarit
R
5

I realize this is old, but I think it's important to note that in JS there is also a -0 which is different than 0 or +0 which makes this feature of JS much more logical than at first glance.

1 / 0 -> Infinity
1 / -0 -> -Infinity 

which logically makes sense since in calculus, the reason dividing by 0 is undefined is solely because the left limit goes to negative infinity and the right limit to positive infinity. Since the -0 and 0 are different objects in JS, it makes sense to apply the positive 0 to evaluate to positive Infinity and the negative 0 to evaluate to negative Infinity

This logic does not apply to 0/0, which is indeterminate. Unlike with 1/0, we can get two results taking limits by this method with 0/0

lim h->0(0/h) = 0
lim h->0(h/0) = Infinity

which of course is inconsistent, so it results in NaN

Risotto answered 14/4, 2017 at 7:3 Comment(3)
Interesting, but incomplete. Think lim h -> 0 (h / h) . Since JS has positive and negative zeroes, there should also be a positive and a negative limit. Thus only 0/-0 and -0/0 should result in NaN. The others should be positive or negative infinity. Of course, the mathematician in me is still crying like a baby.Wyndham
@SMBiggs, why lim h -> 0 (h / h) and not lim h -> 0 (2h / h) (for instance)? There is no consistent result.Clein
I the comment section I did not have room to include the infinite variations that yield the same result. Instead I used the simplest form I could think of that creates a graph of the shape (and limits) in question.Wyndham
K
1

In mathematics, division can be thought of as the inverse operation of multiplication. When you divide a number a by another number b, you are essentially trying to find out how many times b can fit into a. However, when you try to divide by zero, you are asking how many times zero can fit into the number a. Since zero has no value, it cannot be used to measure how many times it can fit into any number. This situation creates an undefined or indeterminate state in mathematics.

However, in the context of JavaScript and the IEEE 754 floating-point standard, the division by zero is handled differently. When you divide a positive number (e.g., 1) by zero, the result is Infinity. This is because, according to the IEEE 754 standard, the number system is extended with positive and negative infinities, which allows for the calculation of limits.

Think of it this way: as the divisor approaches zero, the result of the division grows larger and larger without bounds. In other words, the result approaches infinity. This is why, in JavaScript (and other programming languages that follow the IEEE 754 standard), the division of a positive number by zero results in Infinity

Kimberly answered 11/6, 2023 at 19:43 Comment(5)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewAdames
did you understand my answer?Kimberly
yes, I have thoroughly read and understood it, the answer only describes why division by zero is Infinity, while not describing why 0/0 is NaN and why there is this difference?Adames
think of it this way, In mathematics, 0/0 has no reasonable interpretation and no meaningful numerical answer. This behavior is defined by the IEEE 754 floating-point standard and is consistently implemented across programming languages (including JavaScript) and platforms.Kimberly
You should include this in answer too.Adames
A
0

In calculus, when you start evaluating limits, you learn that the limit of a vertical asymptote as x approaches that vertical asymptote increases without bound — or, in limit notation, evaluates to infinity. 0/0, meanwhile, is treated as an indeterminate form.

JavaScript, therefore, is basing this reasoning off of limit calculus, not algebra.

Aleshia answered 15/2, 2024 at 6:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.