![]() The value of the pi variable keeps a running record of the current approximation of pi. At the end of each for loop iteration, the slice with is halved. For any particular iteration of the for loop, the slicewidth variable gives the x (horizontal) width of the slice, which remains constant during the entire iteration of the for loop. Each iteration of calculatePi()'s for loop represents one attempt to calculate the area of the quarter circle. The calculatePi() method works by making repeated passes at calculating the area of the quarter circle, with each subsequent pass using a smaller slice width. As the rectangles get thinner and more numerous, the approximation will get closer and closer to the real pi. As you can see from the above diagram, this approach to calculating pi will always yield an approximation of pi that is too large. To find the area of the portion of the circle shown in blue in the diagram above, the calculatePi() method slices the area up into progressively smaller rectangular segments, as shown in the diagram below:īecause calculating the area of a rectangle is a piece of cake (or in this case, a slice of pi), the calculatePi() method is able to approximate pi by calculating and summing the areas of the rectangles, then multiplying the result by four. Here's a diagram of a circle with the area that calculatePi() focuses on shown in blue: To determine the area of the circle with radius one, the calculatePi() method works to find the area of one fourth of the circle, then multiplies that area by four to get pi. Because the circle has a radius of one, the circle's area is pi itself. To calculate pi, the calculatePi() method tries to determine the area of a circle that has a radius of one. The calculatePi() method loops forever in an attempt to find and capture the elusive pi, the ratio of the circumference of a circle to its diameter. On CD-ROM in file pi/ex1/PiCalculator.java The bytecode sequence in the simulation was generated by javac for the calculatePi() method of the PiCalculator class shown below: ![]() This applet is described in Appendix D, "Slices of Pi: A Simulation of the Java Virtual Machine", of Inside the Java 2 Virtual Machine.įor some reason, your browser won't let you view this way cool Java applet. The Slices of Pi applet, included below, demonstrates a Java virtual machine executing a sequence of bytecodes that calculates pi.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |