Advertisement
2607

s21_math.c

Dec 10th, 2021
1,421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.89 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 other_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.     t=1.570796;
  81.     long double test_tan = s21_tan(t);
  82.     long double orig_tan = tan(t);
  83.     printf("tan: %Lf\n", s21_tan(t));
  84.     printf("orig tan: %f\n", tan(t));
  85.     printf("difference: %.40Lf\n", test_tan - orig_tan);
  86.     // t = -0.567897654;
  87.     // t = -21345;
  88.     // t = 345;
  89.     // t = 0.9;
  90.     // printf("%f\n", asin(t));
  91.     // printf("%Lf\n", s21_asin(t));
  92.     // printf("%f\n", acos(t));
  93.     // printf("%Lf\n", s21_acos(t));
  94.     // t = 0.4;
  95.     // double x = atan(t);
  96.     // double y = s21_atan(t);
  97.     // double z = old_s21_atan(t);
  98.     // printf("original atan:             %.40lf\t\n", x);
  99.     // printf("s21 atan:                  %.40lf\n", y);
  100.     // printf("difference origin and new: %.40lf\t\n", x - y);
  101.     // printf("difference new and old:    %.40lf\t\t\n", y - z);
  102.     // printf("difference origin and old: %.40lf\t\n", x - z);
  103.     //t = 2.56789;
  104.     //t=1000000000.0;
  105.     //t = 0.00001;
  106.     // t = 1;
  107.     //t = 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0;
  108.     // double or_log = log(t);
  109.     // long double new_log = old_old_s21_log(t);
  110.     // printf("original log = %.40f\n", or_log);
  111.     // printf("     new log = %.40Lf\n", new_log);
  112.     // printf("difference origin and old: %.40Lf\t\n", new_log - or_log);
  113.     // t = (float)M_PI_2;
  114.     // t = (float)s21_PI_POPOLAM;
  115.     // t = s21_PI_6;
  116.     // double or_exp = tan(t);
  117.     // long double new_exp = s21_tan(t);
  118.     // printf("original exp = %.40f\n", or_exp);
  119.     // printf("     new exp = %.40Lf\n", new_exp);
  120.     // printf("difference origin and old: %.40Lf\t\n", new_exp - or_exp);
  121.     // t = 10000000000;
  122.     // double or_sin = sin(t);
  123.     // long double new_sin = s21_sin(t);
  124.     // printf("original sin = %.40f\n", or_sin);
  125.     // printf("     new sin = %.40Lf\n", new_sin);
  126.     // printf("difference origin and old: %.40Lf\t\n", new_sin - or_sin);
  127.     return 0;
  128. }
  129. long int s21_abs(int x) {
  130.     if (x < 0) {
  131.         x = x * (-1);
  132.     }
  133.     return x;
  134. }
  135.  
  136. long double s21_fabs(double x) {
  137.     if (x < 0) {
  138.         x = x * (-1);
  139.     }
  140.     return (long double)x;
  141. }
  142.  
  143. long double s21_floor(double x) {
  144.     double t = x;
  145.     x = (long double)(int)x;
  146.     if (x < 0 && (int)t - t != 0) x -= 1;
  147.     return x;
  148. }
  149. long double s21_ceil(double x) {
  150.     double t = x;
  151.     x = (long double)(int)x;
  152.     if (x > 0 && (int)t - t != 0) x += 1;
  153.     return x;
  154. }
  155.  
  156. long double s21_fmod(double x, double y) {
  157.     double res1, res2, res;
  158.     res1 = x / y;
  159.     if (x < 0 && y < 0) {
  160.         res2 = s21_abs(res1);
  161.     } else if (x < 0 || y < 0) {
  162.         res2 = (-1) * s21_abs(res1);
  163.     } else {
  164.         res2 = s21_abs(res1);
  165.     }
  166.     res = x - res2 * y;
  167.     if (y == 0) {
  168.         res = 0 / 0.0;
  169.     }
  170.     return res;
  171. }
  172.  
  173. long double s21_exp(double x) {
  174.     long double res = 0;
  175.     // if (x > 5) {
  176.     //     res = s21_exp(x-5)*exp(5);
  177.     // }
  178.      if (x<0){
  179.         res = 1/s21_exp((-1)*x);
  180.     } else {
  181.     for (int k = 0; k < 2000; ++k) {
  182.         double current = 1;
  183.         for (int j = 1; j <= k; ++j) {
  184.             current*=x;
  185.             current /= j;
  186.         }
  187.         res += current;
  188.     }
  189.     }
  190.     return res;
  191. }
  192.  
  193. double int_pow(double x, int n) {
  194.     double res = 1;
  195.     for (int i = 0; i < n; i++) {
  196.         res = res * x;
  197.         // printf("%");
  198.     }
  199.     return res;
  200. }
  201.  
  202. long long int factorial(int x) {
  203.     long long int res = 0;
  204.     if (x == 0)
  205.         res = 1;
  206.     else
  207.         res = x * factorial(x - 1);
  208.  
  209.     return res;
  210. }
  211.  
  212. long double s21_sin(double x) {
  213.     int f_minus = 1;
  214.     if (x < 0) {
  215.         f_minus = -1;
  216.         x = (-1)*x;
  217.     }
  218.     x = shift_2pi(x);
  219.     long double res = 0;
  220.     for (int k = 0; k < 30; k++) {
  221.         // long double current = pow(x, 2 * k + 1);
  222.         long double current = 1;
  223.         for (int j = 1; j <= 2 * k + 1; ++j) {
  224.             current *= x;
  225.             current /= (j);
  226.             // printf("%Lf\n", current);
  227.         }
  228.         res += pow(-1, k) * current;
  229.         // printf("%Lf\n", res);
  230.     }
  231.     return res*f_minus;
  232. }
  233.  
  234.  
  235. long double s21_cos(double x) {
  236.     x = shift_2pi(x);
  237.     return s21_sin(s21_PI_POPOLAM - x);
  238. }
  239.  
  240.  
  241.  
  242. long double old_s21_log(double x) {
  243.     double yn = x - 1.0;
  244.     double yn1 = yn;
  245.     double epsilon = 0.000001;
  246.     int counter = 0;
  247.     do {
  248.         yn = yn1;
  249.         // printf("yn: %f\n", yn);
  250.         // printf("exp: %f\n", exp(yn));
  251.         yn1 = yn + 2 * (x - exp(yn)) / (x + exp(yn));
  252.         // printf("yn1: %f\n", yn1);
  253.         counter++;
  254.     } while (s21_fabs(yn - yn1) > epsilon);
  255.     // printf("%d\n", counter);
  256.     return yn1;
  257. }
  258.  
  259. long double s21_log(double x) {
  260.     long double res = 0;
  261.  
  262.     if (x < 0) {
  263.         res = 0 / 0.0;  // nan
  264.     } else if (x == 0) {
  265.         res = -1 / 0.0;
  266.     } else if (x>1) {
  267.         int counter = 0;
  268.         while(x>1) {
  269.             x/=10;
  270.             counter++;
  271.         }
  272.         printf("x =%f\n", x);
  273.         printf("c =%d\n", counter);
  274.         res = s21_log(x)+counter*other_s21_log(10);
  275.     } else if (x < 0.1){
  276.         int counter = 0;
  277.         while(x<0.1) {
  278.             x*=10;
  279.             counter++;
  280.         }
  281.         res = s21_log(x)-counter*other_s21_log(10);
  282.     } else{
  283.         x -= 1;
  284.         for (int k = 1; k < 1000; k++) {
  285.             // printf("k=%d\n", k);
  286.             long double current = 1;
  287.             for (int j = 1; j <= k; ++j) {
  288.                 current *= x;
  289.             }
  290.             current /= k;
  291.             res += pow(-1, k + 1) * current;
  292.         }
  293.     }
  294.     return res;
  295. }
  296.  
  297. long double s21_tan(double x) {
  298.     long double res = 0;
  299.     int flag_minus = 1;
  300.     if (x < 0) {
  301.         x = -x;
  302.         flag_minus = -1;
  303.     }
  304.     x = shift_2pi(x);
  305.     while (x > s21_PI)
  306.         x-=s21_PI;
  307.     x = x*flag_minus;
  308.     // for (int k = 1;k<2000000;k++)
  309.     //     res+=1/((k-0.5)*(k-0.5)*s21_PI*s21_PI-x*x);
  310.     res = s21_sin(x)/s21_cos(x);
  311.     return res;
  312. }
  313.  
  314. long double s21_sqrt(double x) { return s21_pow(x, 0.5); }
  315. long double s21_pow(double base, double exp) { return s21_exp(exp * s21_log(base)); }
  316.  
  317. double shift_2pi(long double x) {
  318.     int flag_minus = 1;
  319.     if (x < 0) {
  320.         x = -x;
  321.         flag_minus = -1;
  322.     }
  323.     while (x > s21_PI_9)
  324.         x -= s21_PI_9;
  325.     while (x > s21_PI_8)
  326.         x -= s21_PI_8;
  327.     while (x > s21_PI_7)
  328.         x -= s21_PI_7;
  329.     while (x > s21_PI_6)
  330.         x -= s21_PI_6;
  331.     while (x > s21_PI_5)
  332.         x -= s21_PI_5;
  333.     while (x > s21_PI_4)
  334.         x -= s21_PI_4;
  335.     while (x > s21_PI_3)
  336.         x -= s21_PI_3;
  337.     while (x > s21_PI_2)
  338.         x -= s21_PI_2;
  339.     while (x > 2 * s21_PI)
  340.         x -= 2 * s21_PI;
  341.     return flag_minus*x;
  342. }
  343.  
  344. long double s21_asin(double x) {
  345.     int flag_minus = 1;
  346.     if (x<0) {
  347.         x = -x;
  348.         flag_minus = -1;
  349.     }
  350.     long double res = 0;
  351.     if (x > 1)
  352.         res = 0 / 0.0;
  353.     else if (x > 1/sqrt(2)) {
  354.         res = s21_PI_POPOLAM - s21_asin(sqrt(1-x*x));
  355.     }
  356.     else {
  357.         for (int k = 0; k < 3000; k++) {
  358.             // long double current = pow(x, 2 * k + 1);
  359.             long double current = 1;
  360.             for (int j = 1; j <= 2 * k + 1; ++j) {
  361.                 current *= x;
  362.                 if (j % 2 == 0 || j == 2 * k + 1)
  363.                     current /= j;
  364.                 else
  365.                     current *= j;
  366.                 // printf("%Lf\n", current);
  367.             }
  368.             // if (k != 0) current /= k;
  369.             res += current;
  370.             // printf("%Lf\n", res);
  371.         }
  372.     }
  373.     return flag_minus*res;
  374. }
  375.  
  376. long double s21_acos(double x) { return s21_PI_POPOLAM - s21_asin(x); }
  377.  
  378. long double s21_atan(double x) {
  379.     long double res = 0;
  380.     int f_minus = 1;
  381.     if (x < 0) {
  382.         x *= (-1);
  383.         f_minus = -1;
  384.     }
  385.     if (x < 0.9)
  386.         res = old_s21_atan(x);
  387.     else if (x > 1) {
  388.         res = s21_PI_POPOLAM - s21_atan(1 / x);
  389.     } else {
  390.         res = s21_asin(x / sqrt(1 + x * x));
  391.     }
  392.     return f_minus*res;
  393. }
  394.  
  395. long double old_s21_atan(double x) {
  396.     long double res = 0;
  397.     for (int k = 0; k < 5000; k++) {
  398.         long double current = 1;
  399.         int j;
  400.         for (j = 1; j <= 2 * k + 1; ++j) {
  401.             current *= x;
  402.             // printf("current in *=: %Lf\n", current);
  403.         }
  404.         if (j != 0) {
  405.             current /= (j - 1);
  406.             // printf("current in /: %Lf\n", current);
  407.         }
  408.         res += pow(-1, k) * current;
  409.         // printf("res: %Lf\n", res);
  410.     }
  411.     return res;
  412. }
  413. long double other_s21_log(double x) {
  414.     x = (x - 1) / (x + 1);
  415.     //printf(" x after proc = %.40f\n", x);
  416.     long double res = 0;
  417.     for (int k = 0; k < 5000; k++) {
  418.         long double current = 1;
  419.         int j;
  420.         for (j = 1; j <= 2 * k + 1; ++j) {
  421.             current *= x;
  422.             // printf("current in *=: %Lf\n", current);
  423.         }
  424.         if (j != 0) {
  425.             current /= (j - 1);
  426.             // printf("current in /: %Lf\n", current);
  427.         }
  428.         res += current;
  429.         // printf("res: %Lf\n", res);
  430.     }
  431.     return 2 * res;
  432. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement