Integers should be preferred to 2^x
This commit is contained in:
parent
0f26759ee2
commit
0ab7942023
13
number.lua
13
number.lua
|
@ -58,6 +58,12 @@ local SPECIAL_NUMBER = {
|
||||||
real = function (a) return math.huge end,
|
real = function (a) return math.huge end,
|
||||||
repr = function (a) return '1/0' end,
|
repr = function (a) return '1/0' end,
|
||||||
short = function (a) return '∞' 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
|
-- x = a/b
|
||||||
{ est = calculate_fraction,
|
{ est = calculate_fraction,
|
||||||
|
@ -113,12 +119,6 @@ local SPECIAL_NUMBER = {
|
||||||
repr = function (a) return ('math.sqrt(%i)'):format(a) end,
|
repr = function (a) return ('math.sqrt(%i)'):format(a) end,
|
||||||
short = function (a) return '√'..utf8.overline(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
|
end
|
||||||
|
|
||||||
return function (value, options, depth, l)
|
return function (value, options, depth, l)
|
||||||
|
-- TODO: Add support for more relaxed representations.
|
||||||
l[#l+1] = format_num(value, options.math_shorthand)
|
l[#l+1] = format_num(value, options.math_shorthand)
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,10 +11,10 @@ local function format_test (t)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function number_test (t)
|
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, {}))
|
assert_equal(t.expect, format(t.input, {}))
|
||||||
end)
|
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 }))
|
assert_equal(t.shorthand or t.expect, format(t.input, { math_shorthand = true }))
|
||||||
end, { line = debug.getinfo(2).currentline })
|
end, { line = debug.getinfo(2).currentline })
|
||||||
end
|
end
|
||||||
|
@ -167,6 +167,26 @@ number_test {
|
||||||
shorthand = 'NaN',
|
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 {
|
number_test {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user