468,458 Members | 1,827 Online

# pi(e)

@code
#include <iostream>
#include <cmath>

int main() {
double cache;
unsigned long n;
for (n = 1; n != 0; n++) {
cache += 1/n;
std::cout << sqrt(cache*6) << std::endl;
}
}
@end

As far as im concerned this should work, but ofcourse it doesnt.
I must have done somthing wrong, but i fail to see where.
To those in doubt, these few line should calculate pi (3.1415.....)

Zacariaz

Jul 9 '06 #1
9 1683
za******@gmail.com wrote:
@code
#include <iostream>
#include <cmath>

int main() {
double cache;
You should initialize this value:

double cache = 0.0;
unsigned long n;
for (n = 1; n != 0; n++) {
cache += 1/n;
1/n is an unsigned long. Thus, you get cache = 1 after the first iteration.
From there on, 1/n == 0 and cache does not change. Thus, the next line will
always print sqrt(6).

Try

cache += 1.0/n;
std::cout << sqrt(cache*6) << std::endl;
}
}
@end

As far as im concerned this should work, but ofcourse it doesnt.
I must have done somthing wrong, but i fail to see where.
To those in doubt, these few line should calculate pi (3.1415.....)
Oh, wait: in that case

cache += 1.0/n;

would not do you good, either since there is a mistake in the math, too: You
will want

cache += sqr(1.0/n);
Finally, for better precision, you should reverse the order of this loop:
Best

Kai-Uwe Bux
Jul 9 '06 #2

<za******@gmail.comwrote in message
@code
#include <iostream>
#include <cmath>

int main() {
double cache;
This double is not initialized so it could be anything
unsigned long n;
This should be declared inside of the loop
for (n = 1; n != 0; n++) {
cache += 1/n;
Don't you mean 1.0/n ? 1/n is going to
return a long which will truncate so basically,
the first time it adds 1 and then 0 until the
unsigned long wraps to 0.
std::cout << sqrt(cache*6) << std::endl;
}
}
@end

As far as im concerned this should work, but ofcourse it doesnt.
I must have done somthing wrong, but i fail to see where.
To those in doubt, these few line should calculate pi (3.1415.....)
I don't think so. I would expect it to keep printing the same
number for an incredibly long time if it doesn't crash.
With msvc 2005, the initial value of
cache is -9.2559631349317831e+061.

After an initial break to tell me that cache is being used
without initialization, it goes on to print
1.#IND repeatedly.
Jul 9 '06 #3
Working now, and have corrected the error so now it estimates pi as it
should, but, i only get 5 digits, what can i do to get more?

@code
#include <iostream>
#include <cmath>

int main() {
double cache = 0.0;
for (unsigned long n = 1; n != 0; n++) {
cache += 1.0/pow(n,2);
std::cout << sqrt(cache*6) << std::endl;
}
}
@end

Jul 9 '06 #4
This looks a litle better:

@code
#include <iostream>
#include <cmath>

int main() {
double cache = 0.0;
for (unsigned long n = 1000000; n != 0; n--) {
cache += 1.0/pow(n,2);
}
std::cout << sqrt(cache*6) << std::endl;
std::cin.get();
}
@end

Jul 9 '06 #5
<za******@gmail.comwrote:

Working now, and have corrected the error so now it estimates pi as it
should, but, i only get 5 digits, what can i do to get more?
There are tons of articles about different methods of computing pi. Look
around the web. Use "algorithm" as one of the search terms.
Jul 9 '06 #6

osmium skrev:
<za******@gmail.comwrote:

Working now, and have corrected the error so now it estimates pi as it
should, but, i only get 5 digits, what can i do to get more?

There are tons of articles about different methods of computing pi. Look
around the web. Use "algorithm" as one of the search terms.
I know, i just wanted to see if this actually worked, it just seemed
too simple...

Jul 9 '06 #7

za******@gmail.com skrev:
osmium skrev:
<za******@gmail.comwrote:

Working now, and have corrected the error so now it estimates pi as it
should, but, i only get 5 digits, what can i do to get more?
Check how many decimals you print.

There are tons of articles about different methods of computing pi. Look
around the web. Use "algorithm" as one of the search terms.

I know, i just wanted to see if this actually worked, it just seemed
too simple...
It is not too simple. It works.

/Peter

Jul 9 '06 #8

peter koch skrev:
za******@gmail.com skrev:
osmium skrev:
<za******@gmail.comwrote:
>
Working now, and have corrected the error so now it estimates pi as it
should, but, i only get 5 digits, what can i do to get more?

Check how many decimals you print.
i print 5 but would like to print a few more just to see how slow this
is...
>
There are tons of articles about different methods of computing pi. Look
around the web. Use "algorithm" as one of the search terms.
I know, i just wanted to see if this actually worked, it just seemed
too simple...
It is not too simple. It works.

/Peter
Jul 9 '06 #9
za******@gmail.com wrote:
i print 5 but would like to print a few more just to see how slow this
is...
If you want to print more decimals, look up std::setprecision() and
std::fixed in <iomanip>.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 12 '06 #10

### This discussion thread is closed

Replies have been disabled for this discussion.