Sign in 
 
Tutorial

Posted by calcsproject on Nov 30, 2008

Description: Evaluate Pi value tutorial script

Tutorial

In this tutorial we’ll discuss simple Pi value calculation algorithm.
Algorithm described on page
or you may look through our screencast on blog

Create new script

To create new script click Create new button on documents list page, enter new script name
and description (optional); then click Create button.

After a new document will be created you’ll see editor page with simple script, like:


print('Hello world');

Save and evaluate script

To save you script just click Save button.

To evaluate it and see a result click Evaluate button. Result will be displayed in a new window. Note: Your script will be saved before evaluation.

Define functions

Here is simple function definition:

function len(x,y)
{
    return Math.sqrt(x*x+y*y);
}

This algorithm is iterative.



So, let’s define two functions to calculate iteration values.

function next_h(b)
{
    return 1-Math.sqrt( 1-(b/2)*(b/2) );
}

function next_b(b,h)
{
    return Math.sqrt(h*h+(b/2)*(b/2));
}

Evaluate Pi value

Define few variables and define loop for algorithm’s iterations.


// Define initial values
pi=0.0; 
b=2;
h=next_h(b);
pow2=1;
// Make 10 iterations
for(i=0;i<10;++i)
{
    pi += pow2*b*h;
    pow2 = pow2*2;
    b = next_b(b,h);
    h = next_h(b);
}

To output result use print function.

print('Value of Pi is '+pi);

Comments

Sign in to post comments

Powered by Google App Engine