Advertisement
Guest User

Untitled

a guest
Nov 6th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. float func(float x,float m) {
  8.     return powf(1+x,m);
  9. }
  10.  
  11. float approximate_func(float x,float m) {
  12.     float result = 1;
  13.     float temp_x = 1;
  14.     float temp_m = 1;
  15.     float temp_fact = 1;
  16.     int i = 1;
  17.     while (i<=4) {
  18.         temp_m *= (m-i+1);
  19.         temp_x *= x;
  20.         temp_fact *= i;
  21.         float value = temp_m * temp_x / temp_fact;
  22.         result += value;
  23.         i++;
  24.     }
  25.     return result;
  26. }
  27.  
  28. int fact(int n) {
  29.     int result = 1;
  30.     int i = 1;
  31.     while (i<n)
  32.         result *= (++i);
  33.     return result;
  34. }
  35.  
  36. float inverse_fact(float n,int count) {
  37.     float result = n;
  38.     while (count > 0) {
  39.         result *= (--n);
  40.         count--;
  41.     }
  42.     return result;
  43. }
  44.  
  45. float power(float a,int b) {
  46.     if (b == 0)
  47.         return 1;
  48.     float result = a;
  49.     for (int i=1;i<b;i++)
  50.         result *= a;
  51.     return result;
  52. }
  53.  
  54. float approximate_func_without(float x,float m) {
  55.     int i = 1;
  56.     float result = 1;
  57.     while (i<=4) {
  58.         result += (inverse_fact(m,i-1) * power(x,i) / fact(i));
  59.         i++;
  60.     }
  61.     return result;
  62. }
  63.  
  64. int _tmain(int argc, _TCHAR* argv[]) {
  65.     setlocale(0, "");
  66.     float x;
  67.     float m = 0;
  68.     cout << "Введите x:";
  69.     cin >> x;
  70.     cout << "Значение\tПриближенное значение\tАбсолютная ошибка\tОтносительная ошибка" << endl;
  71.     while (m <= 6) {
  72.         float exact = func(x,m);
  73.         float approximate = approximate_func(x,m);
  74.         cout << exact;
  75.         cout << "|";
  76.         cout << approximate;
  77.         cout << "|";
  78.         cout << fabs(exact - approximate);
  79.         cout << "|";
  80.         cout << fabs(exact - approximate) / fabs(approximate);
  81.         cout << endl;
  82.         m += 0.5;
  83.     }
  84.     system("pause");
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement