Python / and //

I am facing a problem in python. Sometimes even after checking that a%b==0, using a/b doesn’t work ( codechef says wrong answer but I don’t know which test case as all sample cases work fine ) whereas using a//b in its place makes the answer correct. What is happening here?
I mean after a%b==0 then aren’t a/b and a//b equivalent?

No. They aren’t equivalent. They are treated differently. / is true division while // is floor division. So answers are different. When working with integers, it is better to use // as it will closely give the results you expect from / when using C++. 5 // 4 will give you 1 while 5 / 4 will give you 1.25 in python. In cpp, when you use int datatype, you get 5 / 4 as 1.

1 Like

a=20,b=2

a/b=10.0
a//b=10
if you print 10.0 instead of 10 it gives WA

1 Like

Precision Errors in Python


Precision Errors costed me 2 WA in March Long. I got to know about this issue after the contest.

this is weird ,
i do code in python 3 but never encountered such issues .
hope we will discuss this issue once contest is over.
@akshitm16 @levio_sar

correct !

correct !

try
a=65768798765456751
print(a/1==a//1)

a/b gives you the value of a divided by b
a//b gives you the Greatest Integer Function or the actual quotient(excluding the remainder) of a and b

For example,
a = 100, b = 8
a/b = 12.5
a//b = 12

Refer
Using Fraction module is better than /

try-1
print(.1 + .1 + .1 == .3)
try-2
from fractions import Fraction
a=12345676543234556
b=12345676543234557
print(a/1==b/1)
print(Fraction(a,1)==Fraction(b,1))

Enjoy!!!