Accuracy is lost when using lexical_cast <float> (string)

When using boost :: lexical_cast (I am using boost version 1.58 on VS2013), I cannot get the exact value indicated in the string even if it is represented in float:

std::wstring t = L"91.25";
float r;
r = boost::lexical_cast<float>(t);

r - 91.249992 (0x42B67FFF) instead of 91.250000 (0x42b68000)

Previous versions of boost led the way. Is there any accuracy that I am lacking?

+4
source share
1 answer

It turns out that this is not associated with an increase. It seems the problem is with Visual Studio and only with VS2013.

#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
    float a;
    std::stringstream s;
    s.str("91.25");
    s >> a;
    std::wcout << std::setprecision(20) << a << std::endl;
    // displays "91.249992370605469" when compiled with 
    // VS2013 and "91.25" when compiled with VS2010 or VS2015
    return 0;
}
+2
source

All Articles