1,027
edits
(Add magical autoround function) |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
} | } | ||
local function sigfig(x, | local function sigfig(x, y) | ||
local x_sign = x < 0 and -1 or 1 | local x_sign = x < 0 and -1 or 1 | ||
local x = math.abs(x) | local x = math.abs(x) | ||
local n = math.floor(math.log10(x)) + 1 - | local n = math.floor(math.log10(x)) + 1 - y | ||
return x_sign * math.pow(10, n) * p.round2(x / math.pow(10, n), 0) | return x_sign * math.pow(10, n) * p.round2(x / math.pow(10, n), 0) | ||
end | end | ||
Line 40: | Line 40: | ||
return _x | return _x | ||
end | end | ||
--- Formats a number by inserting commas as thousand separators. | --- Formats a number by inserting commas as thousand separators. | ||
-- @param number (number or string) The number to format. | -- @param number (number or string) The number to format. | ||
Line 46: | Line 47: | ||
if tonumber(number) == nil then | if tonumber(number) == nil then | ||
return number | return number | ||
end | |||
while true do | -- Find out of the number is using scientific notation. | ||
-- If it is, convert it to a string and remove the trailing zeroes. | |||
local result = tostring(number) | |||
if result:find("[eE]") ~= nil then | |||
result = string.format("%.20f", number) | |||
result = result:gsub("%.?0*$", "") | |||
end | |||
while true do | |||
-- Format in blocks of 3 digits at a time until formatting is complete | |||
local k | |||
result, k = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2') | |||
if k == 0 then | |||
break | |||
end | end | ||
end | end | ||
return result | |||
end | end | ||
Line 224: | Line 232: | ||
end | end | ||
-- | -- Returns the probability of getting at least one drop, provided | ||
function p. | -- the drop rate and the current amount of drops. | ||
function p.getDropProbability(dropRate, totalDrops) | |||
dropRate = p.clamp(dropRate, 0, 1) | |||
return 1 - (1- dropRate) ^ 500 | |||
end | end | ||
-- | -- Returns the amount of drops required for a certain drop probability. | ||
function p.getDropsForProbability(dropRate, probability) | |||
dropRate = p.clamp(dropRate, 0, 1) | |||
function p. | probability = p.clamp(probability, 0, 1) | ||
return math.log(1 - probability) / math.log(1 - dropRate) | |||
end | end | ||
return p | return p |
edits