From: Coleman, M. <MK...@St...> - 2006-09-20 21:44:53
|
> Brian Pratt: > > ...with properly implemented numeric I/O > > routines (in libc), you can have a 1-1 mapping between the=20 > > internal and > > ASCII representation, so that it is possible to round trip without > > introducing error.=20 >=20 > Well, no, but this is something a lot of folks don't realize.=20 > For (a previously cited by Randy) example consider "0.1" - see > http://www.yoda.arachsys.com/csharp/floatingpoint.html for an=20 > explanation. I think we're talking about two different things. As you say, 0.1 does not have an exact IEEE 754 representation. I'm talking about conversion between decimal and IEEE 754. Intuitively, for each IEEE 754 double, there are a set of decimal numbers closer to it than to any other double. Of that set, one will have the shortest decimal representation, after all trailing zeros have been truncated. (There may be two, which is handled by round-to-even.) This representation can in turn be uniquely mapped back to the double. I think that something like this is specified by IEEE 754, but I can't find an exact reference on the web. Java specifies this: =20 http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html#toString(d ouble) and here's a discussion that seems to reference it =09 http://mail.python.org/pipermail/python-dev/2004-March/043742.html I probably don't have the details exactly right, but I believe the basic idea is correct. The effect of this is that it is possible to use a decimal representation without introducing any error. My preference, though, would still be to round away the noise digits. Mike |