The expression 6 // 4 yields 1, where floor division produces the whole number after dividing a number.
But with a negative number, why does -6 // 4 return -2?
The // operator explicitly floors the result. Quoting the Binary arithmetic operations documentation:
the result is that of mathematical division with the ‘floor’ function applied to the result.
Flooring is not the same thing as rounding to 0; flooring always moves to the lower integer value. See the math.floor() function:
Return the floor of x, the largest integer less than or equal to x.
For -6 // 4, first the result of -6 / 4 is calculated, so -1.5. Flooring then moves to the lower integer value, so -2.
If you want to round towards zero instead, you'll have to do so explicitly; you could do this with the int() function on true division:
>>> int(-6 / 4)
-1
int() removes the decimal portion, so always rounds towards zero instead.
Floor division will also round down to the next lowest number, not the next lowest absolute value.
6 // 4 = 1.5, which rounds down to 1, and up to 2.
-6 // 4 = -1.5, which rounds down to -2, and up to -1.
// in Python is a "floor division" operator. That means that the result of such division is the floor of the result of regular division (performed with / operator).
The floor of the given number is the biggest integer smaller than the this number. For example
7 / 2 = 3.5 so 7 // 2 = floor of 3.5 = 3.
For negative numbers it is less intuitive: -7 / 2 = -3.5, so -7 // 2 = floor of -3.5 = -4. Similarly -1 // 10 = floor of -0.1 = -1.
// is defined to do the same thing as math.floor(): return the largest integer value less than or equal to the floating-point result. Zero is not less than or equal to -0.1.
A useful way to understand why floor division // yields the results it does for negative values is to consider that it complements the modulo, or remainder, % operator, which in Python is defined to always return a non-negative number.
5/3 is equivalent to 1 remainder 2
i.e.
5//3 = 1
5%3 = 2
But
-5/3 = -2
-5%3 = 1
Or
-2 + 1/3rd which is -1.6667 (ish)
It can seem strange, but it ensures results such as
-2,-2,-2,-1,-1,-1,0,0,0,1,1,1,2,2,2,3,3,3 etc. when generating sequences.
Related
This question already has answers here:
Negative integer division surprising result
(5 answers)
Why is -1/2 evaluated to 0 in C++, but -1 in Python?
(4 answers)
Closed last month.
I found that -1 // 2 is equal to -1 (Why not 0?), but int(-1 / 2) is equal to 0 (as I expected).
It's not the case with 1 instead of -1, so both 1 // 2 and int(1 / 2) is equal to 0.
Why the results are different for -1?
In Python, the division operator / and the floor division operator // have different behavior.
The division operator / returns a floating-point number that represents the exact quotient of the division. In the case of -1/2, the quotient is -0.5. When you cast it to int, it rounds the number up to 0.
The floor division operator // returns the quotient of the division rounded down to the nearest integer. In the case of -1//2, the quotient is -1, because -1 divided by 2 is -0.5, which is rounded down to -1.
That's why -1//2 = -1 and int(-1/2) = 0 in python.
For example,
int result;
result = 125/100;
or
result = 43/100;
Will result always be the floor of the division? What is the defined behavior?
Will result always be the floor of the division? What is the defined behavior?
Not quite. It rounds toward 0, rather than flooring.
6.5.5 Multiplicative operators
6 When integers are divided, the result of the / operator is the algebraic quotient with any
fractional part discarded.88) If the quotient a/b is representable, the expression
(a/b)*b + a%b shall equal a.
and the corresponding footnote:
This is often called ‘‘truncation toward zero’’.
Of course two points to note are:
3 The usual arithmetic conversions are performed on the operands.
and:
5 The result of the / operator is the
quotient from the division of the
first operand by the second; the
result of the % operator is the
remainder. In both operations, if the
value of the second operand is zero,
the behavior is undefined.
[Note: Emphasis mine]
Dirkgently gives an excellent description of integer division in C99, but you should also know that in C89 integer division with a negative operand has an implementation-defined direction.
From the ANSI C draft (3.3.5):
If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or the smallest integer greater than the algebraic quotient is implementation-defined, as is the sign of the result of the % operator. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.
So watch out with negative numbers when you are stuck with a C89 compiler.
It's a fun fact that C99 chose truncation towards zero because that was how FORTRAN did it. See this message on comp.std.c.
Yes, the result is always truncated towards zero. It will round towards the smallest absolute value.
-5 / 2 = -2
5 / 2 = 2
For unsigned and non-negative signed values, this is the same as floor (rounding towards -Infinity).
Where the result is negative, C truncates towards 0 rather than flooring - I learnt this reading about why Python integer division always floors here: Why Python's Integer Division Floors
Will result always be the floor of the division?
No. The result varies, but variation happens only for negative values.
What is the defined behavior?
To make it clear floor rounds towards negative infinity,while integer division rounds towards zero (truncates)
For positive values they are the same
int integerDivisionResultPositive= 125/100;//= 1
double flooringResultPositive= floor(125.0/100.0);//=1.0
For negative value this is different
int integerDivisionResultNegative= -125/100;//=-1
double flooringResultNegative= floor(-125.0/100.0);//=-2.0
I know people have answered your question but in layman terms:
5 / 2 = 2 //since both 5 and 2 are integers and integers division always truncates decimals
5.0 / 2 or 5 / 2.0 or 5.0 /2.0 = 2.5 //here either 5 or 2 or both has decimal hence the quotient you will get will be in decimal.
Recently, I read a book on Numpy which mentions different types of ufuncs, where I was encountered with two different ufuncs, namely 'modulus', denoted by % symbol and 'floor division' //. Can someone explain the difference between them and why two operators are provided to do the the same thing (display reminder of division, according to me)? Please correct, if I am wrong.
Both are valid mathematical functions with different results.
modulus
The modulus-function computes the remainder of a division, which is the "leftover" of an integral division.
floor
The floor-function provides the lower-bound of an integral division. The upper-bound is computed by the ceil function. (Basically speaking, the floor-function cuts off all decimals).
a=5
b=2
print(a%b) # Prints 1 as leftover
print(a//b) # Prints 2, since 5/2=2.5 and the decimal is cut off
print(a - (a//b)*b) # Prints 1, this is the modulo, calculated by the floor function
Assume a= 10, b = 6
a%b will give you the remainder, that is 4
a//b will give you the quotient, that is 1
The relationship between floor division and modulus may be helpful for understanding this:
denominator = 6 # replace with any integer
numerator = 10 # replace with any integer
floor_division_result = numerator//denominator
modulo_result = numerator % denominator
# this assertion will always be true:
assert denominator * floor_division_result + modulo_result == numerator
One way to think of the difference between modulus and floor division is to use an analogy of a clock face
Imagine a hand on this clock. It moves around and points at a number. Let's say a cycle starts (and ends) when the hand points at 12. In this context
modulus, %, is where the hand is pointing right now
floor division is the number of times the hand has completed a cycle
Example 1
In the first cycle when the hand points at 4 the modulus is 4 but the floor division is 0.
4 % 12 # returns 4
4 // 12 # returns 0
Example 2
In the second cycle when the hand points at 4 the modulus is still 4 but the floor division is now 1
16 % 12 # returns 4
16 // 12 # returns 1
What is the algorithmic difference between math.ceil() and round() when trailing decimal points are >= 0.5 in Python 3?
For example,
round(9.5) = 10
round(9.67) = 10
math.ceil(9.5) = 10
math.ceil(9.5) = 10
From the docs,
[...] if two multiples are equally close, rounding is done toward the even
choice (so, for example, both round(0.5) and round(-0.5) are 0, and
round(1.5) is 2).
However, math.ceil will always "round" up. I.e. the smallest integer greater than or equal to the input.
Moreover, round and math.ceil differ greatly when executing on negative numbers.
>>> math.ceil(-2.8)
-2
>>> round(-2.8)
-3
Why does -103/100 == -2 but 103/100 == 1 in Python? I can't seem to understand why.
Integer division always rounds down (towards negative infinity).
Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the floor1 function applied to the result.
http://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations
This allows for the integer division and modulo (remainder, %) operators to connect nicely through the identity x == (x/y)*y + (x%y).
1 floor(x) is the largest integer not greater than x.
Integer division takes (I believe) the floor() of whatever float comes out, more or less.
So that's -2 for the first division and 1 for the second.