A
A
Andrey2018-10-24 16:40:11
C++ / C#
Andrey, 2018-10-24 16:40:11

Why does the compiler not correctly perform arithmetic operations?

The 3rd line calculates the factorial
. The rest are the upper and lower limits according to the Stirling formula.
But for some reason the results are the same, although this is obviously not correct, since 1 / (12n + 1) is less than 1 / (12n)
What is the problem?
Thank you!

int N, n, i;
  double res1 = 0, res2 = 1, res3 = 0, res4 = 0;
  
  N = 10;

  for (n = 1; n <= N; n++) {
    res1 = sqrt(2 * M_PI * n) * pow((n / M_E), n) * pow(M_E, 1 / (12 * n + 1));
    res2 = res2 * n;
    res3 = sqrt(2 * M_PI * n) * pow((n / M_E), n) * pow(M_E, 1 / (12 * n));
    res4 = sqrt(2 * M_PI * n) * pow((n / M_E), n);
  }
  printf("Bot: %.20f\n", res1);
  printf("Fac: %.20f\n", res2);
  printf("Top: %.20f\n", res3);
  printf("Sti: %.20f\n", res4);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lz961, 2018-10-24
@officialandrey

1 / (12 * n + 1) == 0 1 -- int, 12*n+1 -- int, int/int -- division with remainder

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question