What's the step by step answer for 5.5//1.1. While running in compiler it gives 4.0 as an answer.
Asked
Active
Viewed 90 times
4
-
Java actually gives 5.0 as the answer. I checked in python, it gives same as what you got! – Ruthvik Nov 15 '21 at 13:04
-
Which platform, CPU architecture and Python runtime? (That said, in general, this comes down to "IEEE floating point is unintuitive to people accustomed to thinking in decimal", which isn't in any way specific to Python) – Charles Duffy Nov 15 '21 at 13:05
-
I tried ``` import math
print(math.floor(5.5/1.1))```. This worked correctly and gave 5 as answer – Ruthvik Nov 15 '21 at 13:08 -
1@Ruthvik OP is concerned about `5.5 // 1.1`, not `5.5 / 1.1` – BatWannaBe Nov 15 '21 at 13:08
-
1@BatWannaBe, I know, but to get the correct answer, i gave this solution – Ruthvik Nov 15 '21 at 13:09
-
1Already answered [here](https://stackoverflow.com/questions/38588815/rounding-errors-in-python-floor-division) – Shanavas M Nov 15 '21 at 13:14
-
1That's because there is no 5.5 or 1.1 in python (floating-point finite representation) it's actually a float like 5.500000000000001 and 1.100000000000001 which makes the floor of the division of to be 4.0. `5.500000000000001//1.100000000000001=4.0` – A l w a y s S u n n y Nov 15 '21 at 13:20
-
1For the record, 5.5 can be accurately represented as a float (it's ``17/2``) but 1.1 cannot (it's ``11/(5*2)``) – its float representation is slightly *larger* than 1.1. Check for example ``f"{1.1:.16f}"``. – MisterMiyagi Nov 15 '21 at 13:26