Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

JavaScript - Math.PI property example

0 contributions
13 points

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

// ONLINE-RUNNER:browser;

console.log( Math.PI );

// Math.PI with circle:

// 1. Circle surface area:
var radius = 5;
var area = Math.PI * Math.pow(radius, 2);
console.log( area ); // 78.53981633974483

// 2. Circle circumference:
var radius = 5;
var circumference = 2 * Math.PI * radius;
console.log( circumference ); // 31.41592653589793

1. Documentation

SyntaxMath.PI
Resultπ number (3.141592653589793...).
Description

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


2. Nilakantha series example

To calculate PI number Nilakantha series can be used.

Nilakantha series used to calculate Math.PI in JavaScript.
Nilakantha series used to calculate Math.PI in JavaScript.
// ONLINE-RUNNER:browser;

function computePi(iterations) {
	var aproximation = 3;

  	for (var i = 0, a = 2; i < iterations; ++i) {
      	aproximation += 4 / (a * (++a) * (++a));
      	aproximation -= 4 / (a * (++a) * (++a));
    }
  
  	return aproximation;
}

// Usage example:

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

References

  1. Pi - Wikipedia

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey đź‘‹
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more