How do i calculate pie as a computer programmer?

I was wondering how it is i could calculate pi as a mathematical formula? i wanted to make a computer program using either C++ or VB.NET(2008) but i cannot find how to calculate pi... can anyone help me out?

Update:

But i want to know how i would put those numbers into a programming way?

Update 3:

Ok, i used the C++ program and edited it a bit... I got it to work right, but why does it only go to the 17th bit and not any further?

Comments

  • This comes up again and again...

    The google-fu is not strong with you...

    So, I get to amuse myself. Here is a program to compute PI. It is written in TECO (Text Editor and COrrector), which is an editor that dates back to 1962. TECO macros are very powerful; this is just for fun (can your editor compute PI?).

    [user@ariel ~]$ cat `locate pi.tec`

    GZ0J\UNQN"E 40UN ' BUH BUV HK

    QN< J BUQ QN*10/3UI

    QI< \+2*10+(QQ*QI)UA B L K QI*2-1UJ QA/QJUQ

    QA-(QQ*QJ)-2\ 10@I// -1%I >

    QQ/10UT QH+QT+48UW QW-58"E 48UW %V ' QV"N QV^T ' QWUV QQ-(QT*10)UH >

    QV^T @^A/

    /HKEX

    Note that the macro actually ends in 2 ESC characters. And, to show that it actually works... (MUNG is the macro executor, stands for Mangle Until No Good):

    [user@ariel ~]$ MUNG pi

    3141592653589793238462643383279502884197

    [user@ariel ~]$

    Now, for your answer (not a very good answer -- but it should give you an idea, first answer that comes up in google for the query "calculate pi in c++"):

    http://www.cplusplus.com/forum/beginner/1149/

    And, in case you don't want to bother clicking:

    #include <iostream>

    #include <iomanip>

    int main() {

    unsigned int decP;

    unsigned int denom=3;

    float ourPi=4.0f;

    bool addFlop=true;

    cout << "How many loop iterations? ";

    cin >> decP;

    for (unsigned int i=1;i<=decP;i++) {

    if (addFlop) {

    ourPi-=(4.0/denom);

    addFlop=false;

    denom+=2;

    } else {

    ourPi+=(4.0/denom);

    addFlop=true;

    denom+=2;

    }

    }

    cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed) << setprecision(9);

    cout << "Pi calculated with " << decP << " iterations is: ";

    cout << ourPi << endl;

    }

    And now for a PI program in Scheme:

    ;; Program to calulate the decimal expansion of pi

    ;; This function calculates the arc cotan of a value scaled by the scale

    ;; factor. It uses scaled integer arithmetic to get more precision.

    (define (scaled-acot x scale n value)

    (define (calc-term x n)

    (define two-n-minus-1 (- (* n 2) 1))

    (quotient

    (* (if (odd? n) 1 -1) scale)

    (* (expt x two-n-minus-1) two-n-minus-1)))

    (let ((term (calc-term x n)))

    (if (< (abs term) 1)

    value

    (scaled-acot x scale (+ n 1) (+ value term)))))

    ;; This function sums up the arc cotan function values of the odd terms

    ;; in the Fibonacci sequence to produce an approximation of pi/4

    (define (sum-acot-even-fibonacci F_even F_odd scale sum)

    (let ((term (scaled-acot F_odd scale 1 0)))

    (if (< (abs term) 1)

    sum

    (sum-acot-even-fibonacci

    (+ F_even F_odd) ;; i.e. Fn+1

    (+ F_odd F_even F_odd) ;; equivalent to (+ Fn+1 Fn), i.e., Fn+2

    scale

    (+ sum term)))))

    (define (calc-extra-digits precision)

    (define (log10 x)

    (/ (log x) (log 10)))

    (inexact->exact

    (+ 2

    (ceiling

    (log10 (* 3 precision))))))

    (define (calc-pi precision)

    (let*

    ((extra-digits (calc-extra-digits precision))

    (scale (expt 10 (+ precision extra-digits))))

    ;; Add a couple of extra decimal places to the precision to take

    ;; accumulation of error in lower order terms into account. Scale

    ;; back down for the output

    (quotient

    (* 4 (sum-acot-even-fibonacci 1 2 scale 0))

    (expt 10 extra-digits))))

    (begin

    (display (calc-pi 1000))

    (display "\n"))

    Now, lets run that one (to 1000 places):

    [user@ariel projects]$ gsi pi.scm

    314159265358979323846264

    338327950288419716939937

    510582097494459230781640

    628620899862803482534211

    ...and so on...

  • In Linux there is a nice program called Schnell Pi but it just calculates Pi to a specified amount of digits

  • There are many ways to calculate pi. Google can provide you with many mathematical approaches. Some of the methods are outlined here: http://documents.wolfram.com/v5/Demos/Notebooks/Ca...

Sign In or Register to comment.