NSNumber
is useful for storage in NSArray
or NSDictionary
, or in a Core Data store.
You don't want to use NSNumber
for arithmetic, only for use with foundation or Core Data classes when you need to store that number somewhere.
This is because you will have to constantly convert between NSNumber
and primitive types like int
, double
, long
, etc. when performing arithmetical operations. Do the operations with the primitive types to the desired accuracy, and then create an NSNumber
instance, if you really need one.
Nonetheless, it is fairly trivial to use the C math library in any Cocoa/Cocoa Touch app:
#import <math.h>
...
double _param = 7.389;
NSNumber *_result = [NSNumber numberWithDouble:log(_param)];
This is the same story for NSDecimalNumber
, which is a subclass of NSNumber
:
You might consider the C interface if you don’t need to treat decimal numbers as objects—that is, if you don’t need to store them in an object-oriented collection like an instance of NSArray or NSDictionary. You might also consider the C interface if you need maximum efficiency. The C interface is faster and uses less memory than the NSDecimalNumber class.