Maizere Pathak.Nepal
Maizere Pathak.Nepal

Reputation: 2413

Javascript Infinity

Division by 0 gives this special value:

 3/0 output:Infinity

You can’t play positive and negative infinity against each other:

Infinity - Infinity output:NaN (Why?)

It also turns out that “beyond infinity” is still infinity:

 Infinity + Infinity output:Infinity(this is accepted)


 5 * Infinity
Infinity(this is also accepted)

so why infinity-infinity evalutes to NaN?It should be infinity isn't it?Also i wanted to know why cant object be converted to primitive values?Sorry for posting two question at a time ,as this is the last question i can post.See here:

var obj = {
    valueOf: function () {
        console.log("valueOf");
        return {}; // not a primitive
    },
    toString: function () {
        console.log("toString");
        return {}; // not a primitive
    }
}

 Number(obj) //TypeError: Cannot convert object to primitive values

Upvotes: 5

Views: 1825

Answers (3)

Faust
Faust

Reputation: 15404

var A = 1/0
var B = 2 * A
var c = B - A

Note that even though B = 2 * A, still A = B (2 * infinity is still infinity, so they are both infinity), so what do you expect C to be? infinity or 0?

Infinity is not really a number, mathematically speaking. Though IsNaN(1/0) = false.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

That's how ∞ works in mathematics. Infinity itself is not a number, it is a concept. The general idea is that
∞ + x = ∞ ∀ x

∞ is, obviously, infinitely big. If you subtract an infinitely big thing from another infinitely big thing, you can't define what you have left. If the first infinity is bigger, you'll get a negative result, but if it's smaller then the result will be positive (basic rule of subtraction), but since both are infinitely big you have no way of knowing which is bigger (unless more information is given, such as the context leading to these infinities*). Therefore, as far as the computer is concerned, ∞ - ∞ is mathematically undefined, or Not a Number.

* Example: Let x = the sum of all positive integers, and y = the sum of each positive integer doubled. In this case, we can say that y > x, even though both are infinity.

Upvotes: 9

TNW
TNW

Reputation: 726

Because it's an indeterminate form, so it's not infinity. NaN reflects this the best way possible.

http://en.wikipedia.org/wiki/Indeterminate_form

Related question: https://math.stackexchange.com/questions/60766/what-is-the-result-of-infinity-minus-infinity

Upvotes: 0

Related Questions