Advertisement
Vickyyy01

Cosine Calculator

Jan 11th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <climits>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. double Cos(double);
  9. double Fact(int);
  10. double ConvertTo(double);
  11.  
  12. int main()
  13. {
  14.     double degrees;
  15.     cin >> degrees;
  16.  
  17.     if(degrees > 180)
  18.     {
  19.         degrees = ConvertTo(degrees);
  20.     }
  21.  
  22.     if(degrees < 0)
  23.     {
  24.         degrees = degrees*(-1);
  25.     }
  26.  
  27.     double radians = (degrees/180)*3.14159;
  28.  
  29.     cout << setprecision(7) << Cos(radians) << endl;
  30. }
  31.  
  32. double Cos(double number)
  33. {
  34.     double cos = 0;
  35.  
  36.     cout << fixed;
  37.     for(int i = 0; i <= 18; i++)
  38.     {
  39.         cos += (pow(-1,i)*pow(number,(2*i)))/Fact(2*i);
  40.     }
  41.  
  42.     return cos;
  43. }
  44.  
  45. double Fact(int num) //factorial algorithm
  46. {
  47.     double counter = 1;
  48.  
  49.     for(double i = 1; i <= num; i++)
  50.     {
  51.         counter = counter * i;
  52.  
  53.         if(counter >= INT_MAX)
  54.         {
  55.            break;
  56.         }
  57.     }
  58.  
  59.     return counter;
  60. }
  61.  
  62. double ConvertTo(double number) //converts degrees to radians
  63. {
  64.     int num = number/180.0;
  65.     number = number - (int)(num*180);
  66.     number = (double)number;
  67.  
  68.     return number;
  69. }
  70.  
  71. /*
  72. --------------------------------------------------------------------------------------------------------------------------------------
  73. --------------------------------------------------------------------------------------------------------------------------------------
  74. Calculus II Math Exercise - Degrees to Arc Length
  75. --------------------------------------------------------------------------------------------------------------------------------------
  76. --------------------------------------------------------------------------------------------------------------------------------------
  77. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement