4

For the following piece of code

std::cout<<boost::lexical_cast<std::string>(2.34)<<std::endl

i get the following output:

2.3399999999999999

Whereas if i do

 double d = 2.34;
 std::stringstream ss;
 ss<<d;
 std::string s = ss.str();
 cout<<s<<endl;

i get the following output:

 2.34

Why is this happening ? Obviously, I am looking for the latter's output representation, and not the former.

Thanks,

Aditya Sihag
  • 5,057
  • 4
  • 32
  • 43

1 Answers1

1

This has nothing to do with the boost::lexical_cast, but it comes along with double's internal representation:

See this answer also: C++ internal representation of double/float

Community
  • 1
  • 1
sebm
  • 11
  • 1
  • That was my initial instinct, too, but only `double`s are explicitly used here (no `float`s). See http://ideone.com/iVw2e for a demonstration. – johnsyweb Jul 26 '12 at 09:15