X-Git-Url: http://git.scottworley.com/inverse-tax/blobdiff_plain/a659a4f09cf859b2a63dc5c15f0d4589220c9a83..7a5e093ab307db6abaa51db26c319ec0eaa73438:/tax.js?ds=sidebyside diff --git a/tax.js b/tax.js index 7f32f74..f3d2406 100644 --- a/tax.js +++ b/tax.js @@ -53,8 +53,6 @@ function merge_tax_tables(t1, t2) { } function invert(table) { - if (table.length == 0) return x => x; - // Here we solve // net = m * gross + b // for gross: @@ -63,7 +61,7 @@ function invert(table) { // and the calculate the inverse's bounds const ms = table.map(([start, end, rate]) => 1 - rate); - const full_brackets = [[0]].concat(table.map(([start, end, rate]) => (end - start) * rate)).slice(0, table.length); + const full_brackets = [0].concat(table.map(([start, end, rate]) => (end - start) * rate)).slice(0, table.length); function sum_lower_brackets(remaining_brackets, acc = 0) { if (remaining_brackets.length == 0) return []; return [acc + remaining_brackets[0]].concat(sum_lower_brackets(remaining_brackets.slice(1), acc + remaining_brackets[0])); @@ -82,13 +80,14 @@ function invert(table) { const inverse_table = table.map(([start, end, rate], i) => { const m = ms[i]; const b = bs[i]; - return [(start - b) / m, (end - b) / m, m, b]; + return [m * start + b, m * end + b, m, b]; }); return function(net) { for (const [start, end, m, b] of inverse_table) { - if (start < net && net < end) { + if (start <= net && net <= end) { return (net - b) / m; } } + return net; }; }