Jamie Lokier <ja...@shareable.org> writes: > Anthony Liguori wrote: >> After checking that we can demarshal, marshal again and compared to >> the expected decoded value. This doesn't work so well for floats >> because they cannot be accurately represented in decimal but we try >> our best. > > Good sprintf/scanf/strtod implementations do guarantee that what's > printed and then parsed gets back the same floating point value, as > long as you have printed sufficient decimal digits. > > I'm not sure if FLT_DIG/DLB_DIG are the right number of digits, > though. Glibc's documentation of those is confusing and they might > mean something a little different. > > -- Jamie
Yes, conversion fp <-> decimal with sufficient precision can be made exact. However, it's non-trivial[1], and thus neither IEEE-754 nor ISO C require it. IIRC, GNU libc makes it exact. No, FLT_DIG/DBL_DIG is not sufficient precision. You need 7 decimal digits for IEEE single, 17 for IEEE double[2]. FLT_DIG is 6 and DBL_DIG is 15 on my system. An alternative to all that ingenuity is C99's hexadecimal format. [1] Steele & White: How to print floating-point numbers accurately http://portal.acm.org/citation.cfm?id=93559 Clinger: How to Read Floating Point Numbers Accurately, http://portal.acm.org/citation.cfm?id=93557 [2] http://docs.sun.com/source/806-3568/ncg_goldberg.html#1251