On Sun, May 1, 2016 at 5:43 AM, Steven D'Aprano <st...@pearwood.info> wrote: > On Sun, May 01, 2016 at 01:02:50AM -0500, boB Stepp wrote: >> Life has kept me from Python studies since March, but now I resume. >> Playing around in the interpreter I tried: >> >> py3: 1.9999999999999999 >> 2.0 >> py3: 1.999999999999999 >> 1.999999999999999
[...] > Starting with Python 2.6, floats have "hex" and "fromhex" methods which > allow you to convert them to and from base 16, which is more compact > than the base 2 used internally but otherwise equivalent. > > https://docs.python.org/2/library/stdtypes.html#float.hex I had not read of these methods yet. Thanks! [...] > Given that we only have 64 bits for a float, and some of them are used > for the exponent and the sign, it is invariable that conversions to and > from decimal must be inexact. Remember that I mentioned that both > 1.9999999999999997 and 1.9999999999999998 are treated as the same float? > That is because a 64-bit binary float does not have enough binary > decimal places to distinguish them. You would need more than 64 bits to > tell them apart. And so, following the IEEE-754 standard (the best > practice for floating point arithmetic), both numbers are rounded to the > nearest possible float. Just before bed, I was looking at the Wikipedia article on IEEE-754. But it was not clear from it which of the "optional" rounding methods were being used by Python, though the behavior I was observing suggested rounding to nearest. [...] >> It has been many years since I did problems in converting decimal to >> binary representation (Shades of two's-complement!), but I am under >> the (apparently mistaken!) impression that in these 0.999...999 >> situations that the floating point representation should not go "up" >> in value to the next integer representation. > > In ancient days, by which I mean the earlier than the 1980s, ... Heavy sigh! I last explicitly did binary arithmetic in an Intro to C. Sc. class in 1975. So perhaps my incorrect expectation of *not* rounding up is an artifact of that long ago era. Thank you Steve, and Eryk, for your excellent explanations! I really appreciate the time and depth both of you put into your answers to my questions. boB _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor