[ create a new paste ] login | about

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

C, pasted on Oct 26:
#include <stdio.h>
#include <math.h>

#define N 10
double fact[N];

double test_exp(double x)
{
  int i;
  double sum = x;

  /* powers of x */  
  double xp[N];
  xp[1] = x;
  for (i = 2; i < N; ++i)
    xp[i] = xp[i - 1] * x;
  
  /* sum */
  for (i = N - 1; i; --i)
    sum += xp[i] / fact[i];

  return sum;
}

int main()
{
  int i;
  /* fill fact */
  fact[0] = 1.0;
  for (i = 1; i < N; ++i)
    fact[i] = fact[i - 1] * (double)i;

  /* test exp */
  printf("%f", exp(1.0) - test_exp(1.0));
  return 0;
}


Output:
1
0.000000


Create a new paste based on this one


Comments: