2

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.

Mark Dickinson
31.3k9 gold badges92 silver badges130 bronze badges
asked Jun 9, 2016 at 8:07
1

1 Answer 1

4

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

answered Jun 9, 2016 at 8:15
Sign up to request clarification or add additional context in comments.

2 Comments

If this is a general problem, maybe, I could remove the Python bit from the question.
It's fairly general, except that the exact semantics of the 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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.