NSNumber计算&精确? [英] NSNumber Calculations & precision?

查看:162
本文介绍了NSNumber计算&精确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个快速问题,如果我可以,这是我应该如何采取两个NSNumber对象,执行计算,并结束与一个结果也是一个NSNumber?

Two quick questions if I may, is this how I should go about taking two NSNumber objects, performing a calculation and ending up with a result that is also an NSNumber?

NSNumber *flux = [[NSNumber alloc] initWithDouble:100.0];
NSNumber *mass = [[NSNumber alloc] initWithDouble:3];

double intermediate = [flux doubleValue] / [mass doubleValue];
NSLog(@"INTER : %.20f", intermediate);

NSNumber *result = [[NSNumber alloc] initWithDouble:intermediate];
NSLog(@"RESULT: %@", result);

...
...

[flux release];
[mass release];
[result release];

同样从NSLog看看控制台的结果,有没有精度的损失?我会假设没有,我看到的只是显示精度,但只是好奇?

Also looking at the results in console from NSLog, is there any loss of precision? I would assume there is not and what I am seeing is just display precision, but just curious?

INTER : 33.33333333333333570181
RESULT: 33.33333333333334

gary

推荐答案

(与你的问题相切,但是相关)

(Tangential to your question but related)

NSNumber不是用来做基础数学。它在很大程度上包装和存储数值。如果您需要进行真实的数学运算,则需要使用 NSDecimal。

NSNumber isn't intended to do base-10 math with. It's largely there to wrap and store numerical values. If you need to do real math, you want to use a NSDecimal.


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

NSDecimalNumber, an immutable subclass of NSNumber, provides an object-oriented wrapper for doing base-10 arithmetic. An instance can represent any number that can be expressed as mantissa x 10^exponent where mantissa is a decimal integer up to 38 digits long, and exponent is an integer from –128 through 127

尽管我们称之为计算机,我们的逻辑引擎不能做实际的数学所以他们必须伪造它。当你达到非常大或非常小的数字的极端,假的开始显示。这就是为什么你需要自定义数字类,可以保存更多的信息,而不仅仅是一个数字字符串。

Despite the fact that we call them "computers" our logic engines can't do actual math so they have to fake it. When you get to the extremes of very large or very small magnitude numbers, that faking begins to show. That is why you need custom numerical classes that can hold more information than just a string of digits.

因此,如果你对精度有任何顾虑,使用NSDecimal,而不是NSNumber 。 NSDecimal旨在执行精确的计算。

So, if you have any concerns about precision, use NSDecimal instead of NSNumber. NSDecimal is designed to perform precise calculations.

Edit01:


...我应该如何处理两个
NSNumber对象,执行
计算,结果是一个也是NSNumber的
结果。

... how I should go about taking two NSNumber objects, performing a calculation and ending up with a result that is also an NSNumber?

严格地说,你不应该使用NSNumber进行计算。你会注意到NSNumber没有专用的数学方法。你必须转换为标量,然后再转回一个对象。这导致精度的损失,并且精度可以根据硬件或标量的定义而改变。

Strictly speaking, you should not use NSNumber for calculations. You will notice that NSNumber has no dedicated methods for doing math. You have to convert to scalar and then back again to an object. This causes a loss of precision and the precision can change depending on the hardware or the definitions of the scalars.

NSDecimal可以精确地表示非常精确的数字,因为它保持抽象。它有专门的方法来执行精确的数学运算。

NSDecimal by contrast can precisely represent very precise numbers because it holds them abstractly. It has dedicated methods for performing precise mathematical operations.


同时查看控制台中的结果
从NSLog,是有任何损失
precision?

Also looking at the results in console from NSLog, is there any loss of precision?

是的,除了格式化之外,数学精度的损失。标量具有不同的精度,这取决于它们存储的数量的类型和大小。在大幅度,这导致精度问题。如果你混合类型,说NSInteger和NSUInteger,你得到NSInteger的最大精度。

Yes, there is a loss of mathematical precision beyond just the formatting. Scalars have different precision depending on their type and size of the number they store.. At large magnitudes, this causes problems with precision. If you mix types, say a NSInteger and a NSUInteger, you get the maximal precision of the NSInteger.

您还会遇到所有使用标量的旧问题。


如果你使用无法保存
的类型向NSNumber对象申请
值,那么你会得到错误的
结果 - 例如,如果你要求
使用大于FLT_MAX,

的整数值的
创建的数字的浮点值使用大于
的float创建的
NSInteger的最大值。

If you ask an NSNumber object for its value using a type that cannot hold the value, you get back an erroneous result—for example, if you ask for the float value of a number created with a double that is greater than FLT_MAX, or the integer value of a number created with a float that is greater than the maximum value of NSInteger.

NSDecimal可以释放所有这些可能的错误源。它进行精确的数学计算,达到超越任何人在现实世界中使用的程度。

NSDecimal frees you from all these possible sources of error. It does precise mathematical calculations up to magnitudes way beyond what anyone would use in the real world.

我重复一次:如果精度是一个问题,不要使用NSNumber或标量。

I repeat: If precision is a concern, don't use NSNumber or scalar.

这篇关于NSNumber计算&精确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆