#author("2019-04-14T01:02:05+09:00","","") #author("2019-04-14T01:03:40+09:00","","") *Luaで良く作る関数 ~math編~ [#ke1f7d22] -[[math.round>#math_round]] -[[math.type>#math_type]] -[[行列やベクトル>#math_matrix]] -[[複素数>#math_complex]] &aname(math_round); **math.round [#zf0d6a34] #sh(lua){{ -- 四捨五入。実数numを、小数idp桁で丸める。 function math.round(num, idp) if idp and idp>0 then local mult = 10^idp return math.floor(num * mult + 0.5) / mult end return math.floor(num + 0.5) end }} &aname(math_type); **math.type [#u90e429f] #sh(lua){{ -- 対象が整数なら"integer"、実数なら"float"、数値でないならnilが返ってくる。 -- Lua5.3に同じ関数が存在する。 math.type = math.type or function(num) if type(num) == "number" then if num%1 == 0 then return "integer" else return "float" end end return nil end }} &aname(math_hex); **math.hex [#h773c971] #sh(lua){{ -- 対象の引数の整数を16進の文字列に変換します。 function math.hex(num) return string.format("%x", num) end }} **行列やベクトル [#eb114eb7] &aname(math_matrix); - [[davidm/lua-matrix>https://github.com/davidm/lua-matrix/tree/master/lua]] #ref(http://xn--pckzexbx21r8q9b.net/lua_tips/download/func/math/matrix.lua) **複素数 [#mff0282b] &aname(math_complex); - [[davidm/lua-matrix>https://github.com/davidm/lua-matrix/tree/master/lua]] #ref(http://lua.tips/download/func/math/complex.lua) #ref(http://xn--pckzexbx21r8q9b.net/lua_tips/download/func/math/complex.lua)