Added option for disabling soft numbers.
This commit is contained in:
parent
153bf23a9f
commit
4e7be31964
|
@ -132,7 +132,7 @@ local SPECIAL_NUMBER = {
|
|||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
function format_num (n, shorthand)
|
||||
function format_num (n, shorthand, soft_numbers)
|
||||
if n ~= n then return shorthand and 'NaN' or '0/0'
|
||||
elseif n < 0 then return '-' .. format_num(-n, shorthand)
|
||||
end
|
||||
|
@ -152,7 +152,7 @@ function format_num (n, shorthand)
|
|||
local num = special_number_tests.real(unpack(a))
|
||||
if num == n then
|
||||
alternative_repr( special_number_tests[shorthand and 'short' or 'repr'](unpack(a)) )
|
||||
elseif num then
|
||||
elseif num and soft_numbers then
|
||||
local repr = special_number_tests[shorthand and 'short' or 'repr'](unpack(a))
|
||||
local native_precise = tonumber(('%'..utf8.width(repr)..'f'):format(n))
|
||||
if math.abs(num - n) <= math.abs( native_precise - n ) then
|
||||
|
@ -173,5 +173,5 @@ return function (value, depth, l)
|
|||
assert(type(value) == 'number')
|
||||
assert(type(depth) == 'number' and type(l) == 'table')
|
||||
-- TODO: Add support for more relaxed representations.
|
||||
l[#l+1] = format_num(value, CAN_USE_SHORTHAND and l.options.math_shorthand)
|
||||
l[#l+1] = format_num(value, CAN_USE_SHORTHAND and l.options.math_shorthand, l.options.soft_numbers)
|
||||
end
|
||||
|
|
|
@ -469,6 +469,7 @@ local KNOWN_OPTIONS = {
|
|||
include_closure = { type = 'boolean', default = false },
|
||||
indent = { type = 'string', default = '\t' }, -- TODO: Change default to ' '.
|
||||
math_shorthand = { type = 'boolean', default = false },
|
||||
soft_numbers = { type = 'boolean', default = true }, -- TODO: Add support for maximally precise numbers.
|
||||
max_depth = { type = 'number', default = math.huge },
|
||||
more_function_info = { type = 'boolean', default = false },
|
||||
recursion = { type = 'string', default = 'ignore', accepted = {['ignore'] = true, ['marked'] = true} },
|
||||
|
@ -489,7 +490,7 @@ local function ensure_that_all_options_are_known (options)
|
|||
end
|
||||
-- Assign default values
|
||||
for option_name, option_info in pairs(KNOWN_OPTIONS) do
|
||||
if not options[option_name] then
|
||||
if options[option_name] == nil then
|
||||
options[option_name] = option_info.default
|
||||
end
|
||||
end
|
||||
|
|
|
@ -189,6 +189,11 @@ do
|
|||
expect = 'math.pi',
|
||||
shorthand = 'π',
|
||||
}
|
||||
format_test {
|
||||
input = sum,
|
||||
options = { soft_numbers = false },
|
||||
expect = tostring(sum),
|
||||
}
|
||||
end
|
||||
|
||||
number_test {
|
||||
|
|
Loading…
Reference in New Issue
Block a user