Advertisement
2607

s21_math.c

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