I have a program that reads in 5 integers and gives out results of various calculations using those numbers. I am having particular trouble calculating the geometric mean. I am aware that you are supposed to multiply the numbers together and take the nth root of the result.
my code is as follows (assume all #includes and main method are correct.):
int num1, num2, num3, num4, num5;
cout << "Enter five integers: \n";
cin >> num1 >> num2 >> num3 >> num4 >> num5;
double gMean = pow((num1 * num2 * num3 * num4 * num5), (1.0/5.0));
cout << "Geometric mean = " << gMean << endl;
This code works for small numbers, such as 1, 2, 3, 4, 5, but when I input large numbers it gives me nan as the answer.
The numbers I need to work in this are : 85, 43, 95, 100, and 78
My question is: Why does the pow() function give me nan as the answer when the larger numbers are put in but return the correct answers when small numbers are put in?
EDIT: First question answered. Now that I know that I am having overflow issues, how do I go about resolving it?