From 0ab7942023690ca6d6a67e817e3cedc147ff788f Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 30 Apr 2017 22:40:24 +0200 Subject: [PATCH] Integers should be preferred to 2^x --- README.md | 2 ++ number.lua | 13 +++++++------ test/test_number.lua | 24 ++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..cd0b596 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ + +README is TODO diff --git a/number.lua b/number.lua index 3990172..ba2b49d 100644 --- a/number.lua +++ b/number.lua @@ -59,6 +59,12 @@ local SPECIAL_NUMBER = { repr = function (a) return '1/0' end, short = function (a) return '∞' end, }, + -- a = x + { est = function (n) return n end, + real = function (a) return a end, + repr = function (a) return ('%i'):format(a) end, + short = function (a) return ('%i'):format(a) end, + }, -- x = a/b { est = calculate_fraction, real = function (a, b) return b ~= 1 and (a/b) end, @@ -113,12 +119,6 @@ local SPECIAL_NUMBER = { repr = function (a) return ('math.sqrt(%i)'):format(a) end, short = function (a) return '√'..utf8.overline(a) end, }, - -- a = x - { est = function (n) return n end, - real = function (a) return a end, - repr = function (a) return ('%i'):format(a) end, - short = function (a) return ('%i'):format(a) end, - }, } -------------------------------------------------------------------------------- @@ -154,5 +154,6 @@ function format_num (n, shorthand) end return function (value, options, depth, l) + -- TODO: Add support for more relaxed representations. l[#l+1] = format_num(value, options.math_shorthand) end diff --git a/test/test_number.lua b/test/test_number.lua index 7464318..a4a1ac9 100644 --- a/test/test_number.lua +++ b/test/test_number.lua @@ -11,10 +11,10 @@ local function format_test (t) end local function number_test (t) - SUITE:addTest(t.expect, function () + SUITE:addTest(t.name or t.expect, function () assert_equal(t.expect, format(t.input, {})) end) - SUITE:addTest(t.shorthand or (t.expect .. ' shorthand'), function () + SUITE:addTest(t.name and (t.name .. ' shorthand') or t.shorthand or (t.expect .. ' shorthand'), function () assert_equal(t.shorthand or t.expect, format(t.input, { math_shorthand = true })) end, { line = debug.getinfo(2).currentline }) end @@ -167,6 +167,26 @@ number_test { shorthand = 'NaN', } +-------------------------------------------------------------------------------- +-- Use the expression closest to the real value + +do + local sum = 0 + for i = 1, 100 do sum = sum + math.pi/100 end + number_test { + name = 'Approx π', + input = sum, + expect = 'math.pi', + shorthand = 'π', + } +end + +number_test { + input = 4*4, + expect = '16', +} + + -------------------------------------------------------------------------------- number_test {