Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Exponent Calculator Script with Abbreviations
- -- Function to calculate exponentiation
- function exponentiation(base, exponent)
- return base ^ exponent
- end
- -- Function to get the abbreviated form of a number
- function getAbbreviation(value)
- if value == math.huge or value == -math.huge then
- return value, ""
- end
- local abbreviations = {"", "K", "M", "B", "T", "Q", "Qn", "Sx", "Sp", "Oc", "Nn", "Dc",
- "Td", "Qad", "Qid", "Sxd", "Spd", "Ocd", "Nvd", "Vg", "Uvg", "Dvg",
- "Tvg", "Qavg", "Qivg", "Sxvg", "Spvg", "Ocvg", "Nnvg", "Tg", "Ut",
- "Dtg", "Tttg", "Qatg", "Qitg", "Sxtg", "Sptg", "Octg", "Ntrg", "Qag",
- "Uqag", "Dqag", "Tqag", "Qaqg", "Qiqg", "Sxqg", "Spqg", "Ocqg", "Nnqg",
- "Qig", "Uqig", "Dqig", "Tqig", "Qaqig", "Qiqig", "Sxqig", "Spqig", "Ocqig", "Nnqig",
- "Sg", "Usg", "Dsg", "Tsg", "Qasg", "Qisg", "Sxsg", "Spsg", "Ocsg", "Nnsg",
- "St", "Ust", "Dst", "Tst", "Qast", "Qist", "Sxst", "Spst", "Ocst", "Nnst",
- "Og", "Uog", "Dog", "Tog", "Qaog", "Qiog", "Sxog", "Spog", "Ocog", "Nnog",
- "Nog", "Unog", "Dnog", "Tnog", "Qanog", "Qinog", "Sxnog", "Spnog", "Ocnog", "Nnnog",
- "C", "Uc", "Dc", "Tc"}
- local idx = 1
- while value >= 1000 and idx < #abbreviations do
- value = value / 1000
- idx = idx + 1
- end
- return value, abbreviations[idx]
- end
- -- Function to round to the nearest hundredths
- function roundToHundredths(num)
- if num == nil then
- return nil
- end
- return math.floor(num * 100 + 0.5) / 100
- end
- -- Function to convert a number to scientific notation
- function toScientificNotation(num)
- local exponent = math.floor(math.log10(math.abs(num)))
- local mantissa = num / (10 ^ exponent)
- return mantissa, exponent
- end
- -- Get the base and exponent values from the script's children
- local baseValue = script:WaitForChild("BaseValue")
- local exponentValue = script:WaitForChild("ExponentValue")
- -- Store the previous values
- local prevBaseValue = baseValue.Value
- local prevExponentValue = exponentValue.Value
- -- Infinite loop to check values in real time
- while true do
- -- Check if baseValue.Value and exponentValue.Value are not nil
- if baseValue.Value ~= nil and exponentValue.Value ~= nil then
- -- Only calculate if the values have changed
- if baseValue.Value ~= prevBaseValue or exponentValue.Value ~= prevExponentValue then
- -- Calculate exponentiation
- local resultValue = exponentiation(baseValue.Value, exponentValue.Value)
- -- Check for infinity
- if resultValue == math.huge or resultValue == -math.huge then
- print("Syntax Failure: Num Too Large")
- else
- -- Get abbreviated form
- local abbreviatedValue, abbreviation = getAbbreviation(resultValue)
- -- Round the abbreviated form to the nearest hundredths
- abbreviatedValue = roundToHundredths(abbreviatedValue)
- -- Convert the result value to scientific notation
- local mantissa, exponent = toScientificNotation(resultValue)
- local scientificNotation = string.format("%.2fe%d", mantissa, exponent)
- -- Check if any of the variables are nil before printing
- if scientificNotation ~= nil and abbreviatedValue ~= nil and abbreviation ~= nil then
- -- Print results
- print("Base: " .. baseValue.Value)
- print("Exponent: " .. exponentValue.Value)
- print("Exponentiation Result: " .. scientificNotation .. " (" .. abbreviatedValue .. abbreviation .. ")")
- end
- end
- -- Update the previous values
- prevBaseValue = baseValue.Value
- prevExponentValue = exponentValue.Value
- end
- end
- -- Sleep for 1 second before checking again
- wait()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement