I have trouble understanding why python returns different results for these 2 statements:
-1 // 3/4
and -1 // 0.75
The first one returns -0.25
and the second on returns -2
.
The way i understand it, the /
operator is executed before //
, thus those 2 statements should have the same result.
edit: I was referring to a document provided by my university. I misinterpreted that. Official python documentation proves me wrong. Thanks for all the quick answers.
The real floor division operator is “//”. It returns the floor value for both integer and floating-point arguments.
Floor division is a normal division operation except that it returns the largest possible integer. This integer is either less than or equal to the normal division result. Floor function is mathematically denoted by this ⌊ ⌋ symbol.
Right Answer is: The division operator is performed on two operands. This means that we get an exact division result. For example: print(9/4.0) produces 2.25. The floor division performs an integer division.
The logical-AND operator ( && ) has higher precedence than the logical-OR operator ( || ), so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- .
The /
and //
operators have the same precedence according to the documentation so they are evaluated from left to right when used in the same expression. -1 // 3/4
is therefore equivalent to (-1 // 3)/4
rather than -1 // (3/4)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With