Some numbers very close to a short representation will now use that representation.
This commit is contained in:
parent
531967d13b
commit
153bf23a9f
|
@ -149,8 +149,15 @@ function format_num (n, shorthand)
|
||||||
local a = { special_number_tests.est(n) }
|
local a = { special_number_tests.est(n) }
|
||||||
if a[1] then
|
if a[1] then
|
||||||
for i = 1, #a do a[i] = math.floor(a[i] + 0.5) end
|
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)) )
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -82,6 +82,16 @@ number_test {
|
||||||
expect = '0.65',
|
expect = '0.65',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
number_test {
|
||||||
|
input = 2.05,
|
||||||
|
expect = '2.05',
|
||||||
|
}
|
||||||
|
|
||||||
|
number_test {
|
||||||
|
input = 2.0512523,
|
||||||
|
expect = '2.0512523',
|
||||||
|
}
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Fractions
|
-- Fractions
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user