3 ms·
Euclidian division actually gives -7 = -4*2 + 1
by naniwaduni 5y ago
Euclidian division actually gives -7 = -4*2 + 1
- strenholme 5y agoIt’s -3 in GCC 11.2.0 main() { printf("%d\n",-7/2);} Let’s look at, oh, C11: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf PDF page 110, physical page 92: When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. with a footnote: This is often called ‘‘truncation toward zero’’ -7/2 is -3.5, so discarding the fractional part gives us -3
- naniwaduni 5y agoYes, and this is not Euclidean division in the usual sense.
- strenholme 5y agoIndeed. The C99 and C11 standards define integer division as the “algebraic quotient with any fractional part discarded”, not as Euclidean division.
- GreenWatermelon 5y agoThis whole thread is a nice review and excersize of the 2nd chapter of my Computers and networks security course.