Math
The SD.Math
module provides a suite of mathematical functions designed to assist in various calculations commonly required.
Math.WeightedChance
Purpose: Selects a key from a table based on weighted probability. Parameters:
tbl
(table
): A table where each entry is a structure containing the item and its associated chance weight.
Returns:
(
any
): The key of the chosen item based on weighted probability.
Usage Example:
local choices = {apple = {chance = 50}, banana = {chance = 30}, cherry = {chance = 20}}
local result = SD.Math.WeightedChance(choices)
print(result) -- Output: 'apple', 'banana', or 'cherry' based on their weights
Math.Clamp
Purpose: Clamps a number within a specified range. Parameters:
num
(number
): The number to clamp.min
(number
): The minimum allowable value.max
(number
): The maximum allowable value.
Returns:
(
number
): The clamped value.
Usage Example:
local clampedValue = SD.Math.Clamp(15, 1, 10)
print(clampedValue) -- Output: 10
Math.Lerp
Purpose: Linearly interpolates between two values. Parameters:
from
(number
): The start value.to
(number
): The end value.alpha
(number
): The interpolation factor (0-1).
Returns:
(
number
): The interpolated value.
Usage Example:
local interpolated = SD.Math.Lerp(0, 100, 0.5)
print(interpolated) -- Output: 50
Math.Factorial
Purpose: Calculates the factorial of a number. Parameters:
n
(number
): The number to calculate the factorial of. Returns:(
number
): The factorial of the number.
Usage Example:
local fact = SD.Math.Factorial(5)
print(fact) -- Output: 120
Math.Round
Purpose: Rounds a number to a specified number of decimal places. Parameters:
num
(number
): The number to round.numDecimalPlaces
(number
): The number of decimal places to round to.
Returns:
(
number
): The rounded number.
Usage Example:
local rounded = SD.Math.Round(3.14159, 2)
print(rounded) -- Output: 3.14
Last updated