X-Git-Url: http://git.scottworley.com/inverse-tax/blobdiff_plain/1c6e40691d86539d80018aa8fc495272b900c4c4..e66063161c51b0956d2ff3acabf778e8f2b4d045:/tax.js?ds=sidebyside diff --git a/tax.js b/tax.js index d26db5f..10c44f3 100644 --- a/tax.js +++ b/tax.js @@ -1,5 +1,13 @@ "use strict"; +function near(a, b, epsilon = 1e-6) { + return Math.abs(a - b) < epsilon; +} + +function less_than_or_near(a, b, epsilon = 1e-6) { + return a < b || near(a, b, epsilon); +} + function parse_table(as_text) { function parse_line(line) { return line.trim().split(' ').filter(x => x !== '').map(x => parseFloat(x)); @@ -7,11 +15,15 @@ function parse_table(as_text) { return as_text.trim().split('\n').map(parse_line); } -function parse_tax_table(as_text) { - return parse_table(as_text).map(([start, rate], i, table) => +function tax_table_from_table(table) { + return table.map(([start, rate], i, table) => [start, i < table.length - 1 ? table[i+1][0] : Infinity, rate / 100.0]); } +function parse_tax_table(as_text) { + return tax_table_from_table(parse_table(as_text)); +} + function sum(nums) { return nums.reduce((total, num) => total + num, 0); } @@ -49,17 +61,14 @@ 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: // net - b = m * gross // (net - b) / m = gross - // 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])); @@ -78,13 +87,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 (less_than_or_near(start, net) && less_than_or_near(net, end)) { return (net - b) / m; } } + return net; }; }