The math.pi constant represents the mathematical constant π (pi), which is the ratio of the circumference of a circle to its diameter.

Value

\[
\text{math.pi} \approx 3.14159
\]

Property Attributes

  • Writable: no
  • Enumerable: no
  • Configurable: no

Description

Since pi is a constant in the math library, it is accessed as math.pi, rather than as a property of an object. It is commonly used in mathematical calculations involving circles, such as finding the circumference or area.

Examples

Using math.pi

The following function calculates the circumference of a circle given its radius:

function calculateCircumference(radius)
  return math.pi * (radius * 2)
end

print(calculateCircumference(1)) -- Output: 6.283185307179586

Using math.pi to calculate the area of a circle

function calculateArea(radius)
  return math.pi * radius * radius
end

print(calculateArea(1)) -- Output: 3.141592653589793

See also

  • math

Was this page helpful?

Table of Contents