Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x >= x pattern in JavaScript

When reading source of D3.js I saw x >= x pattern. If it is for detecting NaNs among numbers, why not just isNaN(x) or x == x?

Source, where I encountered it:

d3.min = function(array, f) {   var i = -1, n = array.length, a, b;   if (arguments.length === 1) {     while (++i < n) if ((b = array[i]) != null && b >= b) {       a = b;       break;     }     while (++i < n) if ((b = array[i]) != null && a > b) a = b;   } else {     while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {       a = b;       break;     }     while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;   }   return a; }; 
like image 341
Piotr Migdal Avatar asked Jun 21 '15 17:06

Piotr Migdal


People also ask

How to create pattern in JavaScript?

Steps to create a hollow square star pattern are: Create a variable to store the string and assign it with an empty string. Create a for loop to run for 'n' number of times, where 'n' is number of rows/columns in the square, i.e for(let i = 0; i < n; i++)

What is pattern in JavaScript?

A pattern is a reusable solution that can be applied to commonly occurring problems in software design - in our case - in writing JavaScript web applications. Another way of looking at patterns are as templates for how we solve problems - ones which can be used in quite a few different situations.


1 Answers

From my investigations, d3.min is supposed to work on any kind of orderable values, not only numbers. isNaN would only work numbers.

d3 was actually using == at some point. This commit introduced the x == x test:

Unlike Math.min and Math.max, it doesn't make sense to return negative or positive infinity for d3.min and d3.max; the D3 functions return the minimum value according to an arbitrary ordering, not by numeric value. Instead, the minimum or maximum of an empty array, or an array that contains only degenerate values, should always be undefined.

This commit changed x == x to x <= x (which was later again changed to x >= x):

In addition to NaN, which is not equal to itself, you can have objects that are not orderable due to defined valueOf functions which return NaN. For example:

var o = new Number(NaN); 

Here, o == o is true, but o <= o is false. Therefore it was possible for d3.min, d3.max and d3.extent to observe these non-orderable values rather than ignore them as intended. The fix is to check !(o <= o) rather than o == o.

like image 88
Felix Kling Avatar answered Sep 19 '22 18:09

Felix Kling