1,331
edits
(Copied from page of same name from Wikipedia) |
(Changed from area1 to area) |
||
Line 41: | Line 41: | ||
} | } | ||
local function popdensity(pop, | local function popdensity(pop, area, areaunit1, areaunit2, prec, disp, flip) | ||
local dens1, prec1 = nil, nil | local dens1, prec1 = nil, nil | ||
local dens2, prec2 = nil, nil | local dens2, prec2 = nil, nil | ||
Line 47: | Line 47: | ||
local uniterror = '<sup>[[Template:Pop density|?Unknown unit?]]</sup>[[Category:Pop density using unsupported units]]' | local uniterror = '<sup>[[Template:Pop density|?Unknown unit?]]</sup>[[Category:Pop density using unsupported units]]' | ||
if(pop ~= '' and | if(pop ~= '' and area ~= '') then | ||
-- pop and area are defined | -- pop and area are defined | ||
local popnum = tonumber(pop) | local popnum = tonumber(pop) | ||
Line 53: | Line 53: | ||
error(string.format('Unable to convert population "%s" to a number', pop), 2) | error(string.format('Unable to convert population "%s" to a number', pop), 2) | ||
end | end | ||
local area1num = tonumber( | local area1num = tonumber(area) | ||
if not area1num then | if not area1num then | ||
error(string.format('Unable to convert area "%s" to a number', | error(string.format('Unable to convert area "%s" to a number', area), 2) | ||
end | end | ||
local dens1num = popnum/area1num | local dens1num = popnum/area1num | ||
Line 62: | Line 62: | ||
local unit2 = unitnames[areaunit2] or nil | local unit2 = unitnames[areaunit2] or nil | ||
prec1 = (prec ~= '') and tonumber(prec) or (1+math.log10(2* | prec1 = (prec ~= '') and tonumber(prec) or (1+math.log10(2*area/(1/10^precision(pop)+pop/area/10^precision(area)))) | ||
dens1 = rnd(dens1num, math.floor(prec1 + 0.5)) | dens1 = rnd(dens1num, math.floor(prec1 + 0.5)) | ||
if (unit1) then | if (unit1) then |