Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Calculating Pi

Name: Jim Profit 2013-02-13 0:18

Alright, for the life of me, I can't figure out what is wrong with my code. I'm trying to use this formula to calculate pi in c++. I've tried it with a while loop too, but it always gives me pi = 1. Can anyone help me out?

#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
    char reply;

    double pi=0;
    long n;
    long i;

    cout << "Enter the value of n: ";
    cin >> n;
   
    // Set the state of the output stream to format using 2 places of decimals.
    cout << setprecision(4);
    cout << fixed << setprecision(2);// Set the output stream to display floating point numbers in fixed point notation


for (i = 0; i <= n; i++) {
    if (i%2 == 0)
        pi = (pi+(1 / (2 * i + 1)));
    else
        pi = pi-(1 / (2 * i + 1));
    cout << pi << endl;
}
pi = pi * 4;
cout << pi;

// "Pause" so the user can see the results

    cout << "\nEnter q to quit... ";
    cin >> reply;
return 0;

}

Name: Anonymous 2013-02-13 1:05

I ran your program. It only displayed:
kill me
kill me
kill me
please kill me

Name: Anonymous 2013-02-13 1:09

Wow! Is it really you?

Name: Anonymous 2013-02-13 1:17

so this is the Gregory–Leibniz series?

Name: Anonymous 2013-02-13 1:25

i bet it would work if you used C....

Name: Anonymous 2013-02-13 2:26

still, i would try if ((i%2) == 0), don't know if that's it, but it might be..

Name: Anonymous 2013-02-13 3:04

I remember calculating Pi by directly drawing a circle , using pythagoras theorem, then just counting pixels and dividing their number by radius.

Name: Anonymous 2013-02-13 3:24

>>1
i&1 is 35% more efficient than i%2

Name: Anonymous 2013-02-13 5:46

lazy mode

ji = 0;

for (iter = 0:2:10)

  ji = ji + (1 / (2 * iter + 1));

  ji = ji - (1 / (2 * (iter + 1) + 1);

  endfor;

ji = ji * 4;

Name: Anonymous 2013-02-13 5:49

Jim Profit

If you have posted in this thread YHBT!

Name: Anonymous 2013-02-13 5:58

>>10 nah, i learnt something =)

Name: Anonymous 2013-02-13 7:23

similar but faster..

function ji = pivalX(n, n2 = 0)

longrange = [n2:n];

lrplus = 1 ./ (longrange .* 4 .+ 1);

lrminus = 1 ./ (longrange .* 4 .+ 3);

ji = sum(lrplus .- lrminus);
 
ji = ji * 4;

endfunction;


>pivalX(10,000,000)
>elapsed time is 1.4976
>ans = 3.14159260358980

Name: Anonymous 2013-02-13 7:26

pi += (1.0 / (2.0 * i + 1.0));
    else
pi -= (1.0 / (2.0 * i + 1.0));


maybe

Name: Anonymous 2013-02-13 7:34

so why does this approximate pi though?

Name: Anonymous 2013-02-13 8:50

>>11
Oh fuck, >>1 is the 13yo retard-kun!

Name: Anonymous 2013-02-13 9:15

>>14
the jew formulas say so apparently, what are you a nazi

Name: Anonymous 2013-02-13 9:38

#define PI 22.0/7.0

Name: Anonymous 2013-02-13 13:56

>>14
taylor series of arctan(x) around x=1

Name: Anonymous 2013-02-13 14:01

I can't figure out what is wrong with my code

cepples

also the true men calculate pi with monte-carlo

Name: Anonymous 2013-02-14 1:16

>>17
Terrible!

Name: Anonymous 2013-02-14 1:22

#include "stdio.h"

long double GetPI(void){
long double pi;
_asm("fldpi;fstpt %pi")
return pi;
}

int main(void){
printf("%.20Lg\n",GetPI());
return 0;
}

Don't change these.
Name: Email:
Entire Thread Thread List