+ 3
Why does 1000/1.6 and 1000//1.6 give a different answer?
If 1.6 goes into 1000 evenly 625 times, why does the floor division equal 624.0? Can someone explain please? https://code.sololearn.com/c1Ov8fuYB5q0/?ref=app
4 Antworten
+ 8
Hi! It’s division vs floor division. It’s two different operators. Division always return a float. Floor division return an integer if both operands are integers, otherwise a float.
The nature of floats makes them more difficult to handle comparing with integers and calculation with them gives results that’s not always one hundred percent mathematically accurate. Python gives that 1000 // 1.6 = 624.0
but 1000 // 1.599999999999999 = 625.0
You can take back what you’ve lost by using the following formula:
k = d * (k // d) + (k % d), where k = 1000 and d = 1.6.
1000 =
= 1.6 * (1000 // 1.6) + (1000 % 1.6) =
= (1.6 * 624.0) + (1000 % 1.6) =
= (998.4000000000001 + 1.5999999999999446) =
= 1000.0
So what you miss above, is in the term (1000 % 1.6).
+ 4
1.6 cannot be exactly rappresented as a float. The nearest rappresentation is a slightly higher number, so it doesn't evenly divide 1000
+ 2
NEZ I understood the difference between the two. My issue was that 1.6 technically goes into 1000 evenly, 625 times. So it was odd to me that the floor division equated to 624.0.
- 3
Go to the course and learn the difference between / and //