开发者

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 开发者_Go百科do I need to make some other changes in my code?


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).


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜