I've created and edited the crap out of a script to calculate pi, and now I just want to know if I can make the line that carries out the calculation any shorter; everything else is fine.
Now, I do realize that I can put the {pi+=4/i*s;} on a separate line, but I'm trying to minimize the number of lines while still making it look good, so that's why I'm asking, is there some magical function I'm not aware of to make things even simpler?
#include <iostream>
int main()
{
double pi = 0 ; // initial value is zero, we will keep updating this in the loop
int sign = 1 ; // sign multiplier for the next term
constdouble numerator = 4.0 ; // constant for all terms
double denominator = 1.0 ; // initial denominator
double term ; // magnitude of the next term in the series
constdouble epsilon = 0.00001 ; // we quit when term becomes less than this
// while the magnitude of the next term is greater than epsilon
while( ( term = numerator/denominator ) > epsilon )
{
pi += term * sign ; // add or subtract the term
sign = -sign ; // flip the sign
denominator += 2 ; // get to the next denominator
}
std::cout << "pi == approximately " << pi << '\n' ;
}