Math
The Math module provides a comprehensive set of mathematical functions and utilities for FiveM resource development. It extends the standard Lua math library with additional functionality.
round
Rounds a number to the specified number of decimal places.
B2Lib.Math.round(num, decimals)Parameters:
num(number): The number to rounddecimals(number, optional): Number of decimal places (default: 0)
Returns: number - The rounded number
Example:
local rounded = B2Lib.Math.round(3.14159, 2) -- Returns 3.14
local wholeNumber = B2Lib.Math.round(3.7) -- Returns 4clamp
Clamps a value between minimum and maximum bounds.
B2Lib.Math.clamp(value, min, max)Parameters:
value(number): The value to clampmin(number): The minimum allowed valuemax(number): The maximum allowed value
Returns: number - The clamped value
Example:
lerp
Performs linear interpolation between two values.
Parameters:
a(number): The start valueb(number): The end valuet(number): The interpolation factor (0.0 = a, 1.0 = b)
Returns: number - The interpolated value
Example:
distance2D
Calculates the Euclidean distance between two 2D points.
Parameters:
x1(number): X coordinate of the first pointy1(number): Y coordinate of the first pointx2(number): X coordinate of the second pointy2(number): Y coordinate of the second point
Returns: number - The distance between the two points
Example:
distance3D
Calculates the Euclidean distance between two 3D points or vectors.
Parameters:
pos1(vector3|table): The first positionpos2(vector3|table): The second position
Returns: number - The distance between the two positions
Example:
normalize
Maps a value from one range to another range.
Parameters:
value(number): The value to normalizemin(number): The current range minimummax(number): The current range maximumnewMin(number): The target range minimumnewMax(number): The target range maximum
Returns: number - The mapped value in the new range
Example:
toRadians
Converts degrees to radians.
Parameters:
degrees(number): The angle in degrees
Returns: number - The angle in radians
Example:
toDegrees
Converts radians to degrees.
Parameters:
radians(number): The angle in radians
Returns: number - The angle in degrees
Example:
getAngle
Calculates the angle between two 2D points.
Parameters:
x1(number): X coordinate of the first pointy1(number): Y coordinate of the first pointx2(number): X coordinate of the second pointy2(number): Y coordinate of the second point
Returns: number - The angle in radians
Example:
randomFloat
Generates a random floating-point number between min and max.
Parameters:
min(number): The minimum value (inclusive)max(number): The maximum value (exclusive)
Returns: number - A random float in the specified range
Example:
randomInt
Generates a random integer between min and max.
Parameters:
min(number): The minimum value (inclusive)max(number): The maximum value (inclusive)
Returns: number - A random integer in the specified range
Example:
isEven
Checks if a number is even.
Parameters:
num(number): The number to check
Returns: boolean - True if the number is even, false otherwise
isOdd
Checks if a number is odd.
Parameters:
num(number): The number to check
Returns: boolean - True if the number is odd, false otherwise
isPrime
Checks if a number is prime.
Parameters:
num(number): The number to check
Returns: boolean - True if the number is prime, false otherwise
factorial
Calculates the factorial of a number.
Parameters:
num(number): The number to calculate factorial for
Returns: number - The factorial of the number
gcd
Calculates the greatest common divisor of two numbers.
Parameters:
a(number): First numberb(number): Second number
Returns: number - The greatest common divisor
lcm
Calculates the least common multiple of two numbers.
Parameters:
a(number): First numberb(number): Second number
Returns: number - The least common multiple
formatNumber
Formats a number with thousands separators.
Parameters:
num(number): The number to formatseparator(string, optional): The separator character (default: ",")
Returns: string - The formatted number
Example:
formatCurrency
Formats a number as currency.
Parameters:
amount(number): The amount to formatsymbol(string, optional): The currency symbol (default: "$")decimals(number, optional): Number of decimal places (default: 2)
Returns: string - The formatted currency
Example:
percentage
Calculates the percentage of a value relative to a total.
Parameters:
value(number): The valuetotal(number): The total
Returns: number - The percentage (0-100)
Example:
pointInCircle
Checks if a point is inside a circle.
Parameters:
pointX(number): X coordinate of the pointpointY(number): Y coordinate of the pointcircleX(number): X coordinate of the circle centercircleY(number): Y coordinate of the circle centerradius(number): Radius of the circle
Returns: boolean - True if the point is inside the circle
pointInRectangle
Checks if a point is inside a rectangle.
Parameters:
pointX(number): X coordinate of the pointpointY(number): Y coordinate of the pointrectX(number): X coordinate of the rectangle (top-left)rectY(number): Y coordinate of the rectangle (top-left)width(number): Width of the rectangleheight(number): Height of the rectangle
Returns: boolean - True if the point is inside the rectangle
rgbToHex
Converts RGB color values to hexadecimal.
Parameters:
r(number): Red component (0-255)g(number): Green component (0-255)b(number): Blue component (0-255)
Returns: string - The hexadecimal color code
Example:
hexToRgb
Converts hexadecimal color to RGB values.
Parameters:
hex(string): The hexadecimal color code
Returns: table - RGB values {r, g, b}
Example:
rgbToHsv
Converts RGB color values to HSV.
Parameters:
r(number): Red component (0-255)g(number): Green component (0-255)b(number): Blue component (0-255)
Returns: table - HSV values {h, s, v}
hsvToRgb
Converts HSV color values to RGB.
Parameters:
h(number): Hue (0-360)s(number): Saturation (0-100)v(number): Value (0-100)
Returns: table - RGB values {r, g, b}
Configuration
Configure the math system in config.lua:
Troubleshooting
Precision Issues
Use appropriate decimal places for rounding
Be aware of floating-point precision limitations
Use integer operations when possible for exact results
Consider using string formatting for display purposes
Performance Considerations
Cache frequently calculated values
Use simpler functions when precision isn't critical
Avoid unnecessary conversions between number types
Use built-in math functions when available
Invalid Input Handling
Validate input parameters before calculations
Check for division by zero in custom functions
Ensure angles are in expected ranges
Verify color values are within valid ranges (0-255 for RGB)
Common Mistakes
Mixing degrees and radians in calculations
Not handling edge cases in geometric functions
Incorrect range mapping with normalize function
Using wrong coordinate systems for distance calculations
Last updated