[ create a new paste ] login | about

Link: http://codepad.org/NkU8ZWpM    [ raw code | output | fork ]

C, pasted on Feb 7:
#include <stdio.h>
#include <math.h>

#define EPSILON 1.0e-8
int main() {
  double sum, term;
  int p, i;
  p = 1;
  sum = 1.0;
  for (i = 1;; i++) {
    /* break-condition */
    if ((term = 1.0 / (p = p * i)) < EPSILON)
      break;
    sum += term;
  }
  printf("         +0.1234567\n");
  printf("result:  %+.12f\n", sum);
  printf("exp(1) = %+.12f\n", exp(1.0));
  printf("diff:    %+.12f\n", sum - exp(1.0));
  return 0;
}
/* end */


Output:
1
2
3
4
         +0.1234567
result:  +2.718281826198
exp(1) = +2.718281828459
diff:    -0.000000002261


Create a new paste based on this one


Comments: