Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 776
Maizere Pathak.Nepal Avatar asked Apr 13 '13 16:04

Maizere Pathak.Nepal


People also ask

What is infinity in JavaScript?

Infinity is a property of the global object. In other words, it is a variable in global scope. The initial value of Infinity is Number. POSITIVE_INFINITY . The value Infinity (positive infinity) is greater than any other number.

How do you handle infinity in JavaScript?

isFinite is a function property of the global object. You can use this function to determine whether a number is a finite number. The isFinite function examines the number in its argument. If the argument is NaN , positive infinity, or negative infinity, this method returns false ; otherwise, it returns true .

How much is infinity in JavaScript?

POSITIVE_INFINITY (aka Infinity ) or Number. NEGATIVE_INFINITY take in memory for Javascript? The same as all values of type number (that aren't optimized to integers): 8 bytes.


1 Answers

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.

like image 167
Niet the Dark Absol Avatar answered Oct 03 '22 18:10

Niet the Dark Absol