round(1.4 999 999 999 999 999) (without the spaces) gets rounded to 2
but
round(1.4 99 999 999 999 999) (without the spaces) gets rounded to 1.
I suppose this has to do with imprecise floating point representations, but fail to understand how does it come that the first representation is interpreted as closer to 2 than to 1.
-
1docs.python.org/2/tutorial/floatingpoint.html#tut-fp-issuesAmin Alaee– Amin Alaee2016年06月09日 08:15:16 +00:00Commented Jun 9, 2016 at 8:15
1 Answer 1
Because 1.4 999 999 999 999 999 when parsed is exactly 1.5, the difference between them is too small to represent at that magnitude.
But 1.4 99 999 999 999 999 is low enough to parse to "less than 1.5", actually 1.4999999999999988897769753748434595763683319091796875, which is clearly less than 1.5
2 Comments
round operation differ from language to language: in Python, round is correctly rounded; that's not necessarily going to be true for all languages. OTOH, this is more to do with floating-point representation issues than the rounding operation itself.