Lua sqrt()

deenovita's avatar
Published Oct 28, 2023
Contribute to Docs

The math.sqrt() function returns the result of the square root operation on a numeric variable. It takes a positive number as an argument value to be passed into the function.

Note: Results are dependent on the compiler. There may be a small variation or results may not be as precise as expected.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
    • Beginner Friendly.
      4 hours

Syntax

sqrt() function is implemented with the following syntax:

math.sqrt(num_var)

Where the num_var is a positive number.

Example 1

The function returns the square root of 25.

num_var = 25
print(math.sqrt(num_var))

The code returns the following output:

5.0

Example 2

The code below demonstrates the method with a negative value.

num_var = -25
print(math.sqrt(num_var))

The results will look like this:

nan

Where nan stands for “not a number”, which represent undefined or unpresentable numerical results.

All contributors

Contribute to Docs

Learn Lua on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
    • Beginner Friendly.
      4 hours