On 10/11/2016 8:59 PM, Skip Montanaro wrote:
I'm trying to port some code from Python 2 to 3, and beyond the usual
mechanical stuff, I've encountered a difference between the str() of
floats. Here's an example. In Python 3 I get:
print(repr(27.04 - 0.01))
27.029999999999998
print(str(27.04 - 0.01))
27.029999999999998
but in Python 2 str() behaves differently:
print repr(27.04 - 0.01)
27.029999999999998
print str(27.04 - 0.01)
27.03
My test case writes through a csv writer, which writes the str() of each
element to the output. My test assertions are looking at the output, not
the numeric input, so the option to test if the number is "close enough"
isn't readily available. I suppose I should adjust my test case, as in this
case 27.04-0.01 is different than 27.03.
Or you can make the text version-specific.
Basing tests on strings generated by Python is fragile because they are
subject to change. For instance, 3.1 has a new algorithm for
repr(float), discussed in What's New in 3.1. The repr for other objects
can and does change with new versions, as to exception messages. (They
are only changed in bugfix releases if there is a real and significant
but.)
Is there documentation of this particular change? My searching turned up
documentation of plenty of other changes, but not this particular one.
There was discussed on the development lists. I believe this change was
in 3.0, in which case there would be no mention in the 3.x docs. I did
not see mention in What New in 3.0, though. The change would certainly
be in the NEWS file that lists all commits. This is now reformatted
into an html changelog linked from What's New, but this started after in
3.0.
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list