How to make a modulo operation in objective-c / cocoa touch?

IphoneModulo

Iphone Problem Overview


I have two CGFloat values, and want to calculate the modulo result. Or in other words: I want to know what's left if valueA is placed as much as possible into valueB.

So I just tried:

CGFloat moduloResult = valueB % valueA;

the compiler complains about the % and tells me: "invalid operands to binary %". Any idea?

Iphone Solutions


Solution 1 - Iphone

% is for int or long, not float or double.

You can use fmod() or fmodf() from <math.h> instead.

Better is <tgmath.h> as suggested by the inventor of CGFloat.

Solution 2 - Iphone

If I remember correctly modulo requires 2 ints as its input so you'd need something like:

CGFloat moduloResult = (float)((int)valueB % (int)valueA);

Assuming that valueB and valueA are both floats

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHelloMoonView Question on Stackoverflow
Solution 1 - IphonemouvicielView Answer on Stackoverflow
Solution 2 - IphoneJamesView Answer on Stackoverflow