iosobjective-cinfinitycgfloat

CGFloat Returning +Inf


I'm calculating the percentage of a number like so:

CGFloat total = 9;
CGFloat onePercent = total / 100;

CGFloat otherNum = 2;
CGFloat otherNumPercentage = otherNum / onePercent;

The result should be 22.2 repeating. However, it returns +Inf, which I believe is infinity.

I didn't think it would be treated as infinity as 22-23 certainly doesn't count for infinity, however is it because of the forever recurring .2?

Not sure how to handle this, how can I get my expected result?


Solution

  • When you use constant numbers i.e. 9 you need to specify 9.0f to indicate its a float rather than an int