Re: mlib: Math Library
Posted: Sun Sep 29, 2013 9:20 pm
I have no idea why I didn't do that earlier...
Code: Select all
function median(tab)
table.sort(tab)
if #tab%2 == 0 then
return (tab[math.floor(#tab/2)]+tab[math.floor(#tab/2+1)])/2
else
return tab[#tab/2+.5]
end
end
Code: Select all
function mlib.get_median( ... )
name = { ... }
for i = 1, #name do
if #name > 2 then
least = math.min( unpack( name ) )
largest = math.max( unpack( name ) )
for ii = 1, #name do
if name[ii] == least then
table.remove( name, ii )
break
end
end
for ii = 1, #name do
if name[ii] == largest then
table.remove( name, ii )
break
end
end
elseif #name == 2 then
median = mlib.get_mean( name[1], name[2] )
return median
elseif #name == 1 then
median = name[1]
return median
end
end
end
Code: Select all
local tab = {unpack(tab)}
Code: Select all
mlib.get_mode( unpack( tab ) )
Code: Select all
mlib.get_mode( tab )
Might want to avoid unnecessary variable creation by replacing:davisdude wrote:Updated GitHub! Functions are now localized( tab )[/code]
Code: Select all
function mlib.get_midpoint( x1, y1, x2, y2 )
local name = { ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 }
return name
end
Code: Select all
function mlib.get_midpoint( x1, y1, x2, y2 )
return { ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 }
end
Assigning the table to a variable is completely pointless here and only makes your code more verbose. If it has any effect on performance at all it will make things slower.davisdude wrote:I read somewhere that it was faster if you assigned variables then called them as opposed to just calling them. I don't know if it's true or not, or if it even really impacts it that much, but...
I guess that does make more sense, but I just thought it would make the code neater anyway...Hexenhammer wrote:davisdude wrote:I read somewhere that it was faster if you assigned variables then called them as opposed to just calling them. I don't know if it's true or not, or if it even really impacts it that much, but...
Assigning the table to a variable is completely pointless here and only makes your code more verbose. If it has any effect on performance at all it will make things slower.