2.2 What Is Pi?

In this chapter, we continue to explore computer science, problem solving, and the Python programming language by considering one of the most famous numbers of all: the number pi, often represented by the Greek letter π. Almost everyone has at one time or another used the value pi. In fact, we used π in the Introduction to Python chapter in our circle calculations.

Pi is defined to be the ratio of a circle’s circumference to its diameter (FIGURE 2.1). This relationship, π = C/d, gives rise to the familiar equation C = πd that is used to compute the circumference of a circle given the diameter. Because the diameter of a circle is twice the radius, this relation can also be written as C = 2πr, where r is the radius.

A circle of diameter d and circumference C is shown.

FIGURE 2.1 Pi is the ratio of circumference (C) to diameter (d).

Other common formulas that utilize pi include the area of a circle with radius r, (A = πr2), the volume of a sphere with radius r,

Image

and the surface area of a sphere with radius r, (S = 4πr2).

The value of pi has been a matter of interest for thousands of years. Writings from ancient Egypt and Babylon as well as the Bible contain references to this mystical number. In your math class, it is likely that you were told to use 3.14 or 3.14159 as the value of pi. In fact, these values worked fairly well for many of the problems that you were asked to solve. It turns out, however, that the exact value of pi is not as simple as this would make it seem.

Pi is known in mathematics as an irrational number. In other words, pi is a floating-point number with an infinite, nonrepeating pattern of decimal digits. The actual value of pi is

Image

where the ellipsis (…) indicates the infinite digits.

Because pi cannot be stated exactly, the best that we can do is provide an approximation—for example, 3.14. Other usual approximations are

Image

and the more complex Image.

SESSION 2.1 shows the interactive Python environment as we evaluate each of these approximations.

Image

SESSION 2.1 Simple approximations for pi

Session 2.1 introduces a new Python element: the statement import math. Recall that the import statement allows us to gain access to a Python module that contains additional functionality that we might find useful. In this case, we need to compute the square root of 2. After importing the math module, we can use the sqrt() function to do this for us.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.234.39