472,782 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 1821
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.