Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local util = {}
- local selection = game.Selection
- util.base = {}
- util.Instance = {}
- util.misc = {}
- util.scientificNotation = {}
- util.string = {}
- util.table = {}
- -- functions
- do
- -- base
- function util.base:factNumToStrFromBase(charsDict)
- local base = #charsDict + 1
- return function(num)
- local res = ''
- local iterations = 1 + math.max(0, math.floor( math.log(num, base)))
- for i = iterations, 1, -1 do -- going down number places
- local factor = base ^ (i - 1)
- local index = base - 1
- for largestDigit = base, 0, -1 do -- going down the base to 1
- local num2 = factor * largestDigit
- if num - num2 < 0 then continue end
- index = largestDigit
- num -= num2
- break
- end
- res ..= charsDict[index]
- end
- return res
- end
- end
- function util.base:factStrToNumFromBase(charsDict)
- local base = #charsDict + 1
- local flippedCharacters = util.table:flipTable(charsDict)
- return function(str)
- assert(type(str) == 'string')
- local res = 0
- for index = 1, #str do -- iterate each letter
- local letter = str:sub(index, index)
- local num = flippedCharacters[letter] * base ^ (#str - index)
- res += num
- end
- return res
- end
- end
- -- Instance
- function util.Instance:new(className,props)
- local i = Instance.new(className)
- util.table:changeIndex(i, props, true)
- return i
- end
- function util.Instance:create(className)
- return function(props)
- return util.Instance:new(className,props)
- end
- end
- -- scientific notation
- function util.scientificNotation:getScifiNotatSet(num, customBase)
- customBase = customBase or 10
- assert(
- type(num) == 'number' and
- type(customBase) == 'number'
- )
- local res = {
- exponent = 0;
- rational = num;
- sign = math.sign(num)
- }
- local absNum = math.abs(num)
- if absNum ~= 0 then
- -- check out function later
- -- get how many digits - 1 they have
- local logN = math.floor(math.log(absNum, customBase))
- res.exponent = logN
- local rational = absNum * customBase ^ (-logN)
- res.rational = rational
- else
- res.sign = 1
- end
- return res
- end
- function util.scientificNotation:factNumToSciFiNotif(charsDict)
- local base = #charsDict + 1
- local numToStrConvert = util.base:factNumToStrFromBase(charsDict)
- return function(num)
- assert(type(num) == 'number')
- local res = {
- sign = charsDict[0];
- rational = '';
- exponent = charsDict[1]
- }
- -- get set
- local scifiNotifSet = util.scientificNotation:getScifiNotatSet(num, base)
- -- set results
- res.sign = charsDict[scifiNotifSet.sign == -1 and 0 or 1]
- res.exponent = numToStrConvert(scifiNotifSet.exponent + 150)
- -- set rational
- local rationalStr = ''
- local rational = scifiNotifSet.rational
- local str = tostring(rational):gsub('%.','')
- local digit = 0
- -- repeat until we eventually reach near zero, note, we round instead of checking exactly because
- -- there is uncountably infinite amount of numbers between 0 and 1 and hitting 0 in a sea of
- -- numbers is touch. Also we offset the rational with 1e12 to try to preserve the decimals.
- while math.round(rational * 1e12) ~= 0 do
- -- go down the vase
- for index = 0, base do
- local num1 = index * (base ^ -digit)
- local num2 = rational - num1
- if math.sign(num2) == -1 then
- rational -= ((index - 1) * (base ^ -digit))
- rationalStr ..= charsDict[index - 1]
- break
- end
- end
- digit += 1
- end
- res.rational = rationalStr
- -- return
- return res
- end
- end
- function util.scientificNotation:factSciFiNotifToNum(charsDict)
- local base = #charsDict + 1
- local strToNumBaseConvert = util.base:factStrToNumFromBase(charsDict)
- local flippedDict = util.table:flipTable(charsDict)
- return function(scifiNotif)
- assert(
- type(scifiNotif) == 'table' and
- type(scifiNotif.exponent) == 'string' and
- type(scifiNotif.rational) == 'string' and
- type(scifiNotif.sign) == 'string'
- )
- local res = 0
- -- exponent and sign
- local exponent = strToNumBaseConvert(scifiNotif.exponent) - 150
- local sign = flippedDict[scifiNotif.sign] == 0 and -1 or 1
- -- give the int and mantissa their own section
- local rationalStr = scifiNotif.rational
- local intStr = rationalStr:sub(1,1)
- local mantissaStr = rationalStr:sub(2)
- local number = 0
- for exponentB = 0, #rationalStr - 1 do
- local charIndex = exponentB + 1
- local char = rationalStr:sub(charIndex, charIndex)
- number += flippedDict[char] * (base ^ -exponentB)
- end
- -- construct result
- res = sign * (number * (base ^ exponent))
- return res
- end
- end
- -- string
- function util.string:splitChars(str)
- str = tostring(str)
- return str:split''
- end
- -- table
- function util.table:changeIndex(tabFrom, newChanges, printErrors)
- assert(
- (type(tabFrom) == 'table' or typeof(tabFrom) == 'Instance') and type(newChanges) == 'table'
- )
- for prop, newVal in next, newChanges do
- local s, e = pcall(function()
- tabFrom[prop] = newVal
- end)
- if not s and printErrors then
- warn('Util Error: ', e, tabFrom, prop, newVal)
- end
- end
- end
- function util.table:flipTable(t)
- local t2 = {}
- for i, v in next, t do
- t2[v] = i
- end
- return t2
- end
- -- misc
- function util.misc:arrayToCharDict(arr)
- local dict = {}
- for i, v in next, arr do
- dict[i - 1] = v
- end
- return dict
- end
- end
- -- return
- return util
Add Comment
Please, Sign In to add comment