0

Floor division with decimals

Hi, I understand how floor division works with whole numbers but I have come a across a question where one of the numbers in the question is a decimal eg 5//0.5. The answer is 10 but I am struggling to understand how to get to the answer. Is something changed in the question because of the decimal? A breakdown would be great. Thanks in advance.

24th Oct 2018, 6:46 AM
Thomas Hemming
14 Réponses
+ 3
A) Convert the arguments to a common type--float in this case. So 5 becomes 5.0 B) Perform regular division. So 5.0/0.5 = 10.0 C) Take the floor of the result in B; that is, the integer closest to 10.0 but not bigger than 10.0. That's still 10.0. (Interestingly, type conversion is not done here, so it's the float 10.0, and not the int 10.) Another example: 5//1.6 = 3.0 A) 5 becomes 5.0 B) 5.0/1.6 = 3.125 C) Biggest integer not exceeding 3.125 is 3.0 Be careful with negatives: -5//1.6 = -4.0 Because biggest integer not exceeding -3.125 is -4.0 Further reading: https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations
24th Oct 2018, 7:16 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Kishalaya Saha it just clicked after a bit of a google. the numbers divided to get 10 are actually 50 and 5 because the decimal needs to move on both numbers so they are both whole numbers. this was the part i was confused with. thank tou for your help
24th Oct 2018, 11:47 AM
Thomas Hemming
+ 1
Kishalaya Saha That was exactly it. Yes, the rest of it makes perfect sense. Thanks again for you’re help on this 👍
24th Oct 2018, 11:59 AM
Thomas Hemming
+ 1
That's because in a while loop, the condition is checked at the beginning of each loop. What happens in the intermediate stages is not registered. So when a is 10, we still enter the loop. But then it becomes 12, and since it's divisible by 4, the number is printed. Then we go back to the beginning of the loop again, and see that a is no longer <=10, so we exit the loop. So the sequence is like this a=0. While condition true. Enter loop. a=2. If condition false. Beginning of loop. While condition true. a=4. If condition true. Print 4. Beginning of loop. While condition true. a=6. If condition false. Beginning of loop. While condition true. a=8. If condition true. Print 8. Beginning of loop. While condition true. a=10. If condition false. Beginning of loop. While condition true. a=12. If condition true. Print 12. Beginning of loop. While condition false. Exit loop. Does that make sense?
26th Oct 2018, 12:07 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Kishalaya Saha still a little confused but i think i kind of get it. so for the lat loop, a is still 10 when entering the loop, the becomes 12, then gets printed. when the loop starts again, a=12 resulting in the condition not being met
26th Oct 2018, 3:21 PM
Thomas Hemming
+ 1
Precisely! The checks are only performed at the entry point. a was 10 when the last loop started, so it was good. It became 12 in the loop body, and was printed. When a code confuses me, I put a whole bunch of print statements to keep track of what's happening at every stage. Something like a = 0 while a <= 10: print("Loop body begins. a =", a) a = a + 2 if (a % 4 == 0): print(a) print("End of loop body. a =", a) print("Exited loop. a =", a) Then, when I understand the code, I delete those print statements :) Hope that helps.
26th Oct 2018, 4:19 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Kishalaya Saha thats it. thanks for your help on this one
26th Oct 2018, 4:25 PM
Thomas Hemming
+ 1
Kishalaya Saha thats a really good way to check snd learn the code. thanks
26th Oct 2018, 4:36 PM
Thomas Hemming
0
Kishalaya Saha thanks for the response. I’m still a little confused with you’re answer though. where does the 5.5 come from? sorry for the wuestion i just want to understand the theory behind this. Thanks in advance
24th Oct 2018, 11:32 AM
Thomas Hemming
0
Sorry, it's my bad. There were typos. I have fixed them, I think 😂
24th Oct 2018, 11:37 AM
Kishalaya Saha
Kishalaya Saha - avatar
0
Thomas Hemming okay, so you were confused about how the regular division of 5.0 by 0.5 gives 10? I see. Sorry I didn't explain that. I was too focused on the floor business 😂 Is the rest of the logic of floor division clear to you? Let me know if something still seems confusing.
24th Oct 2018, 11:57 AM
Kishalaya Saha
Kishalaya Saha - avatar
0
You're welcome, my friend 😊
24th Oct 2018, 12:02 PM
Kishalaya Saha
Kishalaya Saha - avatar
0
Kishalaya Saha this is a bit off topic but i was wondering if you could explain something to me. the last number to be printed from the following code is 12, however the while loop states to run when equal or less than 10. how does it get to 12? what am i missing? a = 0 while a <= 10: a = a + 2 if (a % 4 == 0): print(a)
26th Oct 2018, 11:57 AM
Thomas Hemming
0
No worries, I'm glad I could help :)
26th Oct 2018, 4:32 PM
Kishalaya Saha
Kishalaya Saha - avatar