double i;
for( i = 1; i<=10; i++)
{
if( i == 1 ) cout << "1";
if( i % 2.0 == 0.0) cout << " - 1/" << 2 * i - 1;
if( i % 2.0 != 0.0) cout << " + 1/" << 2 * i - 1;
}
return 0;
}
Every time I try to compile it, I keep getting an error. Before you ask why I assigned i as a double, this is just part of a bigger project I'm working on, but the main issue is located within the if statements. Any advice?
The problem comes in with pow(-1,i+1). Since it only works with i being a double, its throwing off my if statements. I could always make another variable and have it be positve or negative one depending on the value of i, but that seems very inefficient.
It should always be an integer, because i starts at one and is incremented by 1. I was trying to work around my pow(-1,i + 1), but it looks like i'll have to change that.
the presence of the double literals will force double arithmatic to be used. Otherwise it might use integer division and multiplication and only promote the result of that for the addition. (I'm a little unsure of the promotion rules at this time of the evening...)
If the literals weren't there, you'd need to cast one or more of the ints involved in the calculation to force promotion.
Andy
PS I think one double literal would be enough, but I think it looks nicer (!) when all are the same.