1
0

Integers should be preferred to 2^x

This commit is contained in:
Jon Michael Aanes 2017-04-30 22:40:24 +02:00
parent 0f26759ee2
commit 0ab7942023
3 changed files with 31 additions and 8 deletions

2
README.md Normal file
View File

@ -0,0 +1,2 @@
README is TODO

View File

@ -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

View File

@ -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 {