Lanmonster
Lanmonster

Reputation: 93

Why does the pow function give me nan as an answer?

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?

Upvotes: 3

Views: 6617

Answers (4)

It&#39;sPete
It&#39;sPete

Reputation: 5211

You are overflowing what a double can store. The numbers are too large and cause the doubles you are inputting to overflow.

Also, you can check for these issues based on some events that occur during errors as described in the documentation here: http://en.cppreference.com/w/cpp/numeric/math/pow

Edited for clarity: pow() takes a double as it's input, so when you multiply all those ints together, you can cause an overflow when the result is typecast to a double. Also, the math itself can cause the overflow.

Upvotes: 1

AnT stands with Russia
AnT stands with Russia

Reputation: 320571

The problem is not in the pow. The expression

num1 * num2 * num3 * num4 * num5

is the culprit by itself. If you look at the resultant value in debugger you will probably see some meaningless negative value. This is what makes pow fail. If fails with domain error if the first argument is negative and the second argument is not an integer.

The product of 85, 43, 95, 100, and 78 does not fit into the range of int on your platform. It overflows and leads to undefined behavior. This is what you observe.

Evaluate the value of that expression as

(double) num1 * num2 * num3 * num4 * num5

and your pow should give a more meaningful result.

Upvotes: 2

Mitch Wheat
Mitch Wheat

Reputation: 300599

To (possibly) avoid the overflow, re-write as

double gMean = pow(num1, (1.0/5.0)) *
               pow(num2, (1.0/5.0)) *
               pow(num3, (1.0/5.0)) *
               pow(num4, (1.0/5.0)) *
               pow(num5, (1.0/5.0)) 

Upvotes: 2

Manoj Awasthi
Manoj Awasthi

Reputation: 3520

From the man page of pow(x,y):

   If x is a finite value less than 0, and y is a finite noninteger, 
a domain error occurs, and a NaN is returned. 

   Except as specified below, if x or y is a NaN, the result is a NaN.

   If x is negative, then large negative or positive y values yield a NaN 
as the function result, with  errno  set  to  EDOM,  and  an  invalid
   (FE_INVALID)  floating-point  exception.  For example, with pow(), 
one sees this behavior when the absolute value of y is greater than about
   9.223373e18.

So looks like first case in your case.

Upvotes: 6

Related Questions