وحدة:Wikidata2/Math

من موسوعة المزرعة
اذهب إلى التنقل اذهب إلى البحث

يمكن إنشاء صفحة توثيق الوحدة في وحدة:Wikidata2/Math/شرح

--[[

This module provides a number of basic mathematical operations.

]]

local yesno, getArgs -- lazily initialized

local 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;
end
	
function p.newFromWikidataValue(frame)
	upeer = frame.upperBound if not upeer then upeer = '0' end
	lower = frame.lowerBound if not lower then lower = '0' end
	local diff = math.abs(tonumber(upeer) - tonumber(frame.amount))
	local diff2 = math.abs(tonumber(lower) - tonumber(frame.amount)) 
		if diff2 > diff then
			diff = diff2
		end

	-- TODO, att fixa så att inte 1234.000 'huggs av' till 1234
	local lang = mw.language.new( 'ar' )
	if diff == 0 then
		return lang:formatNum(tonumber(frame.amount))
	else
		local log = -math.log10(diff)
		return lang:formatNum(p._round(frame.amount, math.ceil(log)))
	end
end

local mt = { __index = function(t, k)
	return function(frame)
		if not getArgs then
			getArgs = require('Module:Arguments').getArgs
		end
		return wrap[k](getArgs(frame))  -- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed.
	end
end }

return setmetatable(p, mt)