4 ms·
True, but both Python’s “int()” operation and the C99 and C11 standard for division operators truncate towards 0. [1] [1] e.g. http://www.open-std.org/jtc1/sc2
by strenholme 5y ago
True, but both Python’s “int()” operation and the C99 and C11 standard for division operators truncate towards 0. [1]
[1] e.g. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf PDF page 94/physical page 82
- housecarpenter 5y agoPython's int() truncates towards 0, but (at least in Python 3, I don't know if it was always the same in older versions) its division operator always floors, so -7 // 2 gives -4 even though int(-3.5) is 3. (The int() function is best thought of as just "make this float into an integer, I don't really care how"; if you want to be more specific, you have math.floor(), math.ceil() and math.trunc(), and you also have round(), which rounds towards the closest integer; if the float is evenly between the two integers, it goes towards 0 when the integer part is even and away from 0 when the integer part is odd.)