简体   繁体   English

NSSet:我应该在NSSet中使用float / double吗?

[英]NSSet: should I use float/double in NSSet?

As we know, there is float number precision problem when comparing float numbers. 众所周知,比较浮点数时会出现浮点数精度问题。

Also that NSSet uses equality compare to keep elements unique in it. 此外,NSSet使用相等比较来保持元素唯一。

So what happens when NSSet stocks NSValues that have float/double in it, 那么当NSSet存在具有浮点/双精度的NSValues时会发生什么,

Should we never use float in NSSet to avoid float precision problem? 我们是否应该在NSSet中使用float来避免浮点精度问题?

there is float number precision problem when comparing float numbers. 比较浮点数时存在浮点数精度问题。

To be precise, only comparison for equality presents a problem. 确切地说,只有平等的比较才会出现问题。 Unfortunately, that's the kind of comparison done by NSSet . 不幸的是,这是NSSet所做的那种比较。

So what happens when NSSet stocks NSValues that have float / double in it? 那么当NSSet存在具有float / double NSValues时会发生什么呢?

It treats numbers that are really close to each other but not exactly equal as different items in the set. 它处理的数字彼此非常接近,但与集合中的不同项目不完全相同。 That is why one should be extremely careful when using floating point numbers in NSSet . 这就是为什么在NSSet使用浮点数时应该非常小心。 Ideally, you should avoid it if you can. 理想情况下,如果可以的话,你应该避免它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM