Module:Number: Difference between revisions

no edit summary
m (Use hundreds of a second instead of seconds for timespan conversion in attempts of better accuracy)
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 16: Line 16:
         d = 1e30
         d = 1e30
     }
     }
   
 
local function sigfig(x, y)
local x_sign = x < 0 and -1 or 1
local x = math.abs(x)
local n = math.floor(math.log10(x)) + 1 - y
return x_sign * math.pow(10, n) * p.round2(x / math.pow(10, n), 0)
end
 
-- Automatically rounds to 2 places from the significant figure.
-- Taken from RSWiki.
function p.autoround(x)
x = tonumber(x) or 0
local _x
if x == 0 then
_x = 0
elseif math.abs(x) < 0.1 then
_x = sigfig(x, 2)
elseif math.abs(x) > 999 then
_x = p.round2(x, 0)
else
_x = p.round2(x, 2)
end
return _x
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 23: Line 47:
if tonumber(number) == nil then
if tonumber(number) == nil then
return number
return number
else
end
local result = number
while true do
-- Find out of the number is using scientific notation.
-- Format in blocks of 3 digits at a time until formatting is complete
-- If it is, convert it to a string and remove the trailing zeroes.
            local k
local result = tostring(number)
result, k = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2')
if result:find("[eE]") ~= nil then
if k == 0 then
result = string.format("%.20f", number)
break
result = result:gsub("%.?0*$", "")
end
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
return result
end
end
return result
end     
end     


Line 201: Line 232:
end
end


-- Creates a TimeSpan object from a total amount of seconds.
-- Returns the probability of getting at least one drop, provided
function p.secondsToTimeSpan(totalSeconds)
-- the drop rate and the current amount of drops.
return p.actionTimeToTimeSpan(totalSeconds * 100)
function p.getDropProbability(dropRate, totalDrops)
dropRate = p.clamp(dropRate, 0, 1)
return 1 - (1- dropRate) ^ 500
end
end


--- Creates a TimeSpan object from action time (hundreds of a second).
-- Returns the amount of drops required for a certain drop probability.
-- @param str (number) The amount of action time to convert to a TimeSpan
function p.getDropsForProbability(dropRate, probability)
-- @return (TimeSpan) A TimeSpan object containing the seconds, minutes, hours and days the input amount of action time amounts to.
dropRate = p.clamp(dropRate, 0, 1)
function p.actionTimeToTimeSpan(totalActionTime)
probability = p.clamp(probability, 0, 1)
    local days = math.floor(totalSeconds / 8640000)
    local remainder = totalSeconds % 8640000
return math.log(1 - probability) / math.log(1 - dropRate)
 
    local hours = math.floor(remainder / 360000)
    remainder = remainder % 360000
 
    local minutes = math.floor(remainder / 6000)
    remainder = remainder % 6000
   
    local seconds = math.floor(remainder / 100)
 
    return {
        days = days,
        hours = hours,
        minutes = minutes,
        seconds = seconds,
       
        totalDays = totalSeconds / 86400,
        totalHours = totalSeconds / 3600,
        totalMinutes = totalSeconds / 60,
        totalSeconds = totalSeconds
    }
end
end


return p
return p
1,027

edits