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;
if not yesno then
第158行 ⟶ 第157行:
end
if
return err('precision input appears non-numeric')
else
return p._precision(
end
end
第170行 ⟶ 第169行:
x = tostring(x)
end
local decimal = x:find('%.')▼
local result = 0;
--subtract exponent
local _, _, coefficient, exponent = x:find("^(.*)[eE](.*)$")
result = result - (tonumber(exponent) or 0)
x = coefficient
end
result = result + string.len(x) - decimal▼
return result
end
--subtract trailing zeroes
if i then
result = result -
▲ if pos <= 0 then
end▼
end
第296行 ⟶ 第292行:
function p._sum(...)
local sums
if not sums then
return 0
第424行 ⟶ 第420行:
return oldr
end
local result
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(
-- Use proper unary minus sign rather than ASCII default▼
▲ if value < 0 then
-- Handle cases requiring scientific notation
if math.abs(order) >= 9 or string.find(formatted_num, 'E', 1, true)
value = value *
abs_value = math.abs(value)
current_precision = current_precision + order
precision = precision + order
formatted_num = lang:formatNum(
else
order = 0;
end
formatted_num = sign .. formatted_num▼
▲ -- Use proper unary minus sign rather than ASCII default
-- 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)
-- If failed, attempt to evaluate input as an expression
第628行 ⟶ 第630行:
]]
local mt = { __index = function(
return function(frame)
if not getArgs then
| |||