Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mod operator in ios

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

like image 839
nuteron Avatar asked Apr 27 '12 13:04

nuteron


People also ask

What is the mod (%) operator?

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.

What types does the mod (%) work on?

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.

How does modulo work Swift?

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.


2 Answers

You can use fmod (for double) and fmodf (for float) of math.h:

#import <math.h>

rotationAngle = fmodf(rotationAngle, 360.0f);
like image 69
Hailei Avatar answered Nov 03 '22 17:11

Hailei


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.

like image 37
kuba Avatar answered Nov 03 '22 19:11

kuba