首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用lexical_cast<float>(string)时精度丢失

使用lexical_cast<float>(string)时精度丢失
EN

Stack Overflow用户
提问于 2016-06-23 23:04:22
回答 1查看 247关注 0票数 4

当使用boost::lexical_cast (我在VS2013上使用boost版本1.58 )时,我无法获得字符串中指定的精确值,即使它可以在浮点型中表示:

代码语言:javascript
复制
std::wstring t = L"91.25";
float r;
r = boost::lexical_cast<float>(t);

R为91.249992 (0x42B67FFF),而不是91.250000 (0x42b68000)

先前版本的boost的行为符合预期。有没有我遗漏的精确度设置?

EN

回答 1

Stack Overflow用户

发布于 2016-08-03 01:12:54

事实证明,这与boost无关。这似乎是Visual Studio的问题,而且只有VS2013有问题。

代码语言:javascript
复制
#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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37995387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档