平方根

基本类型 / 数字 / 数学运算

local number = 100
local result = math.sqrt(number)
-- result = 10.0

-- The same as the power 0.5
local half_power = number ^ 0.5
-- half_power = 10.0

-- Integer root, rounded down
local root = math.floor(math.sqrt(99))
-- root = 9

-- sqrt(-1) is not an error, it is nan;
-- nan is the only value not equal to itself
local negative = math.sqrt(-1)
local is_nan = negative ~= negative
-- is_nan = true

print("result = " .. result)
print("half_power = " .. half_power)
print("root = " .. root)
print("is_nan = " .. tostring(is_nan))