The math.type()
function returns the type of a number. It determines whether a given value is an integer, a float (real number), or not a number at all.
Syntax
math.type(x)
Parameters
- x: A number or value whose type you want to determine.
Return Value
- Returns
"integer"
ifx
is an integer. - Returns
"float"
ifx
is a float (real number). - Returns
nil
ifx
is not a number (e.g., NaN, nil, or non-numeric values).
Description
The math.type
function checks the provided value x
and returns a string indicating its type. If x
is a valid integer, it returns "integer"
. If x
is a valid float, it returns "float"
. If x
is not a valid number, it returns nil
.
Examples
Basic usage of math.type()
print(math.type(42)) -- Output: "integer"
print(math.type(3.14)) -- Output: "float"
print(math.type(-0.5)) -- Output: "float"
print(math.type(0)) -- Output: "integer"
print(math.type(5.0)) -- Output: "float"
print(math.type(math.huge)) -- Output: "float"
print(math.type("42")) -- Output: nil (string is not a number)
print(math.type(nil)) -- Output: nil
print(math.type(math.nan)) -- Output: nil (NaN is not a valid number)
See also
math.tointeger()
math.isinteger()