Why infinity equals to infinity in Javascript - javascript

Why Infinity equals to Infinity in Javascript? Please consider following examples:
Math.pow(10,1000)
The above will evaluate to Infinity.
Math.pow(11,1000)
The above will evaluate to infinity as well.
However in actual Math.pow(11,1000) is greater than Math.pow(10,1100). Please help me to understand the reason behind them being equal.

They're equal because that's how Javascript represents numbers that are too large for it to represent effectively.
The MAX_VALUE property has a value of approximately 1.79E+308. Values larger than MAX_VALUE are represented as "Infinity".
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_VALUE
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity

Related

why does "Infinity==Infinity" in JavaScript becomes true?

As far as I know, in math both Infinity and NaN are vague values.
as all of us know:
console.log(NaN == NaN); //-> false
while
console.log(Infinity==Infinity); //-> true
I'm wondering why the result of the second code is true. I'm expecting that the result of the second one, should be false, but it's not.
Could you please help me out.
I'd really appreciate it. Thanks.
This is why:
NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN
The initial value of Infinity is Number.POSITIVE_INFINITY. The value Infinity (positive infinity) is greater than any other number. This value behaves mathematically like infinity; for example, any positive number multiplied by Infinity is Infinity, and anything divided by Infinity is 0.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity
What you also might be interested in is using the isFinite method of Number:
Number.isFinite(Infinity); // false
Number.isFinite(NaN); // false
Read up on Number.isFinite(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite
In addition to the other answers: Because the spec says so.
NaN is the only value in JavaScript that is not equal to itself:
A reliable way for ECMAScript code to test if a value X is a NaN is an expression of the form X !== X. The result will be true if and only if X is a NaN.
http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.4
Because processors interpret it that way. Most math in JS follows the IEEE-754 specification for floating math arithmetic, which processors implement in pretty specific ways. That includes NaN !== NaN and Infinity === Infinity, among other things.
infinity is treated as a numeric value, so infinity==infinity represents one numeric value equaling another. While in common mathematics the infinity can not be compared against infinity, in javascript it can!
NaN on the other hand, is a type of undefined variable, not a number. So comparisons between NaN are not logically comparable. The proper way to compare against NaN is with the function isNaN.
Example:
isNaN(NaN) // returns true

Why is Javascript's MAX_VALUE an "approximate" according to ECMA

The value of Number.MAX_VALUE is the largest positive finite value of
the Number type, which is approximately 1.7976931348623157 × 10308.
Source
Why "approximately"? Can we not know for sure that this is indeed the maximum positive numeric value?
The answers in this question seem to prove this quite well. Or does approximate mean something different in this context?
The exact value of MAX_VALUE is:
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,
844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,
878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,
546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,
944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,
332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,
250,404,026,184,124,858,368
Does this mean anything more to you than "approximately 1.7976931348623157 × 10308"?
First, you would not want to write a number with 308 digits. There are probably further numbers after the coma, which are not written, and this is the reason it is an approximation.
Second, the Number object can not take all the values between 0 and 1.7976931348623157 × 10^308. It can take all the values between +- 0 and 2^53. For bigger values, it stores a number smaller than 2^53 and an order of magnitude. So you cannot have unit precision, unless the number you want to store happens to be exactly of the form x * 2^e.
Still the biggest number you can store is precisely (2^53 - 1) * 2^971, which approximately equals 1.7976931348623157 * 10^308, which is much easier to read.
(So, get me right, "First" is the real reason, and "Second" is just an explanation of what is the exact value.)
Source : http://www.ecma-international.org/ecma-262/5.1/#sec-8.5

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

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
Because that's how floating-point is defined (more generally than just Javascript). See for example:
http://en.wikipedia.org/wiki/Floating-point#Infinities
http://en.wikipedia.org/wiki/NaN#Creation
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.
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.
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

Javascript Infinity Object

I'm caclulating the mean value of a function's request/sec, appearently the result number sometimes is too long so it displays as Infinity, is there a way to round it so it show a number only? Or make a sleep()/wait() while it's on Infinity?
well to be exactly, im monitoring req/sec on a graph, when it's infinity the line goes up not towards zero
It's not too long to display. If you get Inf then you can't do anything with it other than know that it is something larger than the maximum possible value. This is the behavior of IEEE floating point numbers that are used in JavaScript.
Probably the cause for this Infinity is a division by zero, not a big number.
You are most likely unintentionally dividing by zero.
var num = 1/0;
console.log(num);
//>Infinity
Conditionally check that the divisor is not null.
You can check the maximum value of an integer as follows:
console.log([Number.MAX_VALUE, Number.MIN_VALUE]);
//>[1.7976931348623157e+308, 5e-324]
See also the official ECMA Description on Numbers

Why is Infinity / Infinity not 1?

If
Infinity === Infinity
>> true
and
typeOf Infinity
>> "number"
then why is
Infinity / Infinity
>>NaN
and not 1?
Beware any assumptions you make about the arithmetic behaviour of infinity.
If ∞/∞ = 1, then 1×∞ = ∞. By extension, since 2×∞ = ∞, it must also be the case that ∞/∞ = 2.
Since it has come up in discussion against another answer, I'd like to point out that the equation 2×∞ = ∞ does not imply that there are multiple infinities. All countably infinite sets have the same cardinality. I.e., the set of integers has the same cardinality as the set of odd numbers, even though the second set is missing half the elements from the first set. (OTOH, there are other kinds of "infinity", such as the cardinality of the set of reals, but doubling the countable infinity doesn't produce one of these. Nor does squaring it, for that matter.)
Because the specification says so:
Division of an infinity by an infinity results in NaN.
I'm not a mathematician, but even from that point of view, having 1 as result it does not make sense. Infinities can be different and only because they are equal in JavaScript does not justify treating them as equal in all other cases (or letting the division return 1 for that matter). (edit: as I said, I'm not a mathematician ;)).
The result is mathematically undefined. It has nothing to do with javascript. See the following explanation.
It's recognizable from Calculus one! It's a indeterminate form!

Categories

Resources