![]() |
|
#1
|
|||
|
|||
C++ exponent help please!!!!!CPP / C++ / C Code:
Last edited by LuciWiz : 26-Nov-2005 at 05:47.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#2
|
|||
|
|||
Re: C++ exponent help please!!!!!oops forgot to pose question, here it is
I am unable to get the power function to work. please help!!! I have tried different number types and formats. I am trying to use this equation ... pi = pi + 4*pow(-1.0,n)/(2*n+1); or in other words pi = pi + (4* (-1)^n) / (2*n -1); or in other words pi equals pi plus 4 times negative one to the n power all devided by quantity 2 times n minus one this is my program... |
|
#3
|
||||
|
||||
Re: C++ exponent help please!!!!!Hey.
I did some changes only so that your code will compile on my machine, but notice that I declared your globals inside "int main". Why did you declare them as globals ? is there a special reason ? And to business : 1. You are not approximating PI correctly, but you probably understand this by now. Notice the output you get before and after each iteration of your PI approximation loop. This is what I got for n = 4 : Code:
with this code : CPP / C++ / C Code:
Check the calculation. Best Regards, Kobi. [edit] Try this link : PI digits [/edit] __________________
It's actually a one time thing (it just happens alot). |
|
#4
|
|||
|
|||
Re: C++ exponent help please!!!!!Quote:
First let's make sure everyone understands the formula: The value of pi can be expressed as the sum of an infinite series. It's kind of hard to write mathematical notation with text, but I will try: pi is equal the sum of an infinite series: pi = 4 - 4/3 + 4/5 - 4/9 + 4/11 -... The "..." indicates to "continue in the same manner". What the heck does that mean? It means that we would like a general expression for the n'th term. A little examination of the series allows us to observe that, if the first term was for n = 0, and the second term was for n = 1, then the nth term, in general, can be expressed as 4.0 * (-1 to the power n)/(2*n - 1) (Which is kind of what you wrote in your post.) Note that we could write that "pi = 3.14159...". That's also an infinite series, but in this case, the "..." doesn't tell us much about what "in the same manner" can mean. Of course we can't calculate the sum of all of the terms of an infinite sequence in a finite amount of time, so we try to find an approximate value of the sum by stopping after a finite number of terms. Whenever we do something like this, the first question(s) we should ask is/are: Is the formula (the infinite series) correct? Does the series converge? (Not all infinite series have a partial sum that converges to a fixed number as n goes to infinity.) Does it converge to pi? I'll leave proof of this to the mathematicians. We might be able to test it with a program (that is write a program that calculates lots of terms), but we can't prove it with a program (at least not with a program that just adds up a bunch of terms). The next question should always (always) be: If I stop after a certain number of terms, how can I know how good is my approximation? If we can't answer this question, then why bother to calculate at all? Well, you say, if I know the value of pi, then I can calculate the error at each step. That's true, and can give insight to the properties of this infinite series (how fast does it converge), but it's kind of cheating. (If we know the value of pi, then why bother with the series at all.) In general, in order for an approximation of any kind to be useful, we would like to know some kind of upper bound for the error. This series is convergent and the terms have alternating signs, and there is a theorem that holds for such series: The absolute value of the error is less than or equal to the absolute value of the first neglected term. This absolutely astounding fact makes it possible for us to calculate an approximation to pi and know how close we are without even knowing what the value of pi is to start with(!) Maybe you aren't astounded, but I am completely blown away by the awesome power of mathematics to be able to let us do such things. So, if we want to calculate the value to two correct decimal places, we can quit the calculations when we see that the absolute value of the next term is less tnan or equal to 0.005. (Since, if the error is less than half a unit in the second decimal place, the approximate value can be rounded to two correct decimal places.) If we want the value to three correct decimal places, we keep adding until the absolute value of the next term is less than or equal to 0.0005. etc. Now here's how you can calculate partial sums of an infinite series: Code:
From a programming point of view, there is no guarantee that the loop will ever end (due to an error in the formula, or perhaps due to roundoff error), so we always put a limit on the number of times that the loop will ever execute. So, we have a variable, say max_iterations, and we arbitrarily set this to some number, say 10000. If we find that the results are not within our tolerance after max_iterations, we can look at what happened. If the tolerance was unrealistically small, we might increase it. If it looks like things are going OK, but we need more iterations, we can increase max_iterations and run it again. Then the program becomes: Code:
A further programming note: using the power function to calculate values powers of -1 is not really a good way to do it, and I have found some compilers that don't like negative values as the first argument, and there are better ways. But for now, you can try a program based on this pseudo code and see how it comes out. You can actually calculate (with pencil and paper or in the program) how many terms the program should need in order to get a flavor of how fast or slow the convergence is. For example after 100 terms, the absolute value of the next term is about 0.02. (So lots more than 100 terms are required to get two correct decimal places.) Regards, Dave "The purpose of computing is insight, not numbers." ---Richard W. Hamming "Everything is made up of little numbers, whizzing around so fast that they become stuff." ---Terry Pratchett in The Truth Last edited by davekw7x : 26-Nov-2005 at 09:56.
|
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help in c++ recursive functions | vinch | C++ Forum | 7 | 04-Jan-2006 13:22 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The