Languages
[Edit]
EN

Java - Math.PI property example

0 points
Created by:
Flimzy
596

The Math.PI property returns π number (3.141592653589793...).

public class MathExample {

    public static void main(String[] args) {

        // ------------------------------------------------------
        // Math.PI value priting:

        System.out.println( Math.PI ); // 3.141592653589793

        // ------------------------------------------------------
        // Math.PI with circle:

        final double radius = 5;

        // 1. Circle surface area:
        double area = Math.PI * Math.pow(radius, 2);
        System.out.println( area ); // 78.53981633974483

        // 2. Circle circumference:
        double circumference = 2 * Math.PI * radius;
        System.out.println( circumference ); // 31.41592653589793
    }
}

1. Documentation

Syntax
package java.lang;

public final class Math {

    public static final double PI = 3.14159265358979323846;

}

Note: Classes in the java.lang package are imported automatically, so it is not necessary to do it manually - we use just Math.PI() call.

Resultπ number (3.141592653589793...).
Description

PI is a static property that keeps π number what is one of the most important mathematical constants.

2. Nilakantha series example

To calculate PI number Nilakantha series can be used.

Nilakantha series used to calculate Math.PI in Java.
Nilakantha series used to calculate Math.PI in Java.
public class CustomMath {

    static double computePi(int iterations) {
        double approximation = 3;
        double a = 2;
        for (int i = 0; i < iterations; i++) {
            approximation += 4 / (a * (++a) * (++a));
            approximation -= 4 / (a * (++a) * (++a));
        }
        return approximation;
    }

    public static void main(String[] args) {

        System.out.println( CustomMath.computePi(    1 ));  // 3.1333333333333333
        System.out.println( CustomMath.computePi(    2 ));  // 3.1396825396825396
        System.out.println( CustomMath.computePi(    5 ));  // 3.1414067184965018
        System.out.println( CustomMath.computePi(   10 ));  // 3.141565734658547
        System.out.println( CustomMath.computePi(   20 ));  // 3.141589028940776
        System.out.println( CustomMath.computePi(   50 ));  // 3.1415924109719824
        System.out.println( CustomMath.computePi(  100 ));  // 3.141592622804848
        System.out.println( CustomMath.computePi(  200 ));  // 3.1415926497127264
        System.out.println( CustomMath.computePi(  500 ));  // 3.141592653340544
        System.out.println( CustomMath.computePi( 1000 ));  // 3.141592653558594
        System.out.println( CustomMath.computePi( 2000 ));  // 3.141592653585895
        System.out.println( CustomMath.computePi( 5000 ));  // 3.141592653589538
    }
}

References

  1. Pi - Wikipedia

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - Math object

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join