473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Thanks in advance.

Zacariaz

Jul 9 '06 #1
9 1859
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

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

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

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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
18
by: Karl Pech | last post by:
Hi, I got a task there I have to compute pi using the Method above. So I wrote the following program: --- import random import math
9
by: Steven T. Hatton | last post by:
I can get the accurate value of Pi to an arbitrary precision, but it would be nice to have it directly and readily available as a standard feature of the language. Have I overlooked something, or...
20
by: Kraig | last post by:
Hi! I'm new to programming and am trying to figure out the best way using loops, to compute pi to say, 14 terms using values that double each time through the loop. As in, from 1-2-4-8-16, et al....
5
by: confusedprogrammer | last post by:
I am doing a program for a class of mine. I have figured out how to display the final result of PI but i have not figured out how to print the part where is ask me print out Pi after a certain number...
123
by: Lane Straatman | last post by:
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <complex.h> /* double complex z1, z2, z3; bool flag; z1 = .4 + .7I; z2 = cpow(z1, 2.0); z3 = z1 * z1;
17
by: Szabolcs Nagy | last post by:
i've just found out (searching through n1124.pdf) that in c99 math.h does not contain M_PI what is the desired way to use the PI constant in a c code then? #define PI...
17
by: pyramid | last post by:
Hello I am working on one of my lab for this week, which calculates the approximate value of pi. Listed below is the actual problem, which I am posting here, so that you can see the different...
1
by: Andrew Lee | last post by:
Mensanator wrote: Heh! I wonder who needs that many digits? Certainly not number theorists (they need a LOT more). Certainly not physicists -- they need about 30 digits to be within 1%...
42
by: aarklon | last post by:
Hi all, see:- http://mathforum.org/library/drmath/view/54456.html
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.