c++ - Taylor Series Resulting in nan after sin(90) and cos(120) -


doing school project. not understand why sin comes out -nan when after sin(90) , cos(120).

can me understand this?

also, when put in online c++ editor totally works, when compiled in linux not.

// nick garver // taylorseries // taylorseries.cpp #include <iostream> #include <cmath> #include <iomanip>   using namespace std;  const double pi = atan(1.0)*4.0; double angle_in_degrees = 0; double radians = 0; double degreestoradians(double d); double factorial(double factorial); double mysine(double x); double mycosine(double x);  int main() {     cout << "\033[2j\033[1;1h";     cout.width(4); cout << left << "deg";     cout.width(9); cout << left << "radians";     cout.width(11); cout << left << "realsine";     cout.width(11); cout << left << "mysin";     cout.width(12); cout << left << "realcos";     cout.width(11); cout << left << "mycos"<<endl;      while (angle_in_degrees <= 360) //radian equivalent of 45 degrees     {         double sine = sin(degreestoradians(angle_in_degrees));         double cosine = cos(degreestoradians(angle_in_degrees));         //output         cout.width(4); cout << left << angle_in_degrees;         cout.width(9); cout << left << degreestoradians(angle_in_degrees);         cout.width(11); cout << left << sine;         cout.width(11); cout << left << mysine(degreestoradians(angle_in_degrees));         cout.width(12); cout << left << cosine;         cout.width(11); cout << left << mycosine(degreestoradians(angle_in_degrees))<<endl;         angle_in_degrees = angle_in_degrees + 15;     }     cout << endl;     return 0; }   double degreestoradians(double d) {     double answer;     answer = (d*pi)/180;     return answer; }  double mysine(double x) {     double result = 0;     for(int = 1; <= 1000; i++) {         if (i % 2 == 1)             result += pow(x, * 2 - 1) / factorial(i * 2 - 1);         else             result -= pow(x, * 2 - 1) / factorial(i * 2 - 1);     }     return result; }  double mycosine(double x) {     double positive = 0.0;     double negative= 0.0;     double result=0.0;     (int i=4; i<=1000; i+=4)     {         positive = positive + (pow(x,i) / factorial (i));     }     (int i=2; i<=1000; i+=4)     {         negative = negative + (pow(x,i) / factorial (i));     }     result = (1 - (negative) + (positive));     return result; }  double factorial(double factorial) {     float x = 1;     (float counter = 1; counter <= factorial; counter++)     {         x = x * counter;     }     return x; } 

before answer this, few remarks:

  • it's helpful own debugging keep code tidy. remove unnecessary empty lines, make sure bracketing style uniform, , indent. did you, believe me, you'll avoid lot of bugs if keep consistent style!
  • you have functions take double input , return double, internally use float; should red flag!
  • your whole degreestoradians better read , 1 third long if used return (d*pi)/180;

answers now:

in factorial function, calculate factorial values 1999. hint: try figure out value of 1999! , maximum number float on machine can hold. double's maximum. how many orders of magnitude 1999! larger?

1999! ca. 10^5732. large number, 150 orders of magnitude larger 32bit float can hold, or still 18 orders of magnitude larger 64bit double can hold. compare, store 1999! in double trying fit distance sun center earth center in typical 0.1µm diameter of bacteria.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -