簡體   English   中英

使用lexical_cast時精度會丟失 <float> (串)

[英]Precision is lost when using lexical_cast<float>(string)

當使用boost :: lexical_cast(我在VS2013上使用boost版本1.58)時,我無法獲得字符串中指定的確切值,即使它在float中是可表示的:

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

r是91.249992(0x42B67FFF)而不是91.250000(0x42b68000)

boost的早期版本表現出預期的方式。 我缺少精確設置嗎?

事實證明這與提升無關。 這似乎是Visual Studio和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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM