First of all, in order to get an approximation you need the value to decrease dramatically. For example, -1/x
. If x
increases much the answer will be near zero.
- What value must be assumed for
∞
to approximate the calculation as above?
for ( float k = 0 ; ; k++ )
{
sum += (((2 * ( k) + 1) * x) - ((2 * ( k) - 1) * x)); //adding from 1 to infinity
sum += (((2 * (-k) + 1) * x) - ((2 * (-k) - 1) * x)); //adding from -1 to infinity
}
but this will result in an infinite loop. You can replace x
with 1/x
and increase x
each time in the loop. The code becomes
if ( (sum - lastsum) < 0.001 )
{
break;
}
lastsum = sum;
}
- What minimum value must be used to increment
K
within [the] loop, assuming that K
holds fractional values too?
This is according to the type of K
. If it is double
, it could have up to 15 decimal places.
In addition, in the summation, you should move by one, not a fraction. See
https://upload.wikimedia.org/math/d/f/2/df26e1cf51b67fbedd01ce9c68cbbef5.png
https://en.wikipedia.org/wiki/Summation