Math Common
scripts/vscripts/alyxlib/math/common.lua
Methods
sign
Gets the sign of a number.
Parameters
x
number
The input number
Returns
- -1|0|1
Sign of the number: -1 for negative, 1 for positive, 0 if zero
trunc
Truncates a number to a given number of decimal places.
Parameters
number
number
The number to truncateplaces(optional)
integer
Decimal places to truncate to (default: 0)
Returns
- number
The truncated number
round
Rounds a number to a given number of decimal places.
Parameters
number
number
The number to be roundeddecimals(optional)
integer
Decimal places to round to (default: 0)
Returns
- number
The rounded number
isclose
Checks if two numbers are close to each other within a specified tolerance.
Examples:
-
Relative Tolerance (
rel_tol): -
Absolute Tolerance (
abs_tol):
Parameters
a
number
The first number to compareb
number
The second number to comparerel_tol(optional)
number
Defines the maximum allowed relative difference betweenaandbas a percentage of the larger of the two valuesabs_tol(optional)
number
Defines the maximum allowed fixed difference betweenaandb, regardless of their magnitudes
Returns
- boolean
true if the numbers are considered close based on the specified tolerances, false otherwise
has_frac
Checks if a given number has a fractional part (decimal part).
Parameters
number
number
The number to check
Returns
- boolean
true if the number has a fractional part, false otherwise.
get_frac
Returns the fractional part of a number.
Parameters
number
number
The number to get the fractional part of
Returns
- number
The fractional part of the number