Module:Math:修订间差异

删除的内容 添加的内容
zhwiki>Shizhao
无编辑摘要
 
enwiki>Ahecht
Rewrite rounding and precision code to run faster, other code cleanup including removing unused variables. Please revert if this causes any unexpected side effects
第140行:
local input_string = (args[1] or args.x or '0');
local trap_fraction = args.check_fraction;
local input_number;
 
if not yesno then
第158行 ⟶ 第157行:
end
 
input_numberlocal _, input_stringclean_string = p._cleanNumber(input_string);
if input_stringclean_string == nil then
return err('precision input appears non-numeric')
else
return p._precision(input_stringclean_string)
end
end
第170行 ⟶ 第169行:
x = tostring(x)
end
x = string.upper(x)
 
local decimal = x:find('%.')▼
local exponent_pos = x:find('E')
local result = 0;
 
--subtract exponent
if exponent_pos ~= nil then
local _, _, coefficient, exponent = x:find("^(.*)[eE](.*)$")
local exponent = string.sub(x, exponent_pos + 1)
if value < 0coefficient then▼
x = string.sub(x, 1, exponent_pos - 1)
result = result - (tonumber(exponent) or 0)
x = coefficient
end
 
if--add decimal ~= nil thenplaces
▲ local decimali, j = x:find('%..*$')
result = result + string.len(x) - decimal▼
if pos <= 0i then▼
▲ result = result + string.len(x)j - decimali)
return result
end
 
--subtract trailing zeroes
local pos = string.len(x);
whilei, x:byte(pos)j == string.bytex:find('[^0]0+$') do
if i then
pos = pos - 1
result = result - 1(j - i)
▲ if pos <= 0 then
return 0
end▼
end
 
第296行 ⟶ 第292行:
 
function p._sum(...)
local sums, count = fold((function(a, b) return a + b end), ...)
if not sums then
return 0
第424行 ⟶ 第420行:
return oldr
end
local result, count = fold(findGcd, ...)
return result
end
第457行 ⟶ 第453行:
end
 
local abs_value = math.abs(value)
local current_precision = p._precision(value)
local order = p._order(value)
第471行 ⟶ 第468行:
if precision < current_precision then
value = p._round(value, precision)
abs_value = math.abs(value)
current_precision = p._precision(value)
end
 
local formatted_num = lang:formatNum(math.abs(value)abs_value)
local sign
 
-- Use proper unary minus sign rather than ASCII default▼
▲ if value < 0 then
sign = '−'
else
sign = ''
end
 
-- Handle cases requiring scientific notation
if math.abs(order) >= 9 or string.find(formatted_num, 'E', 1, true) ~= nil or math.abs(order) >= 9 then
value = value * math.pow(10, ^ (-order))
abs_value = math.abs(value)
current_precision = current_precision + order
precision = precision + order
formatted_num = lang:formatNum(math.abs(value)abs_value)
else
order = 0;
end
formatted_num = sign .. formatted_num▼
▲ -- Use proper unary minus sign rather than ASCII default
▲ formatted_num = sign((value < 0) and '−' or '') .. formatted_num
 
-- Pad with zeros, if needed
第589行 ⟶ 第582行:
]]
 
function p._cleanNumber(number_string, unformat)
if type(number_string) == 'number' then
-- We were passed a number, so we don't need to do any processing.
第600行 ⟶ 第593行:
-- Attempt basic conversion
local number = tonumber(number_string)
-- If failed, try unformatting the number_string
if unformat and number == nil then
-- Cannot use mw.language.getContentLanguage():parseFormattedNumber()
-- because, unlike {{formatnum:|R}}, it won't ignore operators (it
-- would return an error on "1,234+1,234" instead of "1234+1234")
number_string = mw.getCurrentFrame():callParserFunction("formatnum", {number_string, "R"})
number = tonumber(number_string)
▲ end
 
-- If failed, attempt to evaluate input as an expression
第628行 ⟶ 第630行:
]]
 
local mt = { __index = function(t_, k)
return function(frame)
if not getArgs then