Code keeps returning nan

Write your question here.
I was assigned to create a cose function by hand and must also go to the 50th term. For some reason anytime I increase the amount terms over a certain all I get as a return is nan. Anyone know why that is or have a solution to this issue? (im on a mac btw)

Put the code you need help with here.

#include <iostream>
#include <cmath> // for comparision with actual pow
// and cos functions

using namespace std;

// function prototypes
int fact(int num);
double power(double base, int exponent);
double cosine(double x);

int main()
{
// calling fact function
cout << "fact(5) = " << fact(5) << endl;
cout << endl;

// calling power function using both
// custom power function and pow function
// from cmath library
cout << "power(2.5,6) = " << power(2.5,6) << endl;
cout << "pow(2.5,6) = " << pow(2.5,6) << endl;
cout << "power(-1.2,7) = " << power(-1.2,7) << endl;
cout << "pow(-1.2,7) = " << pow(-1.2,7) << endl;
cout << endl;

// calling cosine function using both
// custom cosine function and cos function
// from cmath library
cout << "cosine(3.14) = " << cosine(3.14) << endl;
cout << "cos(3.14) = " << cos(3.14) << endl;

cout << "cosine(3.14/4) = " << cosine(3.14/4) << endl;
cout << "cos(3.14/4) = " << cos(3.14/4) << endl;

cout << "cosine(-3.14/2) = " << cosine(-3.14/2) << endl;
cout << "cos(-3.14/2) = " << cos(-3.14/2) << endl;

return 0;
}

//----------------------------------------------------------
// Function name: fact
// Description: This function computes the factorial
// of the input number
// Input: num = int = input number
// Output: the factorial of the input number as an integer
// Note: be careful about using this function for larger
// values of num (in those cases, it may be better to
// change this function to return a double rather than
// an integer)
//----------------------------------------------------------
int fact(int num)
{
int nfact = 1;

while(num>1)
{
nfact = nfact*num;
num--;
}

return nfact;
}

//----------------------------------------------------------
// Function name: power
// Description: This function computes base^exponent where
// exponent is assumed to be a positive integer
// Input: base = double = base for power computation
// exponent = int = exponent (assumed to be > 0) for
// power computation
// Output: the value of base to the power of the exponent
// (note: if the exponent is not a positive integer
// as assumed, 1 is returned)
//----------------------------------------------------------
double power(double base, int exponent)
{
double output = 1;
for (int i = 1; i <= exponent; i++)
{
output = output*base;
}

return output;
}

//----------------------------------------------------------
// Function name: cosine
// Description: This function computes the cosine of the
// input number using a power series
// Input: x = double = input number
// Output: the value of cosine of x (approximated using the
// first five terms of a power series)
//----------------------------------------------------------
double cosine(double x)
{
double output = 1;
//cout << endl << "Computing 1"; // for debugging purposes

int numTerms = 50;
int sign = -1; // will switch between -1 and 1
int exponent = 2; // 2, 4, 6, 8, ...
for (int i = 1; i < numTerms; i++)
{
//cout << " + " << sign << " * x^" << exponent << "/" << exponent << "!";
output += sign*power(x,exponent)/fact(exponent);

// update exponent and sign for next time through loop
exponent += 2;
sign = -1*sign; // flip sign (-1 --> 1; 1 --> -1)
}

return output;
}
does your code actually support the biggest factorial you attempted to compute? Print that out first, see if that is the trouble. Factorials get HUGE FAST and you run out of space pretty quickly.
Last edited on
This is the problem thank you! Any work around for running out of space like this?
closed account (E0p9LyTq)
Any work around for running out of space like this?

Instead of using (signed) int for the calculations, use unsigned int.

Or unsigned long long for really big factorials.

Can't calculate the factorial of a negative number, and factorials will never be negative, why go with types that are signed.
Thank you! I got it now!
there are a lot of things you 'can' do about the problem including 'infinitely large' home-made integer storage types when you run out of bits in the biggest ints your machine has (128 bits being the current max on bigger modern systems, 64 on most others).

In my experience, though, the simple solution is usually the right one. I cant recall ever needing more than about 10! -- the places you see factorials a lot are in numerical approximations like this one, or probability, a few other places here and there and in all of those, 10 is usually more than enough for practical purposes. 50 terms was probably given in the assignment so you would see this exact issue and handle it, but its excessive. And, for a lot of these kinds of functions … cos/sin/etc are built into the language and CPU math circuits already, and there are other ways to get it if it were not (if nothing else the slope of the line is the tan which is the sin/cos …)
Last edited on
Topic archived. No new replies allowed.