fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. long long fakultaet(long long x){
  7. if(x < 0){
  8. //Fehler
  9. return -1;
  10. }
  11. if(x == 0 || x == 1){
  12. return 1;
  13. }
  14. long long erg = 2;
  15. for(long long i = 3; i <= x; i++)
  16. {
  17. erg *= i;
  18. }
  19. return erg;
  20. }
  21.  
  22. long double ehoch(long double x){
  23. long double erg = 0;
  24. long long counter = 0;
  25. long double epsilon = 0.00001;
  26. long double next = pow(x,counter) / fakultaet(counter);
  27. counter++;
  28. while(epsilon < abs(next) && counter < 100){
  29. erg += next;
  30. next = pow(x,counter) / fakultaet(counter);
  31. counter++;
  32. }
  33. return erg;
  34. }
  35.  
  36. int main() {
  37. for (double x = 0; x < 10; x++)
  38. cout << x << ": " << setprecision(10) << ehoch(x) << " <-> " << exp(x) << endl;
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
0: 1 <-> 1
1: 2.71827877 <-> 2.718281828
2: 7.389046016 <-> 7.389056099
3: 20.08553443 <-> 20.08553692
4: 54.59814722 <-> 54.59815003
5: inf <-> 148.4131591
6: inf <-> 403.4287935
7: inf <-> 1096.633158
8: inf <-> 2980.957987
9: inf <-> 8103.083928