This question already has answers here:
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed last year.
Is there any difference between int(a//x) and int(a/x) in python3 if both a and x are integers.
Recently I got a wrong answer in a contest when I used int(a/x) but my code was accepted when I used int(a//x) .
x, y = 3, 4
print(int(x/y))
print(x//y)
returns
0
0
However:
x, y = -2, 4
print(int(x/y))
print(x//y)
returns
0
-1
So yes. In case one of your input variables is negative an integer, the output of your variable differs.
int(a/x) cuts off the decimals (truncates the number). It doesn't actually do division in the intfunction.
a//x divides to floor (rounds down). It uses the BINARY_FLOOR_DIVIDE in bytecode.
Related
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 2 years ago.
So i have a problem where i'm trying to do a division of 20-digit number.
my code:
result = 61519713751187780547/3
print(result)
print(int(result))
output:
2.0506571250395927e+19
20506571250395926528
expected output of 61519713751187780547/3 is:
20506571250395926849
#calculated using full precision calculator at https://www.mathsisfun.com/calculator-precision.html
Im very confused on what's wrong or did i do anything wrong. The difference between the output and the expected output is as much as 300+.
The number is divisible by 3 so it should gives an integer result. But somehow, python gives a float result.
Any help will be appreciated. Thanks in advance!
In Python 3, / is float division and // is integer division
61519713751187780547//3
Out[3]: 20506571250395926849
This question already has answers here:
Integer division & modulo operation with negative operands in Python
(4 answers)
Closed 2 years ago.
i have a question about division of negative number in python3
when I do the calculation for 6//(-132) why it is -1 intead of 0 .
what is the mechanism that python deal with this division?
In fact, you're not using Python 3.x, you're using Python 2.x and what you see is the result of integer division, which was the default back then:
>>> 1/(-2)
-1
>>> -1/2
-1
>>> -23/45
-1
To get the expected results in Python 2.x, try this:
from __future__ import division
>>> 1/(-2)
-0.5
>>> -1/2
-0.5
>>> -23/45
-0.5111111111111111
Now, for the question after the edit: When you do integer division // the value will be rounded down toward the negative value of -1. (this is also known as "floor division"). See this post for further details.
>>> 6 / (-132)
-0.045454545454545456
>>> 6 // (-132)
-1
In the remainder operation the python always takes the floor or the greatest integer function as the answer
Well most probably you are not using python 3 because in my idle python 3.8 on 1/-2 it shows -0.5 and the same in case of -1/2
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Why does the following code return the following output?
def z(x, y):
return (x + 2*y + 1)**2
delta = 0.1
x_derivative = (z(delta, 0) - z(0, 0)) / delta
print(x_derivative)
Output: 2.100000000000002
If my maths is correct, it should just be 2.1.
This has probably be asked before, but I don't know the right terms to search for.
This is due to the representation of floating point numbers internally that causes errors of precisions like that one.
0.1 in binary has an infinite representation (much like 1/3 in base 10), so that's why you get that deviation.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
when i try to add 1 to answer python adds 10^-16 to the answer tried printing in different ways but i think it will not help. Any answers ?
x = 0.8475
print(1 + x)
print(1 + 0.8475)
print(1.0 + 0.8475)
y = 1 + x
print(y)
output :
1.8475000000000001
1.8475000000000001
1.8475000000000001
1.8475000000000001
Its called a floating point error, and isn't specific to python.
A float can't represent all values perfectly accurately, so you get these weird inaccuracies like you're seeing.
Use Decimal instead of float if you need your numbers to be that accurate. In most cases though, its acceptable to just round your answer to a few decimal places.
https://docs.python.org/2/library/decimal.html
This question already has answers here:
How to format a floating number to fixed width in Python
(10 answers)
Closed 6 years ago.
I'm making a percentage finder, pretty basic. But, I would only like to print 4 characters in the float or int. Can anyone help?
Here is my code so far:
numer = int(input("What's the numerator?"))
denom = int(input("What's the denominator?"))
percent = (numer / denom * 100, "%")
print(percent)
I want the output for 5/6 to be:
83.33%
But it actually prints:
83.33333333333334%
you can use % in print function
print("%.2f"%(a/b))