1
0

Some numbers very close to a short representation will now use that representation.

This commit is contained in:
Jon Michael Aanes 2017-06-09 16:57:21 +02:00
parent 531967d13b
commit 153bf23a9f
2 changed files with 18 additions and 1 deletions

View File

@ -149,8 +149,15 @@ function format_num (n, shorthand)
local a = { special_number_tests.est(n) }
if a[1] then
for i = 1, #a do a[i] = math.floor(a[i] + 0.5) end
if n == special_number_tests.real(unpack(a)) then
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
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
alternative_repr( repr )
end
end
end
end

View File

@ -82,6 +82,16 @@ number_test {
expect = '0.65',
}
number_test {
input = 2.05,
expect = '2.05',
}
number_test {
input = 2.0512523,
expect = '2.0512523',
}
--------------------------------------------------------------------------------
-- Fractions