diff --git a/number.lua b/number.lua index 9bc639a..45aabdf 100644 --- a/number.lua +++ b/number.lua @@ -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 diff --git a/pretty.lua b/pretty.lua index d0c1b50..a3f5447 100644 --- a/pretty.lua +++ b/pretty.lua @@ -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 diff --git a/test/test_number.lua b/test/test_number.lua index 950f97c..438daaf 100644 --- a/test/test_number.lua +++ b/test/test_number.lua @@ -189,6 +189,11 @@ do expect = 'math.pi', shorthand = 'π', } + format_test { + input = sum, + options = { soft_numbers = false }, + expect = tostring(sum), + } end number_test {