+ 2
Python 5.2
How 1000//1.6=624.0?!
7 Respostas
+ 5
the floor division // rounds the result down to the nearest whole number.
+ 4
Hi, saman burburhosseinbeygi !
I wrote about this here:
https://sololearn.com/discuss/2936969/?ref=app
+ 3
I know that but 1000/1.6=625 we dont need to round it
+ 1
# Hi, saman burburhosseinbeygi !
# Here are some results you can look at. It will show that 1000%1.6 != 0.
print(f"{1000 / 1.6 = }")
print(f"{1000 // 1.6 = }")
print(f"{1.6 * (1000 // 1.6) = }")
print(f"{1000 % 1.6 = }")
print(f"{1.6 * (1000 // 1.6) + (1000 % 1.6) = }")
+ 1
https://stackoverflow.com/questions/19473770/how-to-avoid-floating-point-errors
https://stackoverflow.com/questions/46699651/how-to-avoid-floating-point-arithmetics-issues
worst case scenario: the decimal module, also mentioned in the links
Choosing the best data type, formatting etc are methods to avoid it but in your case it looks kind of hard to predict until it happens.
I think the best advice here would be on how to know when it's expected but I have no idea myself or if even such advice exists(maybe other than double check with a calc but what to do when you use loops or recursion with floats in some list or something or map them? - I don't see the experienced people always importing said module in such cases, I think it's the formatting they avoid the issue with?)
0
Hi Per Bratthammar
But isnt 1000%1.6=0 correct??
0
123