Re: [boost] lexical_cast error with double?

If it's broken in 1.33.1, then it's no my fault. I thought I introduced this bug when I increased precision to pass loopback test. I'm sure it's consistent with std::iostream behavior so it's not a bug :( Preston A. Elder wrote:
I tried on 2 platforms:
RedHat AS4 (2.6 kernel), x86 gcc 3.4.4 and boost 1.33.1
Gentoo (2.6 kernel), amd64 gcc 4.1.1 and boost 1.33.1 AND CVS Head
All cases the same result.
On Thu, 2007-05-31 at 15:52 +0400, Alexander Nasonov wrote:
In HEAD or 1.34? which compiler?
-- Alexander Nasonov http://nasonov.blogspot.com Let the river roll which way it will, cities will rise on its banks. -- Ralph Waldo Emerson -- This quote is generated by: /usr/pkg/bin/curl -L http://tinyurl.com/veusy \ | sed -e 's/^document\.write(.//' -e 's/.);$/ --/' \ -e 's/<[^>]*>//g' -e 's/^More quotes from //' \ | fmt | tee ~/.signature-quote

On 6/1/07, Alexander Nasonov <alnsn@yandex.ru> wrote:
I'm sure it's consistent with std::iostream behavior so it's not a bug :(
FWIW, sending the double value to std::cout works as expected: double d = 72.35; std::string s = boost::lexical_cast<std::string>(d); printf("%s\n", s.c_str()); // prints 72.34999999999999 std::cout << d << std::endl; // prints 72.35 Reading 72.35 from std::cin works as expected in both cases: double d = 0.0; std::cin >> d; std::string s = boost::lexical_cast<std::string>(d); printf("%s\n", s.c_str()); // prints 72.35 std::cout << d << std::endl; // prints 72.35 (MSVC8) Jon Jon

Jonathan Franklin wrote:
FWIW, sending the double value to std::cout works as expected: double d = 72.35; std::string s = boost::lexical_cast<std::string>(d); printf("%s\n", s.c_str()); // prints 72.34999999999999 std::cout << d << std::endl; // prints 72.35
because default precision is 6. Try to set it to numeric_limits<double>::digits10 + 1. -- Alexander Nasonov http://nasonov.blogspot.com Even peace may be purchased at too high a price. -- Benjamin Franklin -- This quote is generated by: /usr/pkg/bin/curl -L http://tinyurl.com/veusy \ | sed -e 's/^document\.write(.//' -e 's/.);$/ --/' \ -e 's/<[^>]*>//g' -e 's/^More quotes from //' \ | fmt | tee ~/.signature-quote

On 6/1/07, Alexander Nasonov <alnsn@yandex.ru> wrote:
Jonathan Franklin wrote:
FWIW, sending the double value to std::cout works as expected: double d = 72.35; std::string s = boost::lexical_cast<std::string>(d); printf("%s\n", s.c_str()); // prints 72.34999999999999 std::cout << d << std::endl; // prints 72.35
because default precision is 6. Try to set it to numeric_limits<double>::digits10 + 1.
Oops, good call. std::cout.precision(std::numeric_limits<double>::digits10 + 1); std::cout << d << std::endl; // prints 72.34999999999999 Jon

Not entirely true ;) Its consistent with iostream, but not DEFAULT iostream - only iostream with increased precision. One can accept the same behavior as iostream as not a bug only when they don't have to manipulate iostream (ie. increase precision) to reveal said bug. But another way, one expects ss << d; ss >> s; to work identically to s = lexical_cast<string>(d); They DON'T expect ss.precision(digit10+1); ss << d; ss >> s; to be the equivalent. Just my $.02 :) PreZ On Fri, 01 Jun 2007 22:47:57 +0400, Alexander Nasonov wrote:
If it's broken in 1.33.1, then it's no my fault. I thought I introduced this bug when I increased precision to pass loopback test. I'm sure it's consistent with std::iostream behavior so it's not a bug :(
Preston A. Elder wrote:
I tried on 2 platforms:
RedHat AS4 (2.6 kernel), x86 gcc 3.4.4 and boost 1.33.1
Gentoo (2.6 kernel), amd64 gcc 4.1.1 and boost 1.33.1 AND CVS Head
All cases the same result.
On Thu, 2007-05-31 at 15:52 +0400, Alexander Nasonov wrote:
In HEAD or 1.34? which compiler?

Preston A. Elder wrote:
Not entirely true ;)
Its consistent with iostream, but not DEFAULT iostream - only iostream with increased precision. One can accept the same behavior as iostream as not a bug only when they don't have to manipulate iostream (ie. increase precision) to reveal said bug.
lexical_cast should not lose digits, for example lexical_cast<string>(1.0000001) == 1.0000001; although cout << 1.0000001; prints 1. It's more important use case. -- Alexander Nasonov http://nasonov.blogspot.com The sick do not ask if the hand that smoothes their pillow is pure, nor the dying care if the lips that touch their brow have known the kiss of sin. -- Oscar Wilde -- This quote is generated by: /usr/pkg/bin/curl -L http://tinyurl.com/veusy \ | sed -e 's/^document\.write(.//' -e 's/.);$/ --/' \ -e 's/<[^>]*>//g' -e 's/^More quotes from //' \ | fmt | tee ~/.signature-quote
participants (3)
-
Alexander Nasonov
-
Jonathan Franklin
-
Preston A. Elder