Mathematical functions are tools for performing various arithmetic and numerical operations. They support high-precision calculations and include both basic operations and more specialized ones. Let's take a closer look at them.
math.addAdds two numbers. Equivalent to the + operation.
Signature: math.add(num1 number, num2 number, precision int = 12) number
Arguments:
num1 — the first addend.num2 — the second operand.precision — calculation accuracy, number of digits after the decimal point. The default value is 12, the maximum is 14.Result: The sum of the numbers with the specified accuracy.
Example of use:
$a = math.add(1.5, 3.5) // $a equals 5
$a = 1.5 + 3.5 // same as the previous line
$a = math.add(1.000006, 2.1, 5) // $a equals 3.10001
math.subPerforms subtraction of two numbers. Equivalent to the - operation.
Signature: math.sub(num1 number, num2 number, precision int = 12) number
Arguments:
num1 — the minuend.num2 — the subtrahend.precision — calculation accuracy, number of digits after the decimal point. The default value is 12, the maximum is 14.Result: The difference between the numbers with the specified accuracy.
Example of use:
$a = math.sub(1.5, 3.5) // $a equals -2
$a = 1.5 - 3.5 // same as the previous line
$a = math.sub(2.100006, 1.1, 5) // $a equals 1.00001
math.mulMultiplies two numbers. Equivalent to the * operation.
Signature: math.mul(num1 number, num2 number, precision int = 12) number
Arguments:
num1 — the first factor.num2 — the second multiplier.precision — calculation precision, number of digits after the decimal point. The default value is 12, the maximum is 14.Result: The product of numbers with the specified precision.
Example of use:
$a = math.mul(1.5, 3.5) // $a equals 5.25
$a = 1.5 * 3.5 // same as the previous line
$a = math.mul(1.2345, 1.234, 5) // $a equals 1.52337
math.divPerforms division of two numbers. Equivalent to the / operation.
Signature: math.div(num1 number, num2 number, precision int = 12) number
Arguments:
num1 — the dividend.num2 — the divisor.precision — calculation accuracy, number of digits after the decimal point. The default value is 12, the maximum is 14.Result: The quotient of two numbers with the specified accuracy.
Example of use:
$a = math.div(1.5, 3.5) // $a equals 0.428571428571
$a = 1.5 / 3.5 // same as the previous line
$a = math.div(1, 3, 5) // $a equals 0.33333
math.idivPerforms integer division of two numbers. Equivalent to the \ operation.
Signature: math.idiv(num1 number, num2 number) int
Arguments:
num1 — the dividend.num2 — the divisor.Result: The integer part of the quotient.
Example of use:
$a = math.idiv(2.5, 0.3) // $a equals 8
$a = 2.5 \ 0.3 // same as the previous line
math.modCalculates the remainder of the division of two numbers. Equivalent to the % operation.
Signature: math.mod(num1 number, num2 number, precision int = 12) number
Arguments:
num1 — the dividend.num2 — the divisor.precision — calculation precision, number of digits after the decimal point. The default value is 12, the maximum is 14.Result: The remainder of the division.
Example of use:
$a = math.mod(3.5, 1.5) // $a equals 0.5
$a = 3.5 % 1.5 // same as the previous line
$a = math.mod(1/3, 2/7, 5) // $a equals 0.04762
math.powElevates a number to the specified power. Equivalent to the ** operation.
Signature: math.pow(base number, power number, precision int = 12) number
Arguments:
base — base.power — exponent.precision — calculation precision, number of digits after the decimal point. The default value is 12, the maximum is 14.Result: The number raised to the power.
Example of use:
$a = math.pow(1.5, 3.5) // $a equals 4.133513940947
$a = 1.5 ** 3.5 // same as the previous line
$a = math.pow(1.3, 7.1, 5) // $a equals 0.44166
math.sqrtExtracts the square root of a number. Returns an error if the number is negative.
Signature: math.sqrt(num number, precision int = 12) number
Arguments:
num — the number for which the root is to be extracted.precision — calculation precision, number of digits after the decimal point. The default value is 12, the maximum is 14.Result: Square root of a number.
Example of use:
$a = math.sqrt(3.14) // $a equals 1.772004514667
$a = math.sqrt(1.7, 5) // $a equals 0.30384
math.roundRounds a number to the specified number of decimal places.
Signature: math.round(num number, precision int) number
Arguments:
num — the number to round.precision — number of digits after the decimal point. Maximum is 14.Result: The rounded number.
Example:
$a = math.round(3.14159265358979323846264338327950288419716, 17) // $a равно 3.1415926535898
$a = math.round(3.14159265358979323846264338327950288419716, 2) // $a равно 3.14
$a = math.round(3.14159265358979323846264338327950288419716, 0) // $a равно 3
math.randGenerates a pseudorandom number in the specified range.
Signature: math.rand(min int, max int) int
Arguments:
min is the minimum value.max — the maximum value.Result: A random number in the range.
Usage Example:
$r = math.rand(-10, 10)
math.eq and math.neqDescription: Determines that the number a is equal to b with a given precision.
Signature: math.eq(a float, b float, epsilon float = 1e-12) bool
Arguments:
a is the first number to compare.Usage Example:
$equals = math.eq(0.1000000000001, 0.10000000000009) // $equals is true
$equals = math.eq(0.1000000000001, 0.10000000000009, 1e-15) // $equals is false
$equals = math.eq(100000000000.0001, 100000000000.00009) // $equals is true
$equals =math.eq(100000000000.0001, 100000000000.00009, 1e-20) // $equals is false
Description: math.neq checks if the numbers are not equal with a given precision.
Signature: math.neq(a float, b float, epsilon float = 1e-12) bool
Arguments:
a is the first number to compare.Usage Example:
$equals = math.neq(0.1000000000001, 0.10000000000009) // $equals is false
$equals = math.neq(0.1000000000001, 0.10000000000009, 1e-15) // $equals is true
$equals = math.neq(100000000000.0001, 100000000000.00009) // $equals is false
$equals = math.neq(100000000000.0001, 100000000000.00009, 1e-20) // $equals is true