Advertisement
2607

PIPOPOLAM

Dec 5th, 2021
1,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.18 KB | None | 0 0
  1. #include "s21_math.h"
  2.  
  3. #include <math.h>
  4. #include <stdio.h>
  5.  
  6. double int_pow(double x, int n);
  7. long long int factorial(int x);
  8. double shift_2pi(long double x);
  9. long double old_s21_atan(double x);
  10. long double old_s21_log(double x);
  11. float my_faster_logf(float a);
  12. long double old_old_s21_log(double x);
  13.  
  14. int main() {
  15.     // long double y = -5.55555;
  16.     // printf("%Lf\n", s21_fabs(y));
  17.     // printf("%f\n\n", fabs(y));
  18.     // printf("%f\n", floor(-4.4));
  19.     // printf("%Lf\n", s21_floor(-4.4));
  20.     // printf("%f", exp(1));
  21.     // long long int test = factorial(25);
  22.     // printf("%lld", test);
  23.     double t = s21_PI * 756745.6534643;  // сходится
  24.     t = 5.1945045;                       // сходится
  25.     t = -s21_PI * 756450.5;              // cходится
  26.     t = s21_PI * 756450;                 // cходится
  27.     t = s21_PI * 75645000;               // сходится
  28.     t = s21_PI * 756450000.546;          // сходится
  29.     // t = 100000000;                       // сходится
  30.     // t = 10000000040;  // сходится 10 знаков
  31.     // t = s21_PI_POPOLAM + s21_PI_POPOLAM;
  32.     // t = 10000000001.0;
  33.     // t = 10000000002.0;
  34.     // t = 19099100000;  // не сходится последний знакЪ
  35.     // t = 1000000000100.0;
  36.     // t = 57.739537;
  37.     // t = 57.73932712;
  38.     // t = 2707.543636;
  39.     // t = 2707.543615;
  40.     // t= 2713.02345678;
  41.     // t = 1000000.645646;
  42.     // t = 2707;
  43.     // t = s21_PI/2;
  44.     // t = -s21_PI/2;
  45.     // t = 710;
  46.     // t = 2*s21_PI;
  47.     // t = s21_PI_11;
  48.     // t = s21_PI_10;
  49.     // t = 2707;
  50.     // printf("%Lf\n", s21_sin(t));
  51.     // printf("%f\n", sin(t));
  52.     // printf("%Lf\n", s21_cos(t));
  53.     // printf("%f\n", cos(t));
  54.  
  55.     // printf("%Lf\n", s21_log(t));
  56.     // printf("%f\n", log(t));
  57.     // printf("%Lf\n", s21_sqrt(t));
  58.     // printf("%f\n", sqrt(t));
  59.  
  60.     // double pi_d = 314159265358.979323846264338;
  61.     // printf("%.15f\n", pi_d);
  62.     // long double pi_l = 314159265358.979323846264338;
  63.     // printf("%.15Lf\n", pi_l);
  64.     // printf("%.15f\n", s21_PI_9);
  65.     // long double test_exp = s21_exp(t);
  66.     // printf("%f\n", (double)test_exp);
  67.     // printf("%f", exp(t));
  68.     // long double test_sin = s21_sin(t);
  69.     // printf("our sin = %Lf\n", test_sin);
  70.     // printf("origin sin = %f\n", sin(t));
  71.     // printf("pi = %.40f\n", s21_PI);
  72.     // printf("origin pi = %.40f\n", M_PI);
  73.     // printf("1000000*pi = %.40f\n\n", s21_PI * 1000000);
  74.     // printf("%Lf\n", s21_ceil(5.999999999999999999999));
  75.     // printf("%f\n", ceil(5.999999999999999999));
  76.     // printf("%Lf\n", s21_floor(0));
  77.     // printf("%f\n", floor(0));
  78.     // printf("%f\n", sin(0));
  79.     // t = 5 * s21_PI;
  80.     // long double test_tan = s21_tan(t);
  81.     // long double orig_tan = tan(t);
  82.     // printf("tan: %Lf\n", s21_tan(t));
  83.     // printf("orig tan: %f\n", tan(t));
  84.     // printf("difference: %.40Lf\n", test_tan - orig_tan);
  85.     // t = -0.567897654;
  86.     // printf("%f\n", asin(t));
  87.     // printf("%Lf\n", s21_asin(t));
  88.     // printf("%f\n", acos(t));
  89.     // printf("%Lf\n", s21_acos(t));
  90.     // t = 0.4;
  91.     // double x = atan(t);
  92.     // double y = s21_atan(t);
  93.     // double z = old_s21_atan(t);
  94.     // printf("original atan:             %.40lf\t\n", x);
  95.     // printf("s21 atan:                  %.40lf\n", y);
  96.     // printf("difference origin and new: %.40lf\t\n", x - y);
  97.     // printf("difference new and old:    %.40lf\t\t\n", y - z);
  98.     // printf("difference origin and old: %.40lf\t\n", x - z);
  99.     //t = 2.56789;
  100.     //t=1000000000.0;
  101.     //t = 0.00001;
  102.     // t = 1;
  103.     //t = 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0;
  104.     // double or_log = log(t);
  105.     // long double new_log = old_old_s21_log(t);
  106.     // printf("original log = %.40f\n", or_log);
  107.     // printf("     new log = %.40Lf\n", new_log);
  108.     // printf("difference origin and old: %.40Lf\t\n", new_log - or_log);
  109.     // double or_exp = exp(t);
  110.     // long double new_exp = s21_exp(t);
  111.     // printf("original exp = %.40f\n", or_exp);
  112.     // printf("     new exp = %.40Lf\n", new_exp);
  113.     // printf("difference origin and old: %.40Lf\t\n", new_exp - or_exp);
  114.     t = 10000000000;
  115.     double or_sin = sin(t);
  116.     long double new_sin = s21_sin(t);
  117.     printf("original sin = %.40f\n", or_sin);
  118.     printf("     new sin = %.40Lf\n", new_sin);
  119.     printf("difference origin and old: %.40Lf\t\n", new_sin - or_sin);
  120.  
  121.     return 0;
  122. }
  123.  
  124. // АБС КОСЯЧИТ
  125. long int s21_abs(int x) {
  126.     if (x < 0) {
  127.         x = x * (-1);
  128.     }
  129.     return x;
  130. }
  131. // Работает
  132. long double s21_fabs(double x) {
  133.     if (x < 0) {
  134.         x = x * (-1);
  135.     }
  136.     return (long double)x;
  137. }
  138.  
  139. long double s21_floor(double x) {
  140.     double t = x;
  141.     x = (long double)(int)x;
  142.     if (x < 0 && (int)t - t != 0) x -= 1;
  143.     return x;
  144. }
  145. long double s21_ceil(double x) {
  146.     double t = x;
  147.     x = (long double)(int)x;
  148.     if (x > 0 && (int)t - t != 0) x += 1;
  149.     return x;
  150. }
  151.  
  152. long double s21_fmod(double x, double y) {
  153.     double res1, res2, res;
  154.     res1 = x / y;
  155.     if (x < 0 && y < 0) {
  156.         res2 = s21_abs(res1);
  157.     } else if (x < 0 || y < 0) {
  158.         res2 = (-1) * s21_abs(res1);
  159.     } else {
  160.         res2 = s21_abs(res1);
  161.     }
  162.     res = x - res2 * y;
  163.     if (y == 0) {
  164.         res = 0 / 0.0;
  165.     }
  166.     return res;
  167. }
  168.  
  169. long double s21_exp(double x) {
  170.     long double res = 0;
  171.     if (x > 5) {
  172.         res = s21_exp(x-5)*exp(5);
  173.     } else if (x<0){
  174.         res = 1/s21_exp((-1)*x);
  175.     } else {
  176.     for (int k = 0; k < 2000; ++k) {
  177.         double current = 1;
  178.         for (int j = 1; j <= k; ++j) {
  179.             current*=x;
  180.             current /= j;
  181.         }
  182.         res += current;
  183.     }
  184.     }
  185.     return res;
  186. }
  187.  
  188. double int_pow(double x, int n) {
  189.     double res = 1;
  190.     for (int i = 0; i < n; i++) {
  191.         res = res * x;
  192.         // printf("%");
  193.     }
  194.     return res;
  195. }
  196.  
  197. long long int factorial(int x) {
  198.     long long int res = 0;
  199.     if (x == 0)
  200.         res = 1;
  201.     else
  202.         res = x * factorial(x - 1);
  203.  
  204.     return res;
  205. }
  206.  
  207. long double s21_sin(double x) {
  208.     int f_minus = 1;
  209.     if (x < 0) {
  210.         f_minus = -1;
  211.         x = (-1)*x;
  212.     }
  213.     x = shift_2pi(x);
  214.     long double res = 0;
  215.     for (int k = 0; k < 30; k++) {
  216.         // long double current = pow(x, 2 * k + 1);
  217.         long double current = 1;
  218.         for (int j = 1; j <= 2 * k + 1; ++j) {
  219.             current *= x;
  220.             current /= (j);
  221.             // printf("%Lf\n", current);
  222.         }
  223.         res += pow(-1, k) * current;
  224.         // printf("%Lf\n", res);
  225.     }
  226.     return res*f_minus;
  227. }
  228.  
  229.  
  230. long double s21_cos(double x) {
  231.     x = shift_2pi(x);
  232.     return s21_sin(s21_PI_POPOLAM - x);
  233. }
  234.  
  235.  
  236.  
  237. long double old_s21_log(double x) {
  238.     double yn = x - 1.0;
  239.     double yn1 = yn;
  240.     double epsilon = 0.000001;
  241.     int counter = 0;
  242.     do {
  243.         yn = yn1;
  244.         // printf("yn: %f\n", yn);
  245.         // printf("exp: %f\n", exp(yn));
  246.         yn1 = yn + 2 * (x - exp(yn)) / (x + exp(yn));
  247.         // printf("yn1: %f\n", yn1);
  248.         counter++;
  249.     } while (s21_fabs(yn - yn1) > epsilon);
  250.     // printf("%d\n", counter);
  251.     return yn1;
  252. }
  253.  
  254. long double old_old_s21_log(double x) {
  255.     long double res = 0;
  256.  
  257.     if (x < 0) {
  258.         res = 0 / 0.0;  // nan
  259.     } else if (x == 0) {
  260.         res = -1 / 0.0;
  261.     } else if (x>1) {
  262.         //x-=1;
  263.         int counter = 0;
  264.         while(x>1) {
  265.             x/=10;
  266.             counter++;
  267.         }
  268.         printf("x =%f\n", x);
  269.         printf("c =%d\n", counter);
  270.         res = old_old_s21_log(x)+counter*s21_log(10);
  271.     } else if (x < 0.1){
  272.         int counter = 0;
  273.         while(x<0.1) {
  274.             x*=10;
  275.             counter++;
  276.         }
  277.         res = old_old_s21_log(x)-counter*s21_log(10);
  278.     } else{
  279.         x -= 1;
  280.         for (int k = 1; k < 1000; k++) {
  281.             // printf("k=%d\n", k);
  282.             long double current = 1;
  283.             for (int j = 1; j <= k; ++j) {
  284.                 current *= x;
  285.                 // printf("%Lf\n", current);
  286.             }
  287.             current /= k;
  288.             // res += current;
  289.             res += pow(-1, k + 1) * current;
  290.             // printf("%.12Lf\n", res);
  291.         }
  292.         //res = counter * res;
  293.         //res = s21_log(x);
  294.     }
  295.     return res;
  296. }
  297.  
  298. long double s21_tan(double x) {
  299.     double result;
  300.  
  301.     if (x == -s21_PI) {
  302.         result = -(s21_sin(x) / s21_cos(x));
  303.     } else {
  304.         result = s21_sin(x) / s21_cos(x);
  305.     }
  306.     return result;
  307. }
  308.  
  309. long double s21_sqrt(double x) { return s21_pow(x, 0.5); }
  310. long double s21_pow(double base, double exp) { return s21_exp(exp * s21_log(base)); }
  311.  
  312. double shift_2pi(long double x) {
  313.     long double origin_x = x;
  314.     // printf("%Lf\n", x);
  315.     // while (x > s21_PI_11) {
  316.     //     x -= s21_PI_11;
  317.     //     printf("11$ %Lf\n", x);
  318.     // }
  319.     // while (x > s21_PI_10) {
  320.     //     x -= s21_PI_10;
  321.     //     printf("10$ %Lf\n", x);
  322.     // }
  323.     while (x > s21_PI_9) {
  324.         x -= s21_PI_9;
  325.         // printf("9$ %Lf\n", x);
  326.     }
  327.     while (x > s21_PI_8) {
  328.         x -= s21_PI_8;
  329.         printf("8$ %Lf\n", x);
  330.     }
  331.     while (x > s21_PI_7) {
  332.         x -= s21_PI_7;
  333.         // printf("7$ %Lf\n", x);
  334.     }
  335.     while (x > s21_PI_6) {
  336.         x -= s21_PI_6;
  337.         // printf("6$ %Lf\n", x);
  338.     }
  339.     while (x > s21_PI_5) {
  340.         x -= s21_PI_5;
  341.         // printf("5$ %Lf\n", x);
  342.     }
  343.     while (x > s21_PI_4) {
  344.         x -= s21_PI_4;
  345.         // printf("4$ %Lf\n", x);
  346.     }
  347.     while (x > s21_PI_3) {
  348.         x -= s21_PI_3;
  349.         // printf("3$ %Lf\n", x);
  350.     }
  351.     while (x > s21_PI_2) {
  352.         x -= s21_PI_2;
  353.         // printf("2$ %Lf\n", x);
  354.     }
  355.     while (x > 2 * s21_PI) x -= 2 * s21_PI;
  356.     while (x < 0) x += 2 * s21_PI;
  357.     return x;
  358. }
  359.  
  360. long double s21_asin(double x) {
  361.     long double res = 0;
  362.     if (x < -1 || x > 1)
  363.         res = 0 / 0.0;
  364.     else {
  365.         for (int k = 0; k < 50000; k++) {
  366.             // long double current = pow(x, 2 * k + 1);
  367.             long double current = 1;
  368.             for (int j = 1; j <= 2 * k + 1; ++j) {
  369.                 current *= x;
  370.                 if (j % 2 == 0 || j == 2 * k + 1)
  371.                     current /= j;
  372.                 else
  373.                     current *= j;
  374.                 // printf("%Lf\n", current);
  375.             }
  376.             // if (k != 0) current /= k;
  377.             res += current;
  378.             // printf("%Lf\n", res);
  379.         }
  380.     }
  381.     return res;
  382. }
  383.  
  384. long double s21_acos(double x) { return s21_PI_POPOLAM - s21_asin(x); }
  385.  
  386. long double s21_atan(double x) {
  387.     long double res = 0;
  388.     int f_minus = 0;
  389.     if (x < 0) {
  390.         x *= (-1);
  391.         f_minus = 1;
  392.     }
  393.     if (x < 0.9)
  394.         res = old_s21_atan(x);
  395.     else if (x > 1) {
  396.         res = s21_PI_POPOLAM - s21_atan(1 / x);
  397.     } else {
  398.         res = s21_asin(x / sqrt(1 + x * x));
  399.     }
  400.     if (f_minus == 1) res *= (-1);
  401.     return res;
  402. }
  403.  
  404. long double old_s21_atan(double x) {
  405.     long double res = 0;
  406.     for (int k = 0; k < 5000; k++) {
  407.         long double current = 1;
  408.         int j;
  409.         for (j = 1; j <= 2 * k + 1; ++j) {
  410.             current *= x;
  411.             // printf("current in *=: %Lf\n", current);
  412.         }
  413.         if (j != 0) {
  414.             current /= (j - 1);
  415.             // printf("current in /: %Lf\n", current);
  416.         }
  417.         res += pow(-1, k) * current;
  418.         // printf("res: %Lf\n", res);
  419.     }
  420.     return res;
  421. }
  422. long double s21_log(double x) {
  423.     x = (x - 1) / (x + 1);
  424.     //printf(" x after proc = %.40f\n", x);
  425.     long double res = 0;
  426.     for (int k = 0; k < 5000; k++) {
  427.         long double current = 1;
  428.         int j;
  429.         for (j = 1; j <= 2 * k + 1; ++j) {
  430.             current *= x;
  431.             // printf("current in *=: %Lf\n", current);
  432.         }
  433.         if (j != 0) {
  434.             current /= (j - 1);
  435.             // printf("current in /: %Lf\n", current);
  436.         }
  437.         res += current;
  438.         // printf("res: %Lf\n", res);
  439.     }
  440.     return 2 * res;
  441. }
  442.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement