X-Git-Url: http://git.scottworley.com/inverse-tax/blobdiff_plain/68251a4c105d8c693061b044d45468184232f35c..f0d014934e5c2ddeff4cdb62f609a76cae50ac8e:/tax.js?ds=sidebyside diff --git a/tax.js b/tax.js index f3d2406..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)); @@ -58,7 +66,6 @@ function invert(table) { // 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); @@ -84,7 +91,7 @@ function invert(table) { }); 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; } }