Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using mod operator in iOS app

I have an NSTimeInterval that is stored as a double. I would like to get the amount of minutes that are inide of the second value using the % operator.

minutes = secondValue % 60;

where minutes is declared as double minutes

The result is that XCode says "Invalid operands to binary %"

Thoughts?

like image 606
Jerry Avatar asked Sep 16 '10 22:09

Jerry


3 Answers

From the C standard, section 6.5.5 Multiplicative operators, paragraph 2:

The operands of the % operator shall have integer type.

like image 111
Carl Norum Avatar answered Sep 24 '22 16:09

Carl Norum


The OP changed their question, so here is my new answer:

You want to do minutes = floor(secondsValue) / 60; You want int division, not modulus.

like image 12
AlcubierreDrive Avatar answered Sep 22 '22 16:09

AlcubierreDrive


If secondValue is a float or double, you can use C type casting:

minutes = ((int)secondValue) % 60;

EDIT:

(If secondValue is too big to fit in an int, this won't work. So you need to know that the range of your value is appropriate.)

like image 4
hotpaw2 Avatar answered Sep 23 '22 16:09

hotpaw2