Module:Math: Difference between revisions

typo fix
 
m 1 revision imported: Content from Template:Location Map on Wikipedia
 
(2 intermediate revisions by 2 users not shown)
Line 140: Line 140:
local input_string = (args[1] or args.x or '0');
local input_string = (args[1] or args.x or '0');
local trap_fraction = args.check_fraction;
local trap_fraction = args.check_fraction;
local input_number;


if not yesno then
if not yesno then
Line 158: Line 157:
end
end


input_number, input_string = p._cleanNumber(input_string);
local _, clean_string = p._cleanNumber(input_string);
if input_string == nil then
if clean_string == nil then
return err('precision input appears non-numeric')
return err('precision input appears non-numeric')
else
else
return p._precision(input_string)
return p._precision(clean_string)
end
end
end
end
Line 170: Line 169:
x = tostring(x)
x = tostring(x)
end
end
x = string.upper(x)


local decimal = x:find('%.')
local exponent_pos = x:find('E')
local result = 0;
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)
result = result - (tonumber(exponent) or 0)
x = coefficient
end
end


if decimal ~= nil then
--add decimal places
result = result + string.len(x) - decimal
local i, j = x:find('%..*$')
if i then
result = result + (j - i)
return result
return result
end
end


local pos = string.len(x);
--subtract trailing zeroes
while x:byte(pos) == string.byte('0') do
i, j = x:find('[^0]0+$')
pos = pos - 1
if i then
result = result - 1
result = result - (j - i)
if pos <= 0 then
return 0
end
end
end


Line 296: Line 292:


function p._sum(...)
function p._sum(...)
local sums, count = fold((function(a, b) return a + b end), ...)
local sums = fold((function(a, b) return a + b end), ...)
if not sums then
if not sums then
return 0
return 0
Line 424: Line 420:
return oldr
return oldr
end
end
local result, count = fold(findGcd, ...)
local result = fold(findGcd, ...)
return result
return result
end
end
Line 457: Line 453:
end
end


local abs_value = math.abs(value)
local current_precision = p._precision(value)
local current_precision = p._precision(value)
local order = p._order(value)
local order = p._order(value)
Line 471: Line 468:
if precision < current_precision then
if precision < current_precision then
value = p._round(value, precision)
value = p._round(value, precision)
abs_value = math.abs(value)
current_precision = p._precision(value)
current_precision = p._precision(value)
end
end


local formatted_num = lang:formatNum(math.abs(value))
local formatted_num = lang:formatNum(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
-- Handle cases requiring scientific notation
if string.find(formatted_num, 'E', 1, true) ~= nil or math.abs(order) >= 9 then
if math.abs(order) >= 9 or string.find(formatted_num, 'E', 1, true) then
value = value * math.pow(10, -order)
value = value * (10 ^ (-order))
abs_value = math.abs(value)
current_precision = current_precision + order
current_precision = current_precision + order
precision = precision + order
precision = precision + order
formatted_num = lang:formatNum(math.abs(value))
formatted_num = lang:formatNum(abs_value)
else
else
order = 0;
order = 0;
end
end
formatted_num = 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
-- Pad with zeros, if needed
Line 589: Line 582:
]]
]]


function p._cleanNumber(number_string)
function p._cleanNumber(number_string, unformat)
if type(number_string) == 'number' then
if type(number_string) == 'number' then
-- We were passed a number, so we don't need to do any processing.
-- We were passed a number, so we don't need to do any processing.
Line 600: Line 593:
-- Attempt basic conversion
-- Attempt basic conversion
local number = tonumber(number_string)
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
-- If failed, attempt to evaluate input as an expression
Line 628: Line 630:
]]
]]


local mt = { __index = function(t, k)
local mt = { __index = function(_, k)
return function(frame)
return function(frame)
if not getArgs then
if not getArgs then