Bugs item #1778207, was opened at 2007-08-20 23:20 Message generated for change (Comment added) made by marketdickinson You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1778207&group_id=5470
Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Python Interpreter Core Group: Python 2.4 Status: Open Resolution: None Priority: 5 Private: No Submitted By: Jim Hurlburt (jim_hurlburt) Assigned to: Nobody/Anonymous (nobody) Summary: rounding inconsisntency using string formatting Initial Comment: 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 ---------------------------------------------------------------------- Comment By: Mark Dickinson (marketdickinson) Date: 2007-08-21 01:02 Message: Logged In: YES user_id=703403 Originator: NO 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. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1778207&group_id=5470 _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com