Luaで良く作る関数 ~math編~

math.round

-- 四捨五入。実数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

math.type

-- 対象が整数なら"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

math.hex

-- 対象の引数の整数を16進の文字列に変換します。
function math.hex(num)
    return string.format("%x", num)
end

行列やベクトル

複素数


トップ   差分 履歴 リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-04-14 (日) 00:00:00