views:

376

answers:

5

OK this may be the dumb question of the day, but supposing I have a class with :

NSDecimalNumber *numOne   = [NSDecimalNumber numberWithFloat:1.0];
NSDecimalNumber *numTwo   = [NSDecimalNumber numberWithFloat:2.0];
NSDecimalNumber *numThree = [NSDecimalNumber numberWithFloat:3.0];

Why can't I have a function that adds those numbers:

- (NSDecimalNumber *)addThem {
    return (self.numOne + self.numTwo + self.numThree);
}

I apologize in advance for being an idiot, and thanks!

+1  A: 
[[numOne decimalNumberByAdding:numTwo] decimalNumberByAdding:numThree]
newacct
+4  A: 

You can't do what you want becuase neither C nor Objective C have operator overloading. Instead you have to write:

- (NSDecimalNumber *)addThem {
    return [self.numOne decimalNumberByAdding:
        [self.numTwo decimalNumberByAdding:self.numThree]];
}

If you're willing to play dirty with Objective-C++ (rename your source to .mm), then you could write:

NSDecimalNumber *operator + (NSDecimalNumber *a, NSDecimalNumber *b) {
    return [a decimalNumberByAdding:b];
}

Now you can write:

- (NSDecimalNumber *)addThem {
    return self.numOne + self.numTwo + self.numThree;
}

Go C++!

Frank Krueger
+5  A: 

See NSDecimalAdd function (as well as NSDecimalMultiply, NSDecimalDivide, NSDecimalSubtract).

Vladimir
Note that these functions work on NSDecimal structs, not NSDecimalNumber objects, but you can easily extract an NSDecimal using NSDecimalNumber's -decimalValue method.
Brad Larson
That said, this is the approach I recommend. The NSDecimal functions are up to five times faster than the NSDecimalNumber equivalents, and you avoid creating autoreleased objects all over the place, yet you maintain the decimal arithmetic and precision of NSDecimalNumber. I use NSDecimal instead of NSDecimalNumber where I can in my applications.
Brad Larson
+1  A: 

You can:

- (NSDecimalNumber *)addThem {
    return [[self.numOne decimalNumberByAdding:numTwo] decimalNumberByAdding:numThree];
}

The problem with your example is that what self.numOne really is at a bit level is a pointer to an object. So your function would return some random memory location, not the sum.

If Objective-C supported C++-style operator overloading, someone could define + when applied to two NSDecimalNumber objects as an alias to decimalNumberByAdding:. But it doesn't.

Frank Schmitt
+4  A: 

NSDecimalNumber is an Objective C class which, when instantiated, produces an object which contains a number. You access the object (and objects in general) through methods only. Objective C doesn't have a way to directly express arithmetic against objects, so you need to make one of three calls:

  • -[NSDecimalNumber doubleValue], which extracts the numbers from your objects before adding the numbers. link
  • -[NSDecimalNumber decimalNumberByAdding], which creates an additional object, which may be more than you want. link
  • NSDecimalAdd, which, again, creates an additional object, which may be more than you want. link
Integer Poet
By the way, I'm surprised nobody has mentioned this, but...I think your first burst of sample code is broken. You appear to be calling the NSNumber class in order to instantiate NSDecimalNumber. I've never seen such a thing before and I'm suspicious of it. You might get away with it for a while due to the "late binding" nature of Objective C, but it seems like it will eventually cause bugs.
Integer Poet
"you can think of the later options as built atop the earlier ones" - This is not true in the first case. NSDecimalNumbers perform decimal math, not floating point, so extracting a double and adding that will eliminate the benefits of working with an NSDecimalNumber. Also, NSDecimalAdd() is a function that works on an NSDecimal struct, not an NSDecimalNumber object.
Brad Larson
Poor choice of words on my part. In fact, the entire chain of logic makes less sense to me now than when I wrote it, since NSDecimalAdd operates on NSDecimal rather than NSDecimalNumber. I've removed the statement. I was trying to show inter-releatedness but took it too far.
Integer Poet