From: Paul A Bristow (pbristow_at_[hidden])
Date: 2008-06-03 10:59:12
>[mailto:boost-bounces_at_[hidden]] On Behalf Of Hervé Brönnimann
>Sent: 02 June 2008 04:03
>Subject: Re: [boost] Rounding, Truncating Values
>Dear Tom: I'm not even sure I understand the contract. How can you
>round to a number of decimal places when the result is not
>necessarily representable? Rather than an implementation, could you
>provide an example of a documentation, stating precisely what the
>return value should be? Also, if the intention is float/double/long
>double, why templates, as opposed to three overloads? Thanks for any
>Equally useful as rounding, perhaps, would be printing to a string
>with guaranteed rounding. You know that printing or reading a binary
>floating point to a decimal string representation, and back, may not
>result in the same number. This goes with printf and scanf, as well
>as I/O streams (perhaps this has been corrected but I haven't seen
>anything very sophisticated in the STL implementations I lurk in).
>There are known algorithms to do this with guaranteed rounding, e.g.:
>- Guy L. Steele , Jon L. White, How to print floating-point numbers
>accurately, ACM SIGPLAN Notices, v.39 n.4, April 2004
>- William D. Clinger, How to read floating point numbers accurately,
>ACM SIGPLAN Notices, v.39 n.4, April 2004
>(Both papers are old, 1990 and 1991, but the more recent reference/
>reprint may have added information, or may be more accessible.) Also:
>- Robert G. Burger , R. Kent Dybvig, Printing floating-point numbers
>quickly and accurately, ACM SIGPLAN Notices, v.31 n.5, p.108-116, May
>Now those would be very nice to have. It appears that numeric_cast
>library would be the natural host for your proposed functionality,
>and lexical_cast library would be the natural host for the one I'm
>asking for above. It doesn't seem that anything like this is in
>those libraries, though, I rechecked both ToC. Is there anything
>like this in boost?
This issue has been raised before after a user noted that round-tripping via serialisation or lexical_cast did not work - always :-(
The key issue is whether conversion (input) from a decimal digit string of sufficient length (17 for 64-but double for example)
should *always* give the *nearest representable* double.
The Standard isn't explicit about this, but the papers you mention show that it can be done, and other systems than MS manage this.
I don't think Microsoft's response shows that they understand this (except that they apparently have a C# function that DOES
For my money, this is a simple off by one bug, but I'm not ploughing though the Dinkumware iostreams code to find where!
If you don't meet this requirement, I don't think you can't expect any round tripping to work.
I've glanced at the Burger and Dybvig algorithms you quote, but they don't seem too simple to implement. If any one can get they to
work, they would be most valuable.
--- Paul A Bristow Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB +44 1539561830 & SMS, Mobile +44 7714 330204 & SMS pbristow_at_[hidden]
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk