I’ve got some C# code that I’m converting to Objective-C. In C# I would call Math.Log(). I’m slowly learning that some people stick to C functions/types and only use NSNumber etc when they need to interop with Cocoa.
Is there an equivalent for ObjC/Cocoa, or do I need to drop into C to do this? I need my code to be as accurate as possible because it is part of a calculator.
This code needs to run on iPhones if that makes any difference.
NSNumberis useful for storage inNSArrayorNSDictionary, or in a Core Data store.You don’t want to use
NSNumberfor 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
NSNumberand primitive types likeint,double,long, etc. when performing arithmetical operations. Do the operations with the primitive types to the desired accuracy, and then create anNSNumberinstance, if you really need one.Nonetheless, it is fairly trivial to use the C math library in any Cocoa/Cocoa Touch app:
This is the same story for
NSDecimalNumber, which is a subclass ofNSNumber: