Gregory Leibniz Series Pi Calculator

closed account (owCkSL3A)
1
Last edited on
Hi,

Please edit your post so it uses code tags, Select the code then press the <> button on the format menu.

The variable i is an int, with integer division line ?? produces pi += 2 * 1 ;

If you use a do loop, put the while part of it on the same line as the closing brace, otherwise it looks like a while loop with a null statement.

} while (dif > acc);

Variable s is not used in the do loop. Is that supposed to changed the sign of the terms in the series?
closed account (owCkSL3A)
variable s changes the sign so that it can follow the series correctly.

How should i divide variable i to produce the correct answer?
Last edited on
Yes, that is the intent, but you don't use that variable in the loop to achieve that.

Edit change i to a double
Last edited on
closed account (owCkSL3A)
The 2 should be an s.
Last edited on
No worries, the other problem is that i should be double.
closed account (owCkSL3A)
.
Last edited on
Guess i have been staring at it for too long.


Here is some advice that will hopefully prove to be gold:

Learn to use a debugger: It will save you days of staring at code, or otherwise messing around trying to see what is wrong.

Hopefully here is a GUI version in your IDE. One can set up a watch list of variables to keep an eye on. Step through the code 1 line at a time, looking at the variable values - deduce where it goes wrong.

There are debuggers that work from the command line as well.

Good Luck !!
Didn't work for me: I put 5, and the answer was 2.7

Try the debugger :+)
closed account (owCkSL3A)
Looking into that now. I also forgot to mention that accuracy is inputted in the form of .001,.0001....

Thanks for the help!
Ok,

Seems ok now, just that you are printing 1 more dp than what the accuracy of the series is, and std::cout does rounding.
It would be also good do write
 
pi +=  s * (double(4) / i) ;

For give a signal, that we use double's and not int.
I also would multiply by 4 at end.
greatings, Daniel
@danjiun

The variable i is now a double so the whole expression is double. However I like to put numbers before and after the decimal point for a double literal, as in:

pi += s * 4.0 / i ;

So this avoids an unnecessary explicit cast.

And to avoid the magic number, make it a const variable , or optionally constexpr for C++11 instead:

const double numerator = 4.0;

constexpr double numerator = 4.0; // c++11

pi +=  s * numerator / i ;


Finally, I like longer variable names - they make the code more readable IMO. A lot of people have this idea they need to abbreviate everything big time for coding, I think that is a misplaced idea.
Yes, this was better.
And even better,specially, if s is already a double we can also write :
1
2
3
pi4 += s / i;
//...
pi = pi4*4;

Wau ... constexpr is superb, i dont know before about it.
Now i already ready read about it. It is really useful in many cases.
.. About shorter or longer variables, i also like to comment :
My opinion is if a ranche of a variable is longer, it is better give a good name for this variable, for understand it better. So i think same as TheldeasMan, however if it is a code in which we use vey often a variable and it is clear which function it have, it would be more readable if it is shorter. For example :
1
2
3
4
for(int t=0;t<n; t++) {
arr1[t] = ...;
arr2[t] = ...;
}

In some case i already use a superlong name for a variable, such in :
 
checkChangePropCondDelegateBinpartLowTriang();

Ok, this was a name of function, but sometimes it will happen for variables. So sometimes, if i use inside a function many times a longer variable or name of a function or a name of a class, i use
* typedef LongNameClass shortClassName;
* className& shortName = longName;
...
greatings, Daniel
Topic archived. No new replies allowed.