This question already has answers here:
Why does Python's modulus operator (%) not match the Euclidean definition?
(1 answer)
Floor division with negative number
(4 answers)
Closed 3 years ago.
In my mind, the answer should be -1. Because 3//2 is equal to 1. Can someone explain why my console outputs -2?
value = -3//2
print(value)
I'm assuming this is python and // is integer division
In python integer division // rounds down (left on the number line) to the nearest integer
-3/2 outputs a float -1.5 not integer division so no rounding
-3//2 outputs an integer -2 rounds down(left) to the nearest integer on the number line
If this is python, then // is known as floor division, and it is an operator that rounds the result down to the nearest whole number.
Therefore
-3/2 = -1,5 rounded down to the nearest whole number is equal to -2.
Therefore:
-3//2 = -2.
Given that the red line is where -1,5 is, by rounding down (this means moving always left in the axis) the closest number is -2, which is what the operator does. It does not round to the closest number to 0, because that would mean it is rounding up, since it would be moving right in the axis.
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.
This question already has answers here:
How does the modulo (%) operator work on negative numbers in Python?
(12 answers)
Closed last month.
I found this definition for modulo
mod(a, n) = a - n * floor(a / n)
Doing an integer division and then multiplying it again means finding the biggest number smaller than a that is dividable by n without a remainder. Subtracting this from a yields the remainder of the division and by that the modulo."
source:https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/
It seemed to help me understand intuitively, and it worked for cases where both a and n where positive and also cases where a was negative while b was positive. For example:
-10 mod 3
-12 is the largest number smaller than -10 divisible by 3. So the remainder is -10-(-12)=2 which is correct.
But when I tried doing 10 mod -3 my answer was incorrect. Because while 9 is the largest number smaller than 10 divisible by -3 it will give the wrong answer as it would evaluate to 10-9=1.
Instead it is supposed to be 10 -((-4)*(-3))=10-12=-2.
But 12 isn't the biggest number smaller than a.
I understand how to solve for the answer using the formula a= (a//n)(n)+(a%n) in python. But is there an intuitive definition to help me understand?
I found an anser here https://en.wikipedia.org/wiki/Remainder
it seems there are two types of remainders and a programming languages picks which one to use
The Python 3 documentation gives the identity x == (x//y)*y + (x%y), from which it can be deduced that x%y == x - (x//y)*y. // denotes “floor division”; x//y is the greatest integer not greater than x/y.
For x=10 and y==−3, x/y is −3⅓. floor(−3⅓) is −4. Therefore x//y is −4. Then x - (x//y)*y is 10 − (−4)*−3 = 10−12 = −2.
Here is a graph of x%3:
Here is a graph of x%-3:
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
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.