I have just made a series of tests on Windows 8.1, and this particular array gives a wrong median value (4089.51507) with Math.median(), Matrix.median() and Vector.median(). Not surprising because the three methods share the same routine to load array elements. Funny enough, if you prepend 0 and 1 to the array:
let data = [ 0, 1,
0.016266, 0.019074, 0.018753, 0.023484, 0.021546, 0.024460, 0.025620, 0.021302,
...
then the routines give the correct result (7091.01807). However, if one adds the same values at the end of the array:
...
0.020081, 0.018997, 0.018662, 0.019059, 0.019791, 0.018891, 0.018250, 0.016617, 0, 1
];
Then the routines give another wrong result (6529.0226775).
I can almost guarantee that this is a compiler bug (Visual C++ 2013). I have seen many VC++ bugs in the past, and the JavaScript engine has been particularly "good" at exposing them. I've seen really strange problems caused by VC++ "peculiarities".
Here is a modified version of the test script, which shows no problems for medians of 10000 arrays of random lengths and element values:
function trivialMedian( A )
{
A.sort( function( a, b ) { return a - b; } );
let n2 = A.length >> 1;
if ( A.length & 1 )
return A[n2];
return (A[n2] + A[n2-1])/2;
}
#define N 10000
for ( let i = 0; i < N; ++i )
{
let A = [];
let n = Math.random()*16000 + 100;
for ( let i = 0; i < n; ++i )
A.push( Math.random() );
//let A = Math.randomArray( n ); // equivalent to code above
let m1 = Math.median( A ); // call this one before...
let m2 = trivialMedian( A ); // ...because trivialMedian() modifies the array
if ( m1 != m2 || i == N-1 )
{
console.writeln( format( "iteration : %d\n"
+ "A.length : %d\n"
+ "trivialMedian() : %.16e\n"
+ "Math.median() : %.16e", i, n, m2, m1 ) );
break;
}
}
However, the array that you have found always gives wrong values on Windows. Welcome to the wonderland of poor compilers written by huge multinational companies...
As soon as I return home I'll try to find a workaround to this VC++ bug. Sorry for the trouble.