Comparison

util/statistics.lua @ 6562:2b5ced5ca31f

util.statistics: Return 0 as percentile if data out of range
author Matthew Wild <mwild1@gmail.com>
date Fri, 30 Jan 2015 14:21:51 +0000
parent 6555:7b2d16c14659
child 6649:99fa40d498cc
comparison
equal deleted inserted replaced
6559:0ef7ca5276a1 6562:2b5ced5ca31f
6 6
7 local function percentile(arr, length, pc) 7 local function percentile(arr, length, pc)
8 local n = pc/100 * (length + 1); 8 local n = pc/100 * (length + 1);
9 local k, d = m_floor(n), n%1; 9 local k, d = m_floor(n), n%1;
10 if k == 0 then 10 if k == 0 then
11 return arr[1]; 11 return arr[1] or 0;
12 elseif k >= length then 12 elseif k >= length then
13 return arr[length]; 13 return arr[length];
14 end 14 end
15 return arr[k] + d*(arr[k+1] - arr[k]); 15 return arr[k] + d*(arr[k+1] - arr[k]);
16 end 16 end