升压序列化和双打 [英] boost serialization and doubles

查看:112
本文介绍了升压序列化和双打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图序列化类使用升压序列化库的字符串,并包含在我的班级有几个双成员变量。

I am trying to serialize a class to a string using the boost serialization library and included in my class are several double member variables.

下面是code我使用序列化:

Below is the code I'm using to serialize:

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>

std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << mPoint;

下面是我的Point类中的方法serialiation:

Here is the serialiation method within my Point class:

friend class boost::serialization::access;

template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
    if (version > 0)
    {
        ar & mLatitude;
        ar & mLongitude;
    }
}

当我序列化到一个字符串,升压没有出现来处理双字符串转换为我所期望的(似乎有四舍五入问题)。研究了一下,它看起来像其他人报告了相同的行为。我也明白与将double转换为字符串,反之亦然,以及如何这可能导致该问题相关的precision相关的问题。

When I serialize to a string, boost doesn't appear to handle the double to string conversion as I would expect (there appear to be rounding issues). Researching a bit it looks like others have reported the same behavior. I also understand the precision related issues associated with converting a double to a string and vice versa and how this could cause the issue.

什么是奇怪的,我不明白的是,虽然这不会出现,当我使用的是stringstream的本身和调整的双重到流也不当我使用升压转换器的功能的lexical_cast从stringstream.str转换发生()返回一个双。之前发现提升有其自己的序列化/反序列化类,我一直在使用字符串流和lexical_cast的调用实际上写我自己和它的工作W / O问题。我真的希望我没有放弃序列化库,并回到了我面前。但愿有只是一些设置/特征/等。我错过了。

What is strange and I don't understand though is this doesn't appear to happen when I'm using a stringstream itself and redirecting the double to the stream nor when I use boost's lexical_cast function to convert from a stringstream.str() back to a double. Before discovering boost had its own serialization/deserialization classes, I had actually written my own using stringstream and lexical_cast calls and it worked w/o issue. I'm really hoping I don't have to abandon the serialization library and go back to what I had before. Hopefully there is just some setting/trait/etc. I'm missing.

推荐答案

您可以尝试迫使你的流用科学的格式浮点连载到它之前:

You could try forcing your stream to use scientific format for floating point before serialising to it:

ss << std::scientific;

它看起来像升压库正确设置precision,但似乎没有设置的格式。或者,您可以我觉得派生并重写逻辑保存和/或装载浮点数没有扔掉库的其余部分 - 开始的这里

它也像有在建工作支持无穷大等。

It also looks like there is work in progress on supporting infinities etc.

这篇关于升压序列化和双打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆