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:

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:

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:

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:

Last updated