NSDecimalNumber,当提升到高功率时,分数返回大数?

时间:2013-11-06 00:45:57

标签: ios iphone objective-c math nsdecimalnumber

好的,所以我试图将这个小数量提高到高功率。但由于某些原因,根据日志我得到一个非常高的数字,有时甚至NSDecimalOverflor错误。有人可以帮我这个吗?

NSDecimalNumber *number = [[NSDecimalNumber alloc] initWithDouble:0.4];
 NSInteger power = 1001;

number = [number decimalNumberByRaisingToPower:power];

NSLog(@"%@",number);

也许我在数量太小的情况下达到某种限制?什么都有帮助!

编辑:哦,堆栈跟踪显示所有数字在实际上下文中断时都是相同的。

1 个答案:

答案 0 :(得分:1)

来自Apple文档:

   NSDecimalNumber,一个NSNumber的不可变子类,提供了一个   用于执行base-10算术的面向对象的包装器。一个实例可以   表示可以表示为尾数x 10 ^指数的任何数字   其中尾数是一个十进制整数,长度不超过38位,指数   是-128到127之间的整数

所以有一个限制,考虑浮点double

0.4 ^ 1001:
4E-399

0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

比NSDecimalNumber可以处理的大得多。