+ 2

Python 5.2

How 1000//1.6=624.0?!

15th Oct 2022, 6:19 PM
saman burburhosseinbeygi
saman burburhosseinbeygi - avatar
7 odpowiedzi
+ 5
the floor division // rounds the result down to the nearest whole number.
15th Oct 2022, 6:31 PM
JaScript
JaScript - avatar
15th Oct 2022, 8:56 PM
Per Bratthammar
Per Bratthammar - avatar
+ 3
I know that but 1000/1.6=625 we dont need to round it
15th Oct 2022, 6:56 PM
saman burburhosseinbeygi
saman burburhosseinbeygi - avatar
+ 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) = }")
16th Oct 2022, 7:16 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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?)
16th Oct 2022, 9:55 PM
Korkunç el Gato
Korkunç el Gato - avatar
0
Hi Per Bratthammar But isnt 1000%1.6=0 correct??
16th Oct 2022, 10:49 AM
saman burburhosseinbeygi
saman burburhosseinbeygi - avatar
0
123
17th Oct 2022, 3:45 PM
郝瑞
郝瑞 - avatar