开发者

float is getting mangled when passing between methods (typecasting problem?)

开发者 https://www.devze.com 2022-12-15 18:52 出处:网络
I\'m having trouble pas开发者_开发知识库sing 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 p

I'm having trouble pas开发者_开发知识库sing 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.


!! 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号