javascript - Infinite from zero division - Stack Overflow

In JavaScript, if you divide by 0 you get Infinitytypeof Infinity; numberisNaN(Infinity); falseThi

In JavaScript, if you divide by 0 you get Infinity

typeof Infinity; //number
isNaN(Infinity); //false

This insinuates that Infinity is a number (of course, no argument there).

What I learned that anything divided by zero is in an indeterminate form and has no value, is not a number.

That definition however is for arithmetic, and I know that in programming it can either yield Infinity, Not a Number, or just throw an exception.

So why throw Infinity? Does anybody have an explanation on that?

In JavaScript, if you divide by 0 you get Infinity

typeof Infinity; //number
isNaN(Infinity); //false

This insinuates that Infinity is a number (of course, no argument there).

What I learned that anything divided by zero is in an indeterminate form and has no value, is not a number.

That definition however is for arithmetic, and I know that in programming it can either yield Infinity, Not a Number, or just throw an exception.

So why throw Infinity? Does anybody have an explanation on that?

Share Improve this question asked Feb 19, 2014 at 22:04 Sterling ArcherSterling Archer 22.4k19 gold badges85 silver badges121 bronze badges 3
  • 1 I can understand a close vote, but at least close for the right reason.. this isn't "general puting or software" it's a specific question about JavaScript ;) Nor is it primarily opinion based... LOL infinity isn't an opinion xD – Sterling Archer Commented Feb 19, 2014 at 22:15
  • Infinity/Infinity = 0/0 = NaN, Infinity/x = Infinity and x/Infinity = 0 seems quite reasonable. – Vikram Commented Feb 19, 2014 at 22:19
  • 2 This is a perfectly valid question according to the FAQ - it has a proper and determinate answer (as you can see below) within the scope of SO (it's about a programming language!) – SomeKittens Commented Feb 19, 2014 at 22:30
Add a ment  | 

7 Answers 7

Reset to default 5

First off, resulting in Infinity is not due to some crazy math behind the scenes. The spec states that:

Division of a non-zero finite value by a zero results in a signed infinity. The sign is determined by the rule already stated above.

The logic of the spec authors goes along these lines:

2/1 = 2. Simple enough.

2/0.5 = 4. Halving the denominator doubles the result.

...and so on:

2/0.0000000000000005 = 4e+1. As the denominator trends toward zero, the result grows. Thus, the spec authors decided for division by zero to default to Infinity, as well as any other operation that results in a number too big for JavaScript to represent [0]. (instead of some quasi-numeric state or a divide by zero exception).

You can see this in action in the code of Google's V8 engine: https://github./v8/v8/blob/bd8c70f5fc9c57eeee478ed36f933d3139ee221a/src/hydrogen-instructions#L4063

[0] "If the magnitude is too large to represent, the operation overflows; the result is then an infinity of appropriate sign."

Javascript is a loosely typed language which means that it doesn't have to return the type you were expecting from a function.

Infinity isn't actually an integer

In a strongly typed language if your function was supposed to return an int this means the only thing you can do when you get a value that isn't an int is to throw an exception

In loosely typed language you have another option which is to return a new type that represents the result better (such as in this case infinity)

Infinity is very different than indetermination.

If you pute x/0+ you get +infinity and for x/o- you get -infinity (x>0 in that example).

Javascript uses it to note that you have exceeded the capacity of the underlaying floating point storage.

You can then handle it to direct your sw towards either exceptional cases or big number version of your putation.

Infinity is actually consistent in formulae. Without it, you have to break formulae into small pieces, and you end up with more plicated code.

Try this, and you get j as Infinity:

var i = Infinity;
var j = 2*i/5;
console.log("result = "+j);

This is because Javascript uses Floating point arithmetics and it's exception for handling division by zero.

Division by zero (an operation on finite operands gives an exact infinite result, e.g., 1/0 or log(0)) (returns ±infinity by default).

wikipedia source

When x tends towards 0 in the formula y=1/x, y tends towards infinity. So it would make sense if something that would end up as a really high number (following that logic) would be represented by infinity. Somewhere around 10^320, JavaScript returns Infinity instead of the actual result, so if the calculation would otherwise end up above that threshold, it just returns infinity instead.

As determined by the ECMAScript language specification:

The sign of the result is positive if both operands have the same sign, negative if the operands have different signs.

Division of an infinity by a zero results in an infinity. The sign is determined by the rule already stated above.

Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule already stated above.

As the denominator of an arithmetic fraction tends towards 0 (for a finite non-zero numerator) the result tends towards +Infinity or -Infinity depending on the signs of the operands. This can be seen by:

  • 1/0.1 = 10
  • 1/0.01 = 100
  • 1/0.001 = 1000
  • 1/0.0000000001 = 10000000000
  • 1/1e-308 = 1e308

Taking this further then when you perform a division by zero then the JavaScript engine gives the result (as determined by the spec quoted above):

  • 1/0 = Number.POSITIVE_INFINITY
  • -1/0 = Number.NEGATIVE_INFINITY
  • -1/-0 = Number.POSITIVE_INFINITY
  • 1/-0 = Number.NEGATIVE_INFINITY

It is the same if you divide by a sufficiently large value:

  • 1/1e309 = Number.POSITIVE_INFINITY

In mathematics, we learned that any number divided by zero is undefined. So why does JavaScript not follow this rule?

Let z = x / y where x and y are both positive.

As y trends toward 0, z trends toward infinity.

1/1 = 1
1/0.1 = 10
1/0.01 = 100
...
1/0.0000000000001 = 10000000000000

JavaScript follows the IEEE 754 standard for floating-point numbers. Thus, it can only store numbers to a finite precision. So, once the number gets too close to 0, it bees 0.

JavaScript cannot hold numbers to that precision, so it has to convert it to the closest value that it can handle. So, any number close to 0 will bee 0.

Here is an example of this:

console.log(10**(-324)) // A really small number

As you can see, 0 doesn't just mean 0, it means 0 or any number really close to 0. That means it may not actually be 0, and thus dividing by it may not actually be undefined. It would break a lot of programs if dividing by any number close to zero resulted in undefined, so an easier behavior is to just follow the trend of the numbers, and return infinity.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744170341a4561505.html

相关推荐

  • javascript - Infinite from zero division - Stack Overflow

    In JavaScript, if you divide by 0 you get Infinitytypeof Infinity; numberisNaN(Infinity); falseThi

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信