Problem
I have a vector containing cpp_dec_float_100
values from the boost::multiprecision
library (i.e., an std::vector<cpp_dec_float_100>
). My goal is to convert this to an std::vector<std::string>
(i.e., to convert cpp_dec_float_100
types to string
s).
What I've Tried
boost::exception::to_string()
/boost::lexical_cast<std::string>
: Boost provides some built-in tools for converting Boost types tostd::string
. Unfortunately, doing so oncpp_dec_float
comes at a cost in precision, which is not sufficient for my purposes. For example,1545596569.76676355
becomes1.5456e+09
. If I were, for example, to write this to a file usingostream
, I'd retain full precision.- The
std::stringstream
approach: Boost's own documentation says:
For more involved conversions, such as where precision or formatting need tighter control than is offered by the default behavior of lexical_cast, the conventional std::stringstream approach is recommended.
I.e., this:
// std::vector<cpp_dec_float_100> my_vector
// contains my data set
std::ostringstream ss;
std::vector<std::string> my_vector_of_string;
for(int i = 0; i < my_vector.size()-1; ++i){
ss << setprecision(n) << my_vector[i];
my_vector_of_string.push_back(ss.str());
}
This works (and, not exactly surprisingly, gives the same precision I get with writing to a file). However (again, not exactly surprisingly), this is incredibly slow (in fact, impractically so my purposes) slow. My data set includes on the order of tens to hundreds of millions of elements.
Is there a more efficient method for converting a cpp_dec_float_100
to std::string
? My concern is purely the time taken to do so.
Please, let me know if there are any questions or if I've left anything important out. I'm not terribly familiar with Boost or c++
.
Aucun commentaire:
Enregistrer un commentaire