Common Functions
Basic Functions
| Function Name | Function 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 Name | Function Description | Example | Example Result |
|---|---|---|---|
| abs | Returns the absolute value of a number | math.abs(-15) | 15 |
| acos | Returns the arccosine value (in radians) | math.acos(0.5) | 1.04719755 |
| asin | Returns the arcsine value (in radians) | math.asin(0.5) | 0.52359877 |
| atan2 | Returns the arctangent value of x/y (in radians) | math.atan2(90.0, 45.0) | 1.10714871 |
| atan | Returns the arctangent value (in radians) | math.atan(0.5) | 0.4636476095 |
| ceil | Returns the smallest integer greater than or equal to x | math.ceil(5.8) | 6 |
| cosh | Returns the hyperbolic cosine value | math.cosh(0.5) | 1.276259652 |
| cos | Returns the cosine value (in radians) | math.cos(0.5) | 0.87758256 |
| deg | Converts radians to degrees | math.deg(math.pi) | 180 |
| exp | Calculates 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) |
| floor | Returns the largest integer less than or equal to x | math.floor(5.6) | 5 |
| fmod (mod) | Performs modulo operation (returns the remainder of x/y) | math.mod(14, 5) | 4 |
| frexp | Decomposes 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⁴) |
| ldexp | Calculates value * 2ⁿ | math.ldexp(10.0, 3) | 80 (10 × 2³ = 80) |
| log10 | Calculates the base-10 logarithm of a number | math.log10(100) | 2 |
| log | Calculates the natural logarithm (base e) of a number | math.log(2.71) | 0.9969 |
| max | Returns the maximum value among the parameters | math.max(2.71, 100, -98, 23) | 100 |
| min | Returns the minimum value among the parameters | math.min(2.71, 100, -98, 23) | -98 |
| modf | Splits a number into its integer and fractional parts | math.modf(15.98) | 15, 0.98 (Note: The original example result "15 98" was incorrect; it should be the fractional part 0.98) |
| pow | Calculates x raised to the power of y | math.pow(2, 5) | 32 (2⁵ = 32) |
| rad | Converts degrees to radians | math.rad(180) | 3.141592654 (≈ π) |
| random | Generates 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 |
| randomseed | Sets 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) |
| sinh | Returns the hyperbolic sine value | math.sinh(0.5) | 0.521095305 (Note: The original example result "0.5" was approximate; the precise value is ~0.5211) |
| sin | Returns the sine value (in radians) | math.sin(math.rad(30)) | 0.5 (Note: The original example result "15" was incorrect; sin(30°) = 0.5) |
| sqrt | Calculates the square root of a number | math.sqrt(16) | 4 |
| tanh | Returns the hyperbolic tangent value | math.tanh(0.5) | 0.46211715 |
| tan | Returns the tangent value (in radians) | math.tan(0.5) | 0.5463024 |
String Functions
| Serial Number | Method & Description |
|---|---|
| 1 | string.upper(argument):Converts all characters in the string to uppercase. |
| 2 | string.lower(argument):Converts all characters in the string to lowercase. |
| 3 | string.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). |
| 4 | string.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) |
| 5 | string.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 |
| 6 | string.reverse(arg):Reverses the string. Example: string.reverse("Lua") → Result: "auL" |
| 7 | string.format(...):Returns a formatted string (similar to printf in C).Example: string.format("the value is:%d",4) → Result: "the value is:4" |
| 8 | string.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') |
| 9 | string.len(arg):Calculates the length of the string. Example: string.len("abc") → Result: 3 |
| 10 | string.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" |
| 12 | string.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) |
| 13 | string.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) |
