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

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.

Adding to Number.MAX_VALUE

Standardwise… In ECMAScript, addition of two nonzero finite numbers is implemented as (ECMA-262 ยง11.6.3 “Applying the Additive Operators to Numbers”): the sum is computed and rounded to the nearest representable value using IEEE 754 round-to-nearest mode. If the magnitude is too large to represent, the operation overflows and the result is then an infinity of … Read more

How to use nan and inf in C?

You can test if your implementation has it: #include <math.h> #ifdef NAN /* NAN is supported */ #endif #ifdef INFINITY /* INFINITY is supported */ #endif The existence of INFINITY is guaranteed by C99 (or the latest draft at least), and “expands to a constant expression of type float representing positive or unsigned infinity, if … Read more

tech