本文介绍了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不是用来做基础数学。它在很大程度上包装和存储数值。如果您需要进行真实的数学运算,则需要使用

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.

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

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没有专用的数学方法。你必须转换为标量,然后再转回一个对象。这导致精度的损失,并且精度可以根据硬件或标量的定义而改变。

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.

是的,除了格式化之外,数学精度的损失。标量具有不同的精度,这取决于它们存储的数量的类型和大小。在大幅度,这导致精度问题。如果你混合类型,说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.

您还会遇到所有

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计算&精确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 22:32