Hello! How can I make a program in C ++, some given x and eps determined by input, calculate the sum of those terms that are greater in absolute value than eps. Compare the results with the exact value of the function for which this sum determines the approximate value when x lies in the interval (- R, R), the formula itself is written in while, I just don't understand what to do next,the code is almost finished, but what else to add?
this code isnt finished at all ... its not c++ but from a math textbook or another language.
^ is logical xor.
! is logical not.
print does not exist.
so the first thing to do is convert the math to c++.
you can use pow(base, exponent).
factorials are best as a lookup table:
uint64_t fact[] = {1,1,2,6,24, .... fill this out as far as you need it};
make sure you have floating point types or that division will be integer division, which says that 2/3 is zero.
then you get something like
while(eps < (pow(x,n+1)/fact[n])
{
etc
}
cout << answer << endl; //print in c++
1 - x^4/3! + x^6/5! - x^8/7!.... (where ^ is power and ! is factorial).
As such, you don't need to calculate powers or factorial. For each iteration, the numerator is the previous one * x * x * -1 and the denominator is the previous one * n * (n - 1) and with n incrementing by 2 each iteration.
@sukuna231
The formula you give in the "while" statement is NOT for the function you put in the printf statement. Which one of those two is correct?
If the one in the printf statement is correct (it's called the sinc() function: sin(x)/x ) then the one in the "while" statement (and the following line) would have n-1 in it and not n+1.
Here's an attempt assuming the one in the printf statement is correct: