have been searching for a mod operator in ios, just like the %
in c, but no luck in finding it. Tried the answer in this link but it gives the same error.
I have a float variable 'rotationAngle' whose angle keeps incrementing or decrementing based on the users finger movement.
Some thing like this:
if (startPoint.x < pt.x) {
if (pt.y<936/2)
rotationAngle += pt.x - startPoint.x;
else
rotationAngle += startPoint.x - pt.x;
}
rotationAngle = (rotationAngle % 360);
}
I just need to make sure that the rotationAngle doesnot cross the +/- 360 limit.
Any help any body.
Thanks
You can use fmod
(for double
) and fmodf
(for float
) of math.h:
#import <math.h>
rotationAngle = fmodf(rotationAngle, 360.0f);
Use the fmod
function, which does a floation-point modulo, for definition see here: http://www.cplusplus.com/reference/clibrary/cmath/fmod/. Examples of how it works (with the return values):
fmodf(100, 360); // 100
fmodf(300, 360); // 300
fmodf(500, 360); // 140
fmodf(1600, 360); // 160
fmodf(-100, 360); // -100
fmodf(-300, 360); // -300
fmodf(-500, 360); // -140
fmodf
takes "float" as arguments, fmod
takes "double" and fmodl
takes "double long", but they all do the same thing.
I cast it to an int first
rotationAngle = (((int)rotationAngle) % 360);
if you want more accuracy use
float t = rotationAngle-((int)rotationAngle);
rotationAngle = (((int)rotationAngle) % 360);
rotationAngle+=t;