views:

113

answers:

1

I'm having trouble passing a float value from one object to another. It appears to be fine in the first method, but in the second its value is huge. I assume this is some kind of a problem with my typecasting, because that's the thing I understand the poorest. Help is greatly appreciated!

In my game controller, I do this:

float accuracy = (float)hitCount/(float)(hitCount+missCount);
NSLog(@"GameController - hits: %i misses: %i enemies: %i accuracy: %f", hitCount, missCount, escapedCount, accuracy);
[delegate postGameWithScore:roundScore andAccuracy:accuracy];

Which invokes this method in the game controller's delegate:

-(void)postGameWithScore:(NSInteger)score andAccuracy:(float)accuracy {
    cumulativeScore += score;
    NSLog(@"GameMaster - score: %i accuracy %f",cumulativeScore, accuracy);
    /* non relevant code clipped */
}

Output:

GameController - hits: 14 misses: 54 enemies: 35 accuracy: 0.205882
GameMaster - score: 3800 accuracy 36893488147419103232.000000

I can't figure out why accuracy is not correct in the second NSLog.

+1  A: 

!! solved it.

Adding this to my delegate's header fixed it:

-(void)postGameWithScore:(NSInteger)score andAccuracy:(float)accuracy;

Don't know why this fixed it, but that'll teach me to treat compiler warnings as warnings instead of errors.

Kenny Winker
If you don't specify the header, the values will be all passed as integers (but for some reason it becomes 0x60000000). Converting that into to `float` representations gives the garbage (3.68935e+19).
KennyTM
There is a build setting in Xcode named “Treat Warnings as Errors”. Turn that on. While you're at it, here's a bunch of other warnings you should turn on: http://boredzo.org/blog/archives/2009-11-07/warnings
Peter Hosey