Module:Math:修订间差异

删除的内容 添加的内容
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
IdleTap-bot​(留言 | 贡献)
导入自求闻百科
 
第140行:
local input_string = (args[1] or args.x or '0');
local trap_fraction = args.check_fraction;
local input_number;
 
if not yesno then
第157行 ⟶ 第158行:
end
 
local _input_number, clean_stringinput_string = p._cleanNumber(input_string);
if clean_stringinput_string == nil then
return err('precision input appears non-numeric')
else
return p._precision(clean_stringinput_string)
end
end
第169行 ⟶ 第170行:
x = tostring(x)
end
x = string.upper(x)
 
local i, jdecimal = x:find('%..*$')▼
local exponent_pos = x:find('E')
local result = 0;
 
if exponent_pos ~= nil then
--subtract exponent
local exponent = string.sub(x, exponent_pos + 1)
local _, _, coefficient, exponent = x:find("^(.*)[eE](.*)$")
x = string.sub(x, 1, exponent_pos - 1)
if coefficient then▼
result = result - (tonumber(exponent) or 0)
x = coefficient
end
 
--addif decimal places~= nil then
result = result + string.len(jx) - i)decimal▼
▲ local i, j = x:find('%..*$')
if i then▼
▲ result = result + (j - i)
return result
end
 
local pos = string.len(x);
--subtract trailing zeroes
i,while jx:byte(pos) == x:findstring.byte('[^0]0+$') do
pos = pos - 1
if i then
result = result - (j - i)1
▲ if ipos <= 0 then
return 0
end▼
end
 
第292行 ⟶ 第296行:
 
function p._sum(...)
local sums, count = fold((function(a, b) return a + b end), ...)
if not sums then
return 0
第420行 ⟶ 第424行:
return oldr
end
local result, count = fold(findGcd, ...)
return result
end
第453行 ⟶ 第457行:
end
 
local abs_value = math.abs(value)
local current_precision = p._precision(value)
local order = p._order(value)
第468行 ⟶ 第471行:
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(abs_valuemath.abs(value))
local sign
 
-- Use proper unary minus sign rather than ASCII default▼
▲ if coefficientvalue < 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(abs_valuemath.abs(value))
else
order = 0;
end
formatted_num = ((value < 0) and '−' or '')sign .. formatted_num▼
▲ -- Use proper unary minus sign rather than ASCII default
▲ formatted_num = ((value < 0) and '−' or '') .. formatted_num
 
-- Pad with zeros, if needed
第582行 ⟶ 第589行:
]]
 
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.
第593行 ⟶ 第600行:
-- 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
第630行 ⟶ 第628行:
]]
 
local mt = { __index = function(_t, k)
return function(frame)
if not getArgs then