Advertisement
Guest User

Exponenta

a guest
Sep 10th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. //exp(x) evaluation
  4. int main() {
  5.   double x = -1000;
  6.  
  7.   bool invert = (x < 0);
  8.   if (invert) {
  9.     x = -x;
  10.   }
  11.   double res = 0;
  12.  
  13.   double pw = 1;
  14.   double fac = 1;
  15.   for (int i = 0; i < 100; ++i) {
  16.     res += pw/fac;
  17.     fac *= (i+1);
  18.     pw *= x;
  19.   }
  20.  
  21.   if (invert) {
  22.     res = 1 / res;
  23.     x = -x;
  24.   }
  25.  
  26.   printf("exp(%lf) = %lf\n", x, res);
  27.  
  28.   return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement