Advertisement
1m1m0

Exponentiation Calculator

Feb 13th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 KB | Software | 0 0
  1. -- Exponent Calculator Script with Abbreviations
  2.  
  3. -- Function to calculate exponentiation
  4. function exponentiation(base, exponent)
  5.     return base ^ exponent
  6. end
  7.  
  8. -- Function to get the abbreviated form of a number
  9. function getAbbreviation(value)
  10.     if value == math.huge or value == -math.huge then
  11.         return value, ""
  12.     end
  13.  
  14.     local abbreviations = {"", "K", "M", "B", "T", "Q", "Qn", "Sx", "Sp", "Oc", "Nn", "Dc",
  15.         "Td", "Qad", "Qid", "Sxd", "Spd", "Ocd", "Nvd", "Vg", "Uvg", "Dvg",
  16.         "Tvg", "Qavg", "Qivg", "Sxvg", "Spvg", "Ocvg", "Nnvg", "Tg", "Ut",
  17.         "Dtg", "Tttg", "Qatg", "Qitg", "Sxtg", "Sptg", "Octg", "Ntrg", "Qag",
  18.         "Uqag", "Dqag", "Tqag", "Qaqg", "Qiqg", "Sxqg", "Spqg", "Ocqg", "Nnqg",
  19.         "Qig", "Uqig", "Dqig", "Tqig", "Qaqig", "Qiqig", "Sxqig", "Spqig", "Ocqig", "Nnqig",
  20.         "Sg", "Usg", "Dsg", "Tsg", "Qasg", "Qisg", "Sxsg", "Spsg", "Ocsg", "Nnsg",
  21.         "St", "Ust", "Dst", "Tst", "Qast", "Qist", "Sxst", "Spst", "Ocst", "Nnst",
  22.         "Og", "Uog", "Dog", "Tog", "Qaog", "Qiog", "Sxog", "Spog", "Ocog", "Nnog",
  23.         "Nog", "Unog", "Dnog", "Tnog", "Qanog", "Qinog", "Sxnog", "Spnog", "Ocnog", "Nnnog",
  24.         "C", "Uc", "Dc", "Tc"}
  25.  
  26.     local idx = 1
  27.  
  28.     while value >= 1000 and idx < #abbreviations do
  29.         value = value / 1000
  30.         idx = idx + 1
  31.     end
  32.  
  33.     return value, abbreviations[idx]
  34. end
  35.  
  36. -- Function to round to the nearest hundredths
  37. function roundToHundredths(num)
  38.     if num == nil then
  39.         return nil
  40.     end
  41.     return math.floor(num * 100 + 0.5) / 100
  42. end
  43.  
  44. -- Function to convert a number to scientific notation
  45. function toScientificNotation(num)
  46.     local exponent = math.floor(math.log10(math.abs(num)))
  47.     local mantissa = num / (10 ^ exponent)
  48.     return mantissa, exponent
  49. end
  50.  
  51. -- Get the base and exponent values from the script's children
  52. local baseValue = script:WaitForChild("BaseValue")
  53. local exponentValue = script:WaitForChild("ExponentValue")
  54.  
  55. -- Store the previous values
  56. local prevBaseValue = baseValue.Value
  57. local prevExponentValue = exponentValue.Value
  58.  
  59. -- Infinite loop to check values in real time
  60. while true do
  61.     -- Check if baseValue.Value and exponentValue.Value are not nil
  62.     if baseValue.Value ~= nil and exponentValue.Value ~= nil then
  63.         -- Only calculate if the values have changed
  64.         if baseValue.Value ~= prevBaseValue or exponentValue.Value ~= prevExponentValue then
  65.             -- Calculate exponentiation
  66.             local resultValue = exponentiation(baseValue.Value, exponentValue.Value)
  67.  
  68.             -- Check for infinity
  69.             if resultValue == math.huge or resultValue == -math.huge then
  70.                 print("Syntax Failure: Num Too Large")
  71.             else
  72.                 -- Get abbreviated form
  73.                 local abbreviatedValue, abbreviation = getAbbreviation(resultValue)
  74.  
  75.                 -- Round the abbreviated form to the nearest hundredths
  76.                 abbreviatedValue = roundToHundredths(abbreviatedValue)
  77.  
  78.                 -- Convert the result value to scientific notation
  79.                 local mantissa, exponent = toScientificNotation(resultValue)
  80.                 local scientificNotation = string.format("%.2fe%d", mantissa, exponent)
  81.  
  82.                 -- Check if any of the variables are nil before printing
  83.                 if scientificNotation ~= nil and abbreviatedValue ~= nil and abbreviation ~= nil then
  84.                     -- Print results
  85.                     print("Base: " .. baseValue.Value)
  86.                     print("Exponent: " .. exponentValue.Value)
  87.                     print("Exponentiation Result: " .. scientificNotation .. " (" .. abbreviatedValue .. abbreviation .. ")")
  88.                 end
  89.             end
  90.  
  91.             -- Update the previous values
  92.             prevBaseValue = baseValue.Value
  93.             prevExponentValue = exponentValue.Value
  94.         end
  95.     end
  96.  
  97.     -- Sleep for 1 second before checking again
  98.     wait()
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement