The math.sqrt() function returns the square root of a specified number. You can also use the expression x^0.5 to compute this value.

Syntax

math.sqrt(x)

Parameters

  • x: A number greater than or equal to 0.

Return Value

Returns the square root of x, which is a nonnegative number. If x is less than 0, the function will return NaN.

Description

The math.sqrt function computes the square root of the given number x. Since sqrt is a static method of the math library, it is always used in the form math.sqrt() and not as a method of a math object.

Examples

Basic usage of math.sqrt()

print(math.sqrt(0))        -- Output: 0
print(math.sqrt(1))        -- Output: 1
print(math.sqrt(2))        -- Output: 1.414213562373095
print(math.sqrt(9))        -- Output: 3
print(math.sqrt(16))       -- Output: 4
print(math.sqrt(-1))       -- Output: NaN
print(math.sqrt(Infinity))  -- Output: Infinity

See also

  • math.pow(): Returns x raised to the power of y.
  • math.cbrt(): Returns the cube root of x.
  • math.exp(): Returns the value of e raised to the power of x.
  • math.log(): Returns the natural logarithm of x.

Was this page helpful?

Table of Contents