Advertisement
2607

Untitled

Dec 3rd, 2021
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.45 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include "s21_math.h"
  4.  
  5.  
  6. double int_pow(double x, int n);
  7. long long int factorial(int x);
  8. double shift_2pi(long double x);
  9.  
  10. int main() {
  11.   // long double y = -5.55555;
  12.   // printf("%Lf\n", s21_fabs(y));
  13.   // printf("%f\n\n", fabs(y));
  14.   // printf("%f\n", floor(-4.4));
  15.   // printf("%Lf\n", s21_floor(-4.4));
  16.   // printf("%f", exp(1));
  17.   // long long int test = factorial(25);
  18.   // printf("%lld", test);
  19.   double t = s21_PI*756745.6534643; // сходится
  20.   t = 5.1945045; // сходится
  21.   t = -s21_PI*756450.5; // cходится
  22.   t = s21_PI*756450; // cходится
  23.   t = s21_PI*75645000; // сходится
  24.   t = s21_PI*756450000.546; // сходится
  25.   t = 100000000; // сходится
  26.   t = 1000000004; // сходится
  27.   t = 10000000001.0;
  28.   t = 10000000002.0;
  29.   t = 110001000000;
  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 = 0;
  41.   // t = 2*s21_PI;
  42.   // t = s21_PI_11;
  43.   // t = s21_PI_10;
  44.   //t = 0;
  45.   // double pi_d = 314159265358.979323846264338;
  46.   // printf("%.15f\n", pi_d);
  47.   // long double pi_l = 314159265358.979323846264338;
  48.   // printf("%.15Lf\n", pi_l);
  49.   printf("%.15f\n", s21_PI_9);
  50.   // long double test_exp = s21_exp(t);
  51.   // printf("%f\n", (double)test_exp);
  52.   // printf("%f", exp(t));
  53.   long double test_sin = s21_sin(t);
  54.   printf("our sin = %Lf\n", test_sin);
  55.   printf("origin sin = %f\n", sin(t));
  56.   printf("pi = %.40f\n", s21_PI);
  57.   printf("origin pi = %.40f\n", M_PI);
  58.   printf("1000000*pi = %.40f\n", s21_PI*100);
  59.   return 0;
  60. }
  61.  
  62. long int s21_abs(int x) {
  63.   if (x < 0) {
  64.     x = x * (-1);
  65.   }
  66.   return x;
  67. }
  68. long double s21_fabs(double x) {
  69.   if (x < 0) {
  70.     x = x * (-1);
  71.   }
  72.   return (long double)x;
  73. }
  74.  
  75. long double s21_floor(double x) {
  76.   x = (long double)(int)x;
  77.   if (x < 0) x -= 1;
  78.   return x;
  79. }
  80.  
  81. long double s21_exp(double x) {
  82.   double res = 0;
  83.   for (int k = 0; k < 200; ++k) {
  84.     double current = int_pow(x, k);
  85.     for (int j = 1; j <= k; ++j) {
  86.       current /= j;
  87.     }
  88.     res += current;
  89.   }
  90.   return res;
  91. }
  92.  
  93. double int_pow(double x, int n) {
  94.   double res = 1;
  95.   for (int i = 0; i < n; i++) {
  96.     res = res * x;
  97.     // printf("%");
  98.   }
  99.   return res;
  100. }
  101.  
  102. long long int factorial(int x) {
  103.   long long int res = 0;
  104.   if (x == 0)
  105.     res = 1;
  106.   else
  107.     res = x * factorial(x - 1);
  108.  
  109.   return res;
  110. }
  111.  
  112. long double s21_sin(double x) {
  113.   x = shift_2pi(x);
  114.   long double res = 0;
  115.   for (int k = 0; k < 300; k++) {
  116.     // long double current = pow(x, 2 * k + 1);
  117.     long double current = 1;
  118.     for (int j = 1; j <= 2 * k + 1; ++j) {
  119.       current *= x;
  120.       current /= (j);
  121.       // printf("%Lf\n", current);
  122.     }
  123.     res += pow(-1, k) * current;
  124.     printf("%Lf\n", res);
  125.   }
  126.   return res;
  127. }
  128.  
  129. double shift_2pi(long double x) {
  130.     long double origin_x = x;
  131.     printf("%Lf\n", x);
  132.     // while (x > s21_PI_11) {
  133.     //     x -= s21_PI_11;
  134.     //     printf("11$ %Lf\n", x);
  135.     // }
  136.     // while (x > s21_PI_10) {
  137.     //     x -= s21_PI_10;
  138.     //     printf("10$ %Lf\n", x);
  139.     // }
  140.     // while (x > s21_PI_9) {
  141.     //     x -= s21_PI_9;
  142.     //     printf("9$ %Lf\n", x);
  143.     // }
  144.     while (x > s21_PI_8) {
  145.         x -= s21_PI_8;
  146.         //printf("8$ %Lf\n", x);
  147.     }
  148.     while (x > s21_PI_7) {
  149.         x -= s21_PI_7;
  150.         //printf("7$ %Lf\n", x);
  151.     }
  152.     while (x > s21_PI_6) {
  153.         x -= s21_PI_6;
  154.         //printf("6$ %Lf\n", x);
  155.     }
  156.     while (x > s21_PI_5) {
  157.         x -= s21_PI_5;
  158.         printf("5$ %Lf\n", x);
  159.     }
  160.     while (x > s21_PI_4) {
  161.         x -= s21_PI_4;
  162.         printf("4$ %Lf\n", x);
  163.     }
  164.     while (x > s21_PI_3) {
  165.         x -= s21_PI_3;
  166.         printf("3$ %Lf\n", x);
  167.     }
  168.     while (x > s21_PI_2) {
  169.         x -= s21_PI_2;
  170.         printf("2$ %Lf\n", x);
  171.     }
  172.     while (x > 2*s21_PI)
  173.         x -= 2*s21_PI;
  174.     while (x < 0)
  175.         x += 2*s21_PI;
  176.     if (origin_x > 2*M_PI)   // это не нужно будет, просто сравнивал, как отрабатывает свой s21_PI с M_PI
  177.         while (origin_x > 2*M_PI)
  178.             origin_x -= 2*M_PI;
  179.     else if (origin_x < 0)
  180.         while (origin_x < 0)
  181.             origin_x += 2*M_PI;
  182.     printf("x after proccesing = %.55Lf\n", x);
  183.     printf("origin x = %.55Lf\n", origin_x);
  184.     return x;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement