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
The modulo operator, denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division. produces the remainder when x is divided by y.
3) modulus operator is not just applicable to integral types e.g. byte, short, int, long but also to floating-point types like float and double. 4) You can also use the remainder operator to check if a number is even or odd, or if a year is leap year.
Swift has a dedicated remainder operator in the form of % , and it's used to return the remainder after dividing one number wholly into another. For example, 14 % 3 is 2, because you can fit four 3s into 14, and afterwards you have the remainder 2.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With