floating point - What rules dictate how Python floats are rounded? -


floating state

Python documentation

0.1

0,1000000000000000055511151231257827021181583404541015625

This is more useful than the maximum number of digits, so the Python keeps the number of digits appearing instead of one round valve

0.1

What are the surrounding rules that float for performance and who do not? I have encountered some strange situations where

1.1 + 2.2 returns 3.3000000000000003 (unrounded)

but

1.0 + 2.3 returns 3.3 (rounded)

I know that the decimal module exists to make these things consistent, but it is curious that which determines the sphere in the floats.

What are the rules that floats are scored for display and who do not? I have encountered some strange situations where

1.1 + 2.2 returns 3.3000000000000003 (unrounded)

but

1.0 + 2.3 returns 3.3 (rounded)

The part of the explanation is definitely that 1.1 + 2.2 and 1.0 + 2.3 is the output of different floating point numbers, and the share of the details for that 1.1 is not actually 11 / 10, 2.2 is not really 22/10, and of course the floating point + is not even a rational addition.

Many modern programming languages, most recently including dragon variation, display a double-specific floating-point value d The numbers show, re-parsing as a double , again as a result for converting at d :

  1. is a floating-point value that is Rs. 3.3 In print. Two can not be, because they have to be the same by definition application, and at least one because if you change the decimal presentation to 3.3 in double , then you get a

  2. The values ​​are rounded for the purpose of showing them.

  3. with decimal digits, but they are otherwise those numbers which are left. Then there are rules about some "rules" that you are asking for that floating-point operations are round. It is very easy but you should be easy for it. If you look at decimal presentations, then the spherical looks random (but not so).

  4. The compact representation in numbers is only in binary, exact values ​​can take several decimal digits to represent properly. "3.3000000000000003" "unrounded" It's just that one is not the goal for more points than "3.3", in particular, represents just the number of all digits required its neighbor (the one that "3.3" that Double precision to distinguish). They are in fact following numbers respectively:

 3.29999999999999982236431605997495353221893310546875 3.300000000000000266453525910037569701671600341796875 

Two, 33/10 nearest the past, prior to "3.3" as can be printed latter can not be printed as "3.3", and neither is it "3.30" can be printed, "3.300", ..., "are equal to all these representations since 3.300000000000000" and Floating Point Number 3,29999999999999982236431605997495353221893310546875 Pars to Back. So it is to be printed as "3.3000000000000003", where 3 is obtained because 6 after the number 2


Comments