Boost logo

Boost :

From: Boost (boost_at_[hidden])
Date: 2004-02-12 13:18:17


| Subject: Lexical_cast improvement not included in 1_31 release
|
| I proposed some time ago an improvement to lexical_cast to
| deal with the corner cases where there were insufficient
| decimal digits to deal with some float values. For example,
| a 'loopback' test should work for any type T and any value l:
|
| template <typename T> bool test(T l)
| {
| cout << l << ' ';
| string sl = lexical_cast<string>(l);
| cout << sl << ' ';
| T ll = lexical_cast<T >(sl);
| cout << ll << ' '
| << (l == lexical_cast<T>(lexical_cast<string>(l)))
| // Convert type T to string and back to T.
| << endl; // eg for long 2147483647 2147483647 2147483647 true
| return (l == lexical_cast<T>(lexical_cast<string>(l)));
| } // test
|
| The current version fails this test.
|
| There seemed no dissent at the time to calculating the
| significant digits using std::numeric_limits<Target>::digits10 +1.
|
| But alas my suggestion (below) has not been included in the
| latest release.
|
| lexical_stream()
| {
| stream.unsetf(std::ios::skipws);
| // was:
| /*if(std::numeric_limits<Target>::is_specialized)
| stream.precision(std::numeric_limits<Target>::digits10 +1 );
| else if(std::numeric_limits<Source>::is_specialized)
| stream.precision(std::numeric_limits<Source>::digits10 +1 );*/
|
| Suggested:
|
| // New calculation to give more accurate number of
| significant decimal digits:
| // 32 significand bits
| digits10 = 6 significant_digits10 = 9
| // 53 significand bits
| digits10 = 15 significant_digits10 = 17
| // 64 significand bits
| digits10 = 18 significant_digits10 = 21
| // 106 significand bits
| digits10 = 31 significant_digits10 = 33
| // 113 significand bits
| digits10 = 33 significant_digits10 = 36
| // 128 significand bits
| digits10 = 38 significant_digits10 = 40
|
| if(std::numeric_limits<Target>::is_specialized &&
| std::numeric_limits<Target>::radix == 2) {
| stream.precision(2 + std::numeric_limits<Target>::digits *
| 301/1000); } else
| if(std::numeric_limits<Source>::is_specialized &&
| std::numeric_limits<Source>::radix == 2) {
| stream.precision(2 + std::numeric_limits<Source>::digits *
| 301/1000); } // Else if not specialized or unusual radix then
| use default stream precision, probably 6. // All builtin
| types, bool, char, integer and floating point, should be
| specialized. } // lexical_stream()
|
| It is also irritating that current version produces a long
| list of spurious 'error' messages which can avoided by
| inheriting from boost noncopyable (suggested by Reece Dunn).
|
| What should I do to progress this?
|
| Prepare a Boost test for lexical_cast using the Boost Test framework?
|
| Paul
|
| Paul A Bristow,
| Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB
| +44 1539 561830 +44 7714 330204
| mailto: 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