Skip to content

Common Functions

Basic Functions

Function NameFunction Description & Example
type(v)Used to judge the type of v, returning the string "nil", "number", "string", "boolean", "table", "function", "thread", or "userdata".
tonumber(e [,base])Converts a string to a number. Converts e (must be a number or a string convertible to a number) to a decimal number. base specifies the base (2-36), defaulting to 10.
Example: tonumber('10') → Result: 10
tostring(var)Converts a variable to a string. Converts any type of e to a string in an appropriate format. If the metatable of e has a __tostring function, it will be called with e as the parameter, and the return value will be the result.
Example: tostring(math.asin(0.5)) → Result: "0.52359877"
isboolean(var)Checks if the variable is of boolean type.
isnil(var)Checks if the variable is nil.
isstring(var)Checks if the variable is of string type.
isinteger(var)Checks if the variable is of integer type.

Mathematical Functions

Function NameFunction DescriptionExampleExample Result
absReturns the absolute value of a numbermath.abs(-15)15
acosReturns the arccosine value (in radians)math.acos(0.5)1.04719755
asinReturns the arcsine value (in radians)math.asin(0.5)0.52359877
atan2Returns the arctangent value of x/y (in radians)math.atan2(90.0, 45.0)1.10714871
atanReturns the arctangent value (in radians)math.atan(0.5)0.4636476095
ceilReturns the smallest integer greater than or equal to xmath.ceil(5.8)6
coshReturns the hyperbolic cosine valuemath.cosh(0.5)1.276259652
cosReturns the cosine value (in radians)math.cos(0.5)0.87758256
degConverts radians to degreesmath.deg(math.pi)180
expCalculates e raised to the power of x (where e is the base of natural logarithm)math.exp(2)7.389056096 (Note: The original example result was incorrect; e²≈7.389)
floorReturns the largest integer less than or equal to xmath.floor(5.6)5
fmod (mod)Performs modulo operation (returns the remainder of x/y)math.mod(14, 5)4
frexpDecomposes the double-precision number val into a mantissa (x) and an exponent n (base 2), i.e., val = x * 2ⁿmath.frexp(10.0)0.625, 4 (Since 10 = 0.625 × 2⁴)
ldexpCalculates value * 2ⁿmath.ldexp(10.0, 3)80 (10 × 2³ = 80)
log10Calculates the base-10 logarithm of a numbermath.log10(100)2
logCalculates the natural logarithm (base e) of a numbermath.log(2.71)0.9969
maxReturns the maximum value among the parametersmath.max(2.71, 100, -98, 23)100
minReturns the minimum value among the parametersmath.min(2.71, 100, -98, 23)-98
modfSplits a number into its integer and fractional partsmath.modf(15.98)15, 0.98 (Note: The original example result "15 98" was incorrect; it should be the fractional part 0.98)
powCalculates x raised to the power of ymath.pow(2, 5)32 (2⁵ = 32)
radConverts degrees to radiansmath.rad(180)3.141592654 (≈ π)
randomGenerates a random number. To use it, you must first set a random seed with math.randomseed().
- math.random(): Returns a float between 0 and 1.
- math.random(n): Returns an integer between 1 and n.
- math.random(m, n): Returns an integer between m and n.
math.random(1, 100)A random integer between 1 and 100
randomseedSets the random number seed (to ensure different random sequences)math.randomseed(os.time())(Sets the seed based on the current time; no direct return value)
sinhReturns the hyperbolic sine valuemath.sinh(0.5)0.521095305 (Note: The original example result "0.5" was approximate; the precise value is ~0.5211)
sinReturns the sine value (in radians)math.sin(math.rad(30))0.5 (Note: The original example result "15" was incorrect; sin(30°) = 0.5)
sqrtCalculates the square root of a numbermath.sqrt(16)4
tanhReturns the hyperbolic tangent valuemath.tanh(0.5)0.46211715
tanReturns the tangent value (in radians)math.tan(0.5)0.5463024

String Functions

Serial NumberMethod & Description
1string.upper(argument):
Converts all characters in the string to uppercase.
2string.lower(argument):
Converts all characters in the string to lowercase.
3string.sub(s, i [, j]):
Extracts a substring from s. i is the start position, j is the end position (defaults to -1, meaning the last character).
4string.gsub(mainString, findString, replaceString, num):
Replaces substrings in a string.
- mainString: The target string to process.
- findString: The substring to be replaced.
- replaceString: The substring to use for replacement.
- num: The number of replacements (optional; defaults to replacing all occurrences).
Example: string.gsub("aaaa","a","z",3) → Result: "zzza", 3 (returns the new string and the number of replacements)
5string.find(str, substr, [init, [plain]]):
Searches for substr in the target string str.
- Returns the start and end indices of the matched substring if found; returns nil otherwise.
- init: The start position of the search (defaults to 1; can be negative, meaning counting from the end).
- plain: Whether to use simple mode (defaults to false; true for exact substring search, false for regex matching).
Example: string.find("Hello Lua user", "Lua", 1) → Result: 7, 9
6string.reverse(arg):
Reverses the string.
Example: string.reverse("Lua") → Result: "auL"
7string.format(...):
Returns a formatted string (similar to printf in C).
Example: string.format("the value is:%d",4) → Result: "the value is:4"
8string.char(arg) and string.byte(arg[, int]):
- string.char: Converts integer values to characters and concatenates them.
- string.byte: Converts a character to its integer ASCII value (specify the character index with int; defaults to the first character).
Examples:
string.char(97,98,99,100) → Result: "abcd"
string.byte("ABCD",4) → Result: 68 (ASCII value of 'D')
string.byte("ABCD") → Result: 65 (ASCII value of 'A')
9string.len(arg):
Calculates the length of the string.
Example: string.len("abc") → Result: 3
10string.rep(string, n):
Returns n copies of the string concatenated together.
Example: string.rep("abcd",2) → Result: "abcdabcd"
11.. (Concatenation Operator):
Concatenates two strings.
Example: print("www.ai-v".."ision.com") → Result: "www.ai-vision.com"
12string.gmatch(str, pattern):
Returns an iterator function. Each call to this function returns the next substring in str that matches pattern. Returns nil if no matches are found.
Example:
for word in string.gmatch("Hello ai vision", "%a+") do print(word) end
Result:
"Hello"
"ai"
"vision" (Note: %a+ matches one or more letters)
13string.match(str, pattern, init):
Searches for the first match of pattern in str.
- init: Optional start position (defaults to 1).
- Returns all capture results if capture markers are set; returns the entire matched string if no captures are set; returns nil if no match is found.
Examples:
string.match("I have 2 questions for you.", "%d+ %a+") → Result: "2 questions"
string.format("%d, %q", string.match("I have 2 questions for you.", "(%d+) (%a+)")) → Result: "2, "questions"" (captures the number and the word separately)

AI-Vision, Making 3D Measurement Easier