Math Problem in Objective C

I’m trying to do some math in an Objective C method, and I’m totally at a loss as to why it’s not working. Here is the code:

-(IBAction)moreSetup:(id)sender{ // Connected to the Curved Average Slider
    NSLog(@"%@",[avgScore class]);
    NSLog(@"%@",[curvedAvg class]);
    NSLog(@"%@",[maxScore class]);
    NSLog(@"%@",[NSNumber numberWithFloat:(maxScore - curvedAvg)]);
    NSLog(@"%@",[NSNumber numberWithFloat:(maxScore - avgScore)]);
   // self.minCrvdStdDev = ((maxScore - curvedAvg)/(maxScore - avgScore)) * stdDev;
   // NSLog(@"CS=%@ max=%@ CA=%@ AveScore=%@ stdDev=%@",minCrvdStdDev,maxScore,curvedAvg,avgScore,stdDev);
   // self.curvedStdDev = minCrvdStdDev;
   // [self getCurved:sender];

…and here is the log:

First thing, how come I need to use “numberWithFloat” to get this to work, since the two number I’m subtracting are NSNumbers or NSCFNumbers (if I don’t put in that in, I get an EXC_BAD_ACCESS error, and I can leave it out if I use %f instead of %@).

Secondly, how come 92-78 = -331040?
I guess my larger question is, what is the best way to do math in Objective C? I have numbers going in and out of dictionaries, so those need to be NSNumbers, but I also need to do various math operations on these numbers.



you can’t do simple math operations with NSNumber objects.
Convert NSNumber to primitives - do math - convert back or use NSDecimalNumber which provides math methods

Thanks Stefan,

I reworked the program, and changed most of my properties from NSNumber pointers to floats or ints. That resulted in a lot fewer conversions back and forth. I’m still a little fuzzy on when to use pointers and when not to. Thanks also for the syntax info for bindings – everything is working fine now.