Hacker News new | past | comments | ask | show | jobs | submit login

Euclidian division actually gives -7 = -4*2 + 1



It’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

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


Yes, and this is not Euclidean division in the usual sense.


Indeed. The C99 and C11 standards define integer division as the “algebraic quotient with any fractional part discarded”, not as Euclidean division.


This whole thread is a nice review and excersize of the 2nd chapter of my Computers and networks security course.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: