From: Scott Worley Date: Fri, 24 May 2019 15:04:04 +0000 (-0700) Subject: Loose equality comparisons for floats X-Git-Tag: v1.0 X-Git-Url: http://git.scottworley.com/inverse-tax/commitdiff_plain/refs/tags/v1.0 Loose equality comparisons for floats --- diff --git a/tax.js b/tax.js index 2687387..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)); @@ -83,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; } } diff --git a/tax.test.js b/tax.test.js index 29f436f..5080001 100644 --- a/tax.test.js +++ b/tax.test.js @@ -6,10 +6,6 @@ function test(description, f) { f(); } -function near(a, b, epsilon = 1e-6) { - return Math.abs(a - b) < epsilon; -} - function rand(limit = 99) { return Math.round(Math.random() * limit); }