pi(e)

Z

zacariaz

@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.....)

Thanks in advance.

Zacariaz
 
K

Kai-Uwe Bux

@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:
start with a large n and go down to 1.


Best

Kai-Uwe Bux
 
D

Duane Hebert

@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.
 
Z

zacariaz

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
 
Z

zacariaz

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
 
O

osmium

in message news:[email protected]...
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.
 
Z

zacariaz

osmium skrev:
in message news:[email protected]...

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...
 
Z

zacariaz

peter koch skrev:
(e-mail address removed) skrev:


Check how many decimals you print.

i print 5 but would like to print a few more just to see how slow this
is...
 
M

Marcus Kwok

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>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top