Math Common
scripts/vscripts/alyxlib/math/common.lua
Methods
sign
Get the sign of a number.
Parameters
x
number
The input number.
Returns
- 1|0|-1
Returns 1 if the number is positive, -1 if the number is negative, or 0 if the number is zero.
trunc
Truncates a number to the specified number of decimal places.
Parameters
number
number
The input number.The input number.places(optional)
integer
The number of decimal places to keep.
Returns
- number
The input number truncated to the specified decimal places.
round
Rounds a number to the specified number of decimal places.
Parameters
number
number
The input number to be rounded.decimals(optional)
integer
The number of decimal places to round to. If not provided, the number will be rounded to the nearest whole number.
Returns
- number
The input number rounded to the specified decimal places or nearest whole 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 compare.b
number
The second number to compare.rel_tol(optional)
number
The relative tolerance (optional). Defines the maximum allowed relative difference betweenaandbas a percentage of the larger of the two values.abs_tol(optional)
number
The absolute tolerance (optional). Defines the maximum allowed fixed difference betweenaandb, regardless of their magnitudes.
Returns
- boolean
Returns true if the numbers are considered close based on the specified tolerances; otherwise, returns false.
has_frac
Checks if a given number has a fractional part (decimal part).
Parameters
number
number
The number to check for fractional part.
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.