From: Scott Worley Date: Thu, 23 May 2019 21:38:38 +0000 (-0700) Subject: Calculate inverse bounds correctly X-Git-Tag: v1.0~12 X-Git-Url: http://git.scottworley.com/inverse-tax/commitdiff_plain/b442070b44917301d40c5a6cbf996898175908be?ds=sidebyside Calculate inverse bounds correctly --- diff --git a/tax.js b/tax.js index d9bd34c..cd0027a 100644 --- a/tax.js +++ b/tax.js @@ -61,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])); @@ -80,7 +80,7 @@ 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) { diff --git a/tax.test.js b/tax.test.js index 125cc20..27767fe 100644 --- a/tax.test.js +++ b/tax.test.js @@ -59,4 +59,5 @@ test("invert", () => { assert.strictEqual(invert([[10, Infinity, .1]])(19), 20); assert.strictEqual(invert([[10, Infinity, .1]])( 9), 9); assert.strictEqual(invert([[0, 100, .1], [100, Infinity, .2]])(170), 200); + assert.strictEqual(invert([[0, 100, .1], [100, Infinity, .2]])(112), 127.5); });