Java

math.pi in Java

In math, the symbol for pi is π. The author, me, has a scientific calculator. From this calculator, the value of pi is:

3.141592654

The number of decimal places here is many. In fact, pi does not have the last decimal place. Pi is defined as the ratio of the circumference of a circle to the diameter of the circle. The reader might have heard that pi = 22/7. That is an approximation. Pi is one of those values in mathematics, called a surd. A surd is a number whose decimal precision can never be determined.

If the ideal circumference of a circle is measured, if the ideal diameter of this same circle is measured, and if this ideal circumference is divided by the ideal diameter, then the value obtained would be pi. This value can only be approximated to a number of decimal places. This is because, in doing the math long-division, there will always be a remainder. And so, the value of pi given by the author’s scientific calculator is an approximation to 9 decimal places.

Pi is a ratio of two lengths, and so it has no unit. It is a particular number. It is a constant in science. A Java program can return the value of pi, as explained in this tutorial.

Java Program for pi

The following Java program, displays the value of pi:

    public class TheClass {
        public static void main(String[] args) {
            double pi = Math.PI;
System.out.println(pi);
        }
    }

The output from the author’s computer is:

3.141592653589793

Though a very long number, it is still an approximation. The main class, TheClass, nests the main() method in the program. The main method nests the two statements of the program. The first statement uses the static Math field (property), PI, to return the value of pi, received by the variable, pi. The second statement prints out the value of pi to the terminal.

Java pi Syntax

The full syntax to obtain the value of pi in Java, is:

public static final double PI

The variable name is PI (uppercase) and not pi (lowercase). PI is the name of a field in the predefined Math class in Java. In the Java Object Oriented Programming scheme, a property is called a field. The return value is of the double type and not of the float type.

The reserved word “final” means that the variable is constant, and its value cannot be changed in the syntax. Remember that pi is a constant in mathematics. The reserved word “static” means that an object of the Math (mathematics) class does not have to be instantiated before the variable, PI can be used. The reserved word “public” means that the variable PI can be accessed from outside the Math class and must not only be accessed by members of the Math class.

Package for Math Class

Java predefined classes exist in different packages. The Math class is in the java.lang.* package. This package does not have to be imported by the programmer. It is imported automatically. That is why there is no importation statement in the program above. All the same, including the importation statement into the program, will not change anything; as the following program illustrates:

    import java.lang.*;
    public class TheClass {
        public static void main(String[] args) {
            double pi = Math.PI;
System.out.println(pi);
        }
    }

The output is still the same, as:

3.141592653589793

In Java, when the class is in the java.lang.* package, importing the package manually is optional.

Custom Value for pi

Instead of using the predefined value for pi, the programmer can create his own value. It is simple: just assign the result of 22 divided by 7 to a variable of type, double. This can be done in any computer language, not only Java. Consider:

double numerator = 22 ; double denominator = 7 ;

double pi1 = numerator/denominator ;

The result of the division is assigned to pi1. The numerator and denominator have to be of the double type to have an appreciable result, with decimal digits. The following program compares such a custom pi, with the pi returned by Math.PI :

    import java.lang.*;
    public class TheClass {
        public static void main(String[] args) {
            double numerator = 22 ; double denominator = 7 ;
            double pi1 = numerator/denominator ;
System.out.println("Custom pi is: " + pi1);
            double pi2 = Math.PI;
System.out.println("Java pi is: " + pi2);
        }
    }

The output is:

Custom pi is: 3.142857142857143

Java pi is: 3.141592653589793

The value returned by Math.PI is more accurate (more precise) than the one obtained from 22.0 / 7.0 . The difference in decimal digits begins from the third decimal place.

When the computer language does not have a library, the custom approach should be used to return pi.

Basic Java Program

A basic Java program consists of the main programmer-defined class and the main() method, like the above programs. For the main method, “String[] args” refers to the argument strings from the keyboard (terminal/console). The body of the main method typically has statements as in the above programs.

In order to print output to the terminal, a statement like

System.out.println(pi);

has to be used. Here, “System” is a predefined class. “out” is a member of the predefined class. “println()” is a method of “out”. Its argument is the value of what is displayed.

The name of the program file should be the name of the main class; something like,

TheClass.java

To compile the file into the bytecode, use a Bash terminal command for the particular compiler, like,

javac TheClass.java

To run the bytecode, use a Bash terminal command for the particular bytecode interpreter, like,

java TheClass

Note that the extension “.java” is omitted here.

Conclusion

pi is a constant in mathematics. It is a ratio of two different lengths of the same unit, so it has no unit. Pi or π is an example of what is called a surd in math. This means that its precision, by decimal digits, can never be obtained. In Java, Math.PI returns a good value for pi. Pi can be obtained manually in a program by diving 22.0 by 7.0. This result is good, but not as good as that returned by the Java Math.PI expression.

It might interest the reader that, though pi is a surd, it is used a lot in engineering, including mechanics and architecture.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.