fork download
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. double exp_func(double x, double error) {
  5. double prev_summ = 0;
  6. double summ = 0;
  7. double curr_x = 1;
  8. double curr_fact = 1;
  9.  
  10. int k = 1;
  11. while (1) {
  12. prev_summ = summ;
  13. summ += curr_x/curr_fact;
  14.  
  15. if (abs(summ - prev_summ) < error) return summ;
  16.  
  17. curr_x *= x;
  18. curr_fact *= k;
  19.  
  20. k++;
  21. }
  22. }
  23.  
  24. double sin_func(double x, double error) {
  25. double prev_summ = 0;
  26. double summ = 0;
  27. double curr_x = x;
  28. double curr_fact = 1;
  29.  
  30. int k = 1;
  31. while (1) {
  32. prev_summ = summ;
  33. summ += curr_x / curr_fact;
  34.  
  35. if (abs(summ - prev_summ) < error) return summ;
  36.  
  37. curr_x *= -1*x*x;
  38. curr_fact *= (2*k)*(2*k+1);
  39.  
  40. k++;
  41. }
  42. }
  43.  
  44. double sqrt_func(double a, double error) {
  45. double prev_x = 0;
  46. double x = 2;
  47.  
  48. while (1) {
  49. prev_x = x;
  50. x = (1 / 2.) * (x + a / x);
  51.  
  52. if (abs(x - prev_x) < error) return x;
  53. }
  54. }
  55.  
  56. double func(double x) {
  57. return sqrt(1 + x) * exp(x + 0.5) * sin(0.3 * x + 0.7);
  58. }
  59.  
  60. int main() {
  61. std::cout << "x my_func std_func error" << std::endl;
  62.  
  63. float c1 = 7.5;
  64. float c2 = 3.3;
  65. float c3 = 12.3;
  66.  
  67. double epsilon1 = pow(10, -6) / c1;
  68. double epsilon2 = pow(10, -6) / c2;
  69. double epsilon3 = pow(10, -6) / c3;
  70.  
  71. float x = 0.5;
  72. for (int i = 0; i < 11; i++) {
  73. double my_func = sqrt_func(1 + x, epsilon1) * exp_func(x + 0.5, epsilon2) * sin_func(0.3 * x + 0.7, epsilon3);
  74. double std_func = func(x);
  75. printf("%.2f ", x);
  76. printf("%.20f ", my_func);
  77. printf("%.20f ", std_func);
  78. printf("%.20f\n", abs(my_func - std_func));
  79.  
  80. x += 0.01;
  81. }
  82. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
x       my_func                           std_func                          error
0.50    2.50116399811221201688            2.50116402330774212359            0.00000002519553010671
0.51    2.54137675220539716037            2.54137675463345580340            0.00000000242805864303
0.52    2.58213842351352473869            2.58213842625982525902            0.00000000274630052033
0.53    2.62345570542747363874            2.62345570853045817472            0.00000000310298453599
0.54    2.66533534077165379372            2.66533534427399487754            0.00000000350234108382
0.55    2.70778414556923241463            2.70778414951825219248            0.00000000394901977785
0.56    2.75080900976205589004            2.75080901421018353403            0.00000000444812764400
0.57    2.79441689793643410766            2.79441690294170763664            0.00000000500527352898
0.58    2.83861485005484270872            2.83861485568144278346            0.00000000562660007475
0.59    2.88340998219355970633            2.88340998851239849188            0.00000000631883878555
0.60    2.92880948728631018696            2.92880949437567217331            0.00000000708936198635