Why does Javascript return Infinity instead of error when dividing by 0?

Asked

Viewed 1,691 times

6

When I divide any value by zero, Javascript, returns me Infinity.

My question is why Infinity? Infinity is set in Javascript, but I could not find a use for it in any case.

Can anyone tell me also, what would be the use of it?

  • 2

    But divide by 0 of the error in any language.

  • I know, so the question... why does javascript return me Infinity? Infinity is not a mistake. Got it?

  • Another thing, the Infinity typeof returns "number"... however Infinity - Infinity = Infinity;

  • 1

    In the JS, Infinity - Infinity results in NaN, not in Infinity.

  • Sorry, that’s right! I got confused.

  • I leave the answer here for someone to translate: https://stackoverflow.com/questions/18838301/in-javascript-why-does-zero-divided-by-zero-return-nan-but-any-other-divided-b

  • 1

    It is not any division by zero that returns Infinity. Zero divided by zero returns Nan.

Show 2 more comments

3 answers

8


When you divide something by zero you have to have some way to represent in Javascript. This is the main reason for its existence: the representation of a mathematical entity.

In another case, imagine that you need to know the smallest number of a given array that you have, it is useful to have a variable that is "inbatable" in order to change the smallest value:

var arr = [345435, 1, 744, 78899, 3e500];
let menor = Infinity;
for (let nr of arr) {
  if (nr < menor) menor = nr;
}

console.log(menor);

  • 1

    Thank you @Sergio, now I saw a good example of use for it. As soon as release milestone as correct the answer

  • 2

    good use, I usually assign the first value of the collection when I need to find the largest or least of a set and then continue comparing, but I liked the Infinity =]

5

I would like to add some information that is not included in the @Sergio replies (which gave a great example of using the Infinity), and @Leocaracciolo (who explained how division by zero tends to infinity - can be infinity negative too, if the dividend is negative).

What I wanted to clarify is that this logic is not a direct decision of the language project, but an indirect one. All numbers in JS are represented as floating points, and the floating point arithmetic specification (IEEE 754) dictates that the division by zero should be treated in this way. The use of exceptions and other types of "traps" or "hooks" (rags) in these cases is optional, the default is to return ±Infinity.

The FAQ of the specification justifies this as follows (free translation):

The 754 model encourages the creation of robust programs. It is aimed not only at numerical analysis, but also at users of spreadsheets, databases and even coffee makers. The rules of propagation of Nans and infinities allow irrelevant exceptions to disappear (...). When exceptional situations require treatment, they can be examined immediately via rags, or at a more convenient time via status flags.

2

To answer your question "My question is why Infinity?"

I don’t know exactly if in javascript they used this reasoning to reach the value Infinity division of any value by zero.

1/0,1 = 10
1/0,01 = 100
1/0,001 = 1000
1/0,0001 = 10000
1/0,00001 = 100000
...................
...................

The divisor, the closer to zero, the greater the result of the division, that is, a very large number, gigantic, inexplicably huge, finally Infinity Como ao uso vide resposta do amigo e Rei do Javascript Sergio

Browser other questions tagged

You are not signed in. Login or sign up in order to post.