abhi.shah88
abhi.shah88

Reputation: 193

Why is pow() reporting an infinite value in this case?

I am using the pow() function like this in my application:

float  IValuePlusOne =  (pow(2.25,120));

but it is reporting back an infinite value, instead of the correct result. I even tried the long long and long double data types but couldn't get them to produce the proper output.

Is there any other data type I need to use or do I need to make some other changes in my code?

Upvotes: 2

Views: 212

Answers (2)

Brad Larson
Brad Larson

Reputation: 170317

As others have pointed out, you're losing precision and reducing the size of value you can represent by casting to a float. Running the following code:

double IValuePlusOne = pow(2.25,120.0);
NSLog(@"Test value: %f", IValuePlusOne);

on my iPhone gives the output:

Test value: 1827688475348373523156051712429585892114432.000000

which looks to be correct (1.827x10^42).

Upvotes: 4

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

If you want to do calculations on values that a double can't hold, use NSDecimalNumber.

Upvotes: 1

Related Questions