I have a question about performing of division with remainder of integers in C/C++. It is said that in operation of division between two integers, if the result can not be expressed as an integer, its part that is not integer are removed in decimal. For example, int a=17; a/=3; /pseudo float a, gets the value of 5.6666../ so the result is 5. This is result of a normal division with remainder in arithmetic at the same time due to the part after point(6666..) which is actually division of the remainder(2) by 3. This works on my compter but is it definitely reliable or I have to declare with float and then cast to int with floor for safe? Which is better for perfonmance and safe? Thanks in advance.
3 Answers
Arithmetic operation on integers in C++ does not depend of the computer.
If a
and b
are integers, a / b
will always give you the integer quotient of the division and a % b
will always give you the remainder of the integer division.
In terms of performance, you can take a look at this StackOverflow question, but it seems to be architecture dependant.
You should use a / b
and a % b
for integer division and remainder. As Levans says, these are guaranteed to give you the "correct" values independent of your hardware (at least if a
and b
are positive). With floating point arithmetic, the result can be affected by rounding errors, which can also be hardware-dependent.

- 3,282
- 2
- 22
- 22
So, instead of integer modulo, you can get two floats then multiply one float with inverse of the divider :
17.0 * 0.33 = 5.61
then floor() it into an integer and subtract :
5.61 - 5 ----> 0.61
then multiply the result with inverse of 0.33:
0.61 * 3 ------> 1.83
then ceil() it
2 ----> this is 17%3
This is 14 times slower than using direct modulus, according to user "Oseiskar " 's benchmarking.

- 11,469
- 4
- 45
- 97
-
what makes you think this is faster or better than `a % b` in any way? – oseiskar Jul 14 '13 at 12:13
-
I said "could be" did not bench it. – huseyin tugrul buyukisik Jul 14 '13 at 12:14
-
Example, 65536 % 255 can be hundreds of cycles. But up here are only floor and ceiling functions take some time. Others only multiplications and a subtraction. – huseyin tugrul buyukisik Jul 14 '13 at 12:16
-
in C `ceil` and `floor` are (non-inlined) function calls that will indeed take some time. I briefly timed this by attempting to run `total += i % mod;` and `total += (int)ceil(i - floor(i * (1.0/mod)) * mod);` in a loop for all i < 1000000 and the latter was approx. 14 times slower. – oseiskar Jul 14 '13 at 12:39
-
What was the mod? Did you select it big enough and smaller than i ? Was it power of two? – huseyin tugrul buyukisik Jul 14 '13 at 12:41
-
the result is fairly independent of `mod`. I used 12437788 in the above (sorry: I used i < 100000000, not one million) – oseiskar Jul 14 '13 at 12:42
-
So it was always smaller than that. Which is directly equal to value itself always. Can you try with 255 too ? – huseyin tugrul buyukisik Jul 14 '13 at 12:43
-
I tried also 255 and the result did not change. Also I'd like to note that your comment about not using constants in divisions does not make much sense either, because the compiler will quite certainly do this kind of optimizations automatically. – oseiskar Jul 14 '13 at 12:47
-
Okay, thank you for benchmarking. Then Im changing the answer in your name. – huseyin tugrul buyukisik Jul 14 '13 at 12:48