Decomposition of a function in a row

C language (C). The result is not what I would like. Implemented without recurrent formulas and did something wrong. Bernoulli numbers seem to be correct.

#include <stdio.h> #include <conio.h> #include <math.h> long int Factorial(long int x) { //функция вычисления факториала if (x == 0 || x == 1) return 1; //по определению факториал нуля и единицы равен единице else return x*Factorial(x-1); //функция вызывается внутри себя (рекурсия) } float Bink (long int n, long int k) { //Биномиальный коэффициент Ньютона return 1.0*Factorial(n)/Factorial(k)/Factorial(nk); } float Bernoulli(long int n) { //Число Бернулли if (n<=0) return 1; else { float s = 0; long k; for(k = 1;k<=n;k++) s+=Bink(n+1,k+1)*Bernoulli(nk); return -1.0/(n+1)*s; } } float Cotangent(int x) { int i = 1; float p = 1.0, s = 1.0, eps = 0.00001; for (;fabs(p)>eps;i++) { p = (pow(2,2*i)*Bernoulli(i)*pow(x,2*i-1))/Factorial(2*i); s+=p; #ifdef DEBUG printf("\ni = %i, p = %f, s = %f",i,p,s); //отладочные сообщения #endif } return 1/(float)x - s; } int main() { printf("\tЧИСЛА БЕРНУЛЛИ:\n"); for (i = 0; i<=10; i++) printf("B[%d] = %f\n", i, Bernoulli(i)); printf("\n\n\tРАЗЛОЖЕНИЕ ФУНКЦИИ\n\n"); printf("\n\n%f", Cotangent(1)); return 0; } 

Attaching debug results

enter image description here

As you can see, the result (0.888889) does not at all coincide with the real value of the function (cot (1) = 0.642092). What is the matter?

    1 answer 1

    I do not remember such a formula. Error, apparently in itself.

    Please take a look at this formula . Took from here . So I substituted it for yours - and the answer came together. If you insist that your formula is correct, please give a link to the source, check.

    • Hmm, really the same. The formula of the university’s textbooks, I think, should be addressed to the teacher. There and Bernoulli numbers as an example are not at all correct. - user193688
    • Well, the manuals of universities are often full of mistakes, I was a teacher myself, I know how and why they are written. Perhaps, of course, there is another record of the same thing in your manual, since the decomposition itself to the right of the last equal sign is correct. In this case, you need to study the program in detail in steps, checking each operation with a manual account at each step. - Zealint 2:46 pm