By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

pi(e)

P: n/a
@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

Jul 9 '06 #1
Share this Question
Share on Google+
9 Replies


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

Kai-Uwe Bux
Jul 9 '06 #2

P: n/a

<za******@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
@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

P: n/a
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

P: n/a
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

P: n/a
<za******@gmail.comwrote:

in message news:11**********************@m73g2000cwd.googlegr oups.com...
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

P: n/a

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

in message news:11**********************@m73g2000cwd.googlegr oups.com...
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

P: n/a

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

in message news:11**********************@m73g2000cwd.googlegr oups.com...
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

P: n/a

peter koch skrev:
za******@gmail.com skrev:
osmium skrev:
<za******@gmail.comwrote:
>
in message news:11**********************@m73g2000cwd.googlegr oups.com...
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

P: n/a
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.