Advertisement
greedydev

Untitled

Oct 12th, 2022
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. namespace myfuncs {
  2.     const double PI2 = M_PI * 2;
  3.     // calculate sin using taylor series
  4.     // D(y) = R
  5.     double sin(double x) {
  6.         while (abs(x) > PI2) {
  7.             x += x > 0 ? -PI2 : PI2;
  8.         }
  9.         double result = 0;
  10.         double term = x;
  11.         int i = 1;
  12.         while (term != 0) {
  13.             result += term;
  14.             term *= -1 * x * x / (2 * i * (2 * i + 1));
  15.             i++;
  16.         }
  17.         return result;
  18.     }
  19.  
  20.     // calculate cos using taylor series
  21.     // D(y) = R
  22.     double cos(double x) {
  23.         while (abs(x) > PI2) {
  24.             x += x > 0 ? -PI2 : PI2;
  25.         }
  26.         double result = 0;
  27.         double term = 1;
  28.         int i = 1;
  29.         while (term != 0) {
  30.             result += term;
  31.             term *= -1 * x * x / (2 * i * (2 * i - 1));
  32.             i++;
  33.         }
  34.         return result;
  35.     }
  36.  
  37.     // calculate pow
  38.     double pow(double x, double y) {
  39.         double result = 1;
  40.         if (y == 0) {
  41.             return 1;
  42.         }
  43.         if (y < 0) {
  44.             x = 1 / x;
  45.             y = -y;
  46.         }
  47.         while (y > 0) {
  48.             result *= x;
  49.             y--;
  50.         }
  51.         return result;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement