Repository metrics
- Stars
- (12 stars)
- PR merge metrics
- (PR metrics pending)
Description
If talking about "there can be only one!" which is stated in repo description, it's not precisely true. In fact there is much more NaN values than you get using regular operations and thus array returned by library would be never complete.
In particular, in IEEE.754, any value that is bigger than encoded maximum (Infinity) is considered impossible number, or what we call not-a-number (NaN).
Let's try to check it.
Firstly, let's split Infinity (it's a number and thus 64-bit float in JavaScript) into lower and higher 32-bit parts:
var infinities = new Float64Array([Infinity]); // [Infinity]
var uint32parts = new Uint32Array(infinities.buffer); // [0, 2146435072]
Now, let's try to increase lower part by 1 and produce float number back:
var uint32parts = new Uint32Array([1, 2146435072]);
var float = new Float64Array(uint32parts.buffer)[0]; // NaN
Let's try to increase it by 2:
var uint32parts = new Uint32Array([2, 2146435072]);
var float = new Float64Array(uint32parts.buffer)[0]; // NaN
We see that different binary representations produce numbers that are considered NaNs because they come after Infinity in binary representations.
Possible combinations for the low part: 0..(232 - 1) => count: 232 Possible combinations for the high part: 2146435072..(231 - 1) => count: 1048575 = 220 - 1
Total count of possible combinations: 232 x (220 - 1) - 1 = 252 - 232 - 1 = 4,503,595,332,403,199 different NaNs.
And it's still not the end. We used 231 - 1 as highest number for high part because the highest bit of float64 is used as sign of number in IEEE.754. But numbers, less than -Infinity are also NaNs!
Thus we need to multiply total count by 2 and we get final count of possible NaNs: 9,007,190,664,806,398
As you can see, variations returned by this library fall perfectly into the range but it would be never complete and "only one NaN" is not really possible.