In Python and Ruby, signed integer division truncates towards negative infinity, and signed integer modulus has the same sign the second operand:
>>> (-41) / 3
-14
>>> (-41) % 3
1
However, in C and Java, signed integer division truncates towards 0, and signed integer modulus has the same sign as the first operand:
printf("%d
", (-41) / 3); /* prints "-13" */
printf("%d
", (-41) % 3); /* prints "-2" */
What is the simplest and most efficient way in C to perform the same kind of division and modulus as in Python and Ruby?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…