Vickyyy01

Sine Calculator

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