Predicates and Comparing Decimals
Predicates and Comparing Decimals
- Subject: Predicates and Comparing Decimals
- From: Andreas Grosam <email@hidden>
- Date: Wed, 22 Dec 2010 13:18:11 +0100
Hello All!
Suppose I have a Core Data Application, with an entity A with a field "x" of type Decimal. I would like to filter the entities by comparing the value for the field x matching a specific value. The user interface provides a search field and displays a list of the entities.
What would the predicate format string for the search field look like?
One could come up with:
"x = $value"
Surprisingly (or not), this doesn't seem to work! Apparently, there are no matches, even though there exist matching values.
Well, we know that comparing real numbers (that is, IEEE floats) can become more elaborated when it should be done right:
<http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm>
However, if Cocoa uses NSDecimal numbers to perform the predicate (or if this is performed by the storage directly) the comparison algorithm can be much different, more straight forward, and could also be exact.
Possibly, there are a number of other gotchas like loss of precision do to rounding or conversions and limitations of the underlaying types which may occur internally in Cocoa, Core Data and the data store when performing its work.
So eventually, how can I achieve reliably comparing Decimals for equality without adding too much additional code and if possible not using too complex and inefficient predicates?
Thanks for suggestions,
Andreas_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden