Issue1778207
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2007-08-20 23:20 by jim_hurlburt, last changed 2022-04-11 14:56 by admin. This issue is now closed.
Messages (3) | |||
---|---|---|---|
msg32670 - (view) | Author: Jim Hurlburt (jim_hurlburt) | Date: 2007-08-20 23:20 | |
Sirs: Using python to generate a text file for transfer of data between systems, I seem to be getting inconsistent rounding results using a text formatting string Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32 print "%9.4f" % (229.0 * .325,) -> 74.4250 correct print "%9.2f" % (round(74.425, 2),) -> 74.43 correct print "%9.2f" % (74.425,) -> 74.42 wrong print "%9.2f" % (167.255,) -> 167.26 correct print "%9.2f" % (167.2551,) -> 167.26 correct print "%9.2f" % (167.2549,) -> 167.25 correct It appears as if the string formatting code is a bit flakey. Perhaps working in binary with too small a precision? For a bit I thought it might be "Round even up, odd down at the .005 break, but that doesn't appear to be the case. Now that I know it's there, I can do a workaround. Seems as if it deserves a fix or at least a document notation of the problem. Thanks in advance, Jim Hurlburt Atrium Windows and Doors Yakima, WA |
|||
msg32671 - (view) | Author: Mark Dickinson (mark.dickinson) * | Date: 2007-08-21 01:02 | |
This isn't really a bug---it's just one of those unavoidable things that happens when you're representing decimals using binary floating point: take a look at section 4.3 of the General Python FAQ: http://www.python.org/doc/faq/general/ If anything *is* wrong here it's actually the round() result, not the string formatting: 74.425 isn't exactly representable as a binary floating-point number, so Python (like most other languages) approximates it by the closest number that *is* exactly representable, which is (on my machine and probably yours too): 74.4249999999999971578290569595992565155029296875 Since this is less than 74.425, the round function would, in an ideal world, round this down to 74.42 instead of up to 74.43. In the absence of an ideal world, making this sort of thing happen reliably and portably is hard, and I'd guess that round() is unlikely to change. Have you looked at the decimal module in the standard library? It sounds as though you might find it useful. |
|||
msg32672 - (view) | Author: Martin v. Löwis (loewis) * | Date: 2007-08-22 06:01 | |
As marketdickinson explains, this is not a bug - at least the line you consider "wrong" does not show a bug. 74.425 is indeed smaller than 74+425/1000, so it's correct that rounding rounds down. As for documentation, please take a look at http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate Closing as invalid. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:56:26 | admin | set | github: 45332 |
2007-08-20 23:20:21 | jim_hurlburt | create |