@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 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
<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.
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
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
<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.
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... 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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...
|
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....
|
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...
|
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;
|
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...
|
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...
|
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%...
|
by: aarklon |
last post by:
Hi all,
see:- http://mathforum.org/library/drmath/view/54456.html
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |