andrelievable

Untitled

Mar 22nd, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6. double exp_(double a)
  7. {
  8. double b = exp(-a);
  9. return (1.f - b) / a;
  10. }
  11. double bw(double przyblizona, double rzeczywista)
  12. {
  13. return fabs((przyblizona - rzeczywista) / rzeczywista);
  14. }
  15. double szereg(double x)
  16. {
  17. double suma = 1;
  18. double wyraz = 1;
  19. for (int i = 1; i <= 18; i++)
  20. {
  21. wyraz *= -x / (i + 1); // na przemian dodawanie i odejmowanie
  22. suma += wyraz;
  23. }
  24. return suma;
  25. }
  26. const double b_repr = pow(10, -16); // blad reprezentacji
  27. int main() {
  28. double fx, log10x, x, b_wz, blad, f_wyliczona;
  29. ifstream infile("dane.txt");
  30. ofstream output;
  31. output.open("wyniki.txt");
  32. while (infile >> log10x >> x >> fx)
  33. {
  34. f_wyliczona = exp_(x);
  35. b_wz = bw(f_wyliczona, fx);
  36. if (b_wz > b_repr)
  37. {
  38. f_wyliczona = szereg(x);
  39. blad = bw(f_wyliczona, fx);
  40. }
  41. if (blad < b_wz)
  42. {
  43. b_wz = blad;
  44. }
  45. output << scientific << log10x << "\t" << log10(b_wz) << "\t" <<
  46. f_wyliczona << "\t" << fx << endl;
  47. }
  48. infile.close();
  49. output.close();
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment