ماڈیول:Wikidata2/Math

Documentation icon دستاویز
--[[This module provides a number of basic mathematical operations.]]local yesno, getArgs -- lazily initializedlocal p = {} -- Holds functions to be returned from #invoke, and functions to make available to other Lua modules.local wrap = {} -- Holds wrapper functions that process arguments from #invoke. These act as intemediary between functions meant for #invoke and functions meant for Lua.function p._round(value, precision)local rescale = math.pow(10, precision or 0);return math.floor(value * rescale + 0.5) / rescale;endfunction p.newFromWikidataValue(frame)local upperBound = frame.upperBound or frame.amountlocal lowerBound = frame.lowerBound or frame.amountlocal diff = math.abs(tonumber(upperBound) - tonumber(frame.amount))local diff2 = math.abs(tonumber(lowerBound) - tonumber(frame.amount)) if diff2 > diff thendiff = diff2end-- TODO, att fixa så att inte 1234.000 'huggs av' till 1234local lang = mw.language.new( 'ur' )if diff == 0 thenreturn lang:formatNum(tonumber(frame.amount))elselocal log = -math.log10(diff)return lang:formatNum(p._round(frame.amount, math.ceil(log)))endendlocal mt = { __index = function(t, k)return function(frame)if not getArgs thengetArgs = require('Module:Arguments').getArgsendreturn wrap[k](getArgs(frame))  -- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed.endend }return setmetatable(p, mt)