diff options
author | Thomas Habets <thomas@habets.se> | 2020-04-03 15:24:49 +0100 |
---|---|---|
committer | Marcus Müller <marcus@hostalia.de> | 2020-04-11 02:39:54 +0200 |
commit | 3bab5f023ab406b9a4521e7c3be7fd4bd33135f7 (patch) | |
tree | 4b1e21191d16653afb7c1ed233d9c6ef8bf170f9 /gnuradio-runtime/lib | |
parent | 3cb8a09e065f39e2b6db444d62f756dc03bf332d (diff) |
qtgui: Remove boost::lexical_cast for printing
boost::lexical_cast seems not not be consistent given different
inputs. E.g. std::complex is stringified like iostream, but float and
double avoid scientific notation… mostly. See below for comparisons
between before and after (`printf` is what this commit switches to).
I don't think the differences matter.
```
int main() {
constexpr float low_float = 200000000.2;
constexpr float high_float = 20000000000.2;
constexpr float low_double = 200000000.2;
constexpr float mid_double = 20000000000.2;
constexpr float high_double = 200000000000000000.2;
const std::complex<float> c(1.3,low_float);
const std::complex<double> d(1.3,low_double);
const std::complex<double> d2(1.3,high_double);
const std::vector<float> fs = {low_float, high_float};
const std::vector<double> ds = {low_double, mid_double, high_double};
std::cout << "# complex\n";
std::cout << "iostream: " << c << std::endl;
std::cout << "lexical_cast: " << boost::lexical_cast<std::string>(c) << std::endl;
std::cout << std::endl;
for (int i = 0; i < fs.size(); i++) {
std::cout << "# float " << i << std::endl;
std::cout << "iostream: " << fs[i] << std::endl;
std::cout << "to_string: " << std::to_string(fs[i]) << std::endl;
printf("printf %.f\n", std::numeric_limits<float>::digits10, fs[i]);
std::cout << "lexical: " << boost::lexical_cast<std::string, float>(fs[i]) << std::endl;
std::cout << std::endl;
}
for (int i = 0; i < ds.size(); i++) {
std::cout << "# double " << i << std::endl;
std::cout << "iostream: " << ds[i] << std::endl;
std::cout << "to_string: " << std::to_string(ds[i]) << std::endl;
printf("printf %.*g\n", std::numeric_limits<double>::digits10, ds[i]);
std::cout << "lexical: " << boost::lexical_cast<std::string, double>(ds[i]) << std::endl;
std::cout << std::endl;
}
}
```
Output (Debian amd64):
```
iostream: (1.3,2e+08)
lexical_cast: (1.3,2e+08)
iostream: 2e+08
to_string: 200000000.000000
printf 200000000
lexical: 200000000
iostream: 2e+10
to_string: 20000000000.000000
printf 20000000000
lexical: 2e+10
iostream: 2e+08
to_string: 200000000.000000
printf 200000000
lexical: 200000000
iostream: 2e+10
to_string: 20000000000.000000
printf 20000000000
lexical: 20000000000
iostream: 2e+17
to_string: 199999996861349888.000000
printf 1.9999999686135e+17
lexical: 1.9999999686134989e+17
```
Diffstat (limited to 'gnuradio-runtime/lib')
-rw-r--r-- | gnuradio-runtime/lib/pmt/pmt_unv.cc | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/gnuradio-runtime/lib/pmt/pmt_unv.cc b/gnuradio-runtime/lib/pmt/pmt_unv.cc index baf6ec5d3c..23135dd4fd 100644 --- a/gnuradio-runtime/lib/pmt/pmt_unv.cc +++ b/gnuradio-runtime/lib/pmt/pmt_unv.cc @@ -17,7 +17,8 @@ #include "pmt_int.h" #include "pmt_unv_int.h" #include <pmt/pmt.h> -#include <boost/lexical_cast.hpp> +#include <sstream> +#include <string> #include <vector> //////////////////////////////////////////////////////////////////////////// // pmt_u8vector @@ -137,7 +138,7 @@ uint8_t* u8vector_writable_elements(pmt_t vector, size_t& len) const std::string pmt_u8vector::string_ref(size_t k) const { - return boost::lexical_cast<std::string, uint8_t>(ref(k)); + return std::to_string(ref(k)); } } /* namespace pmt */ @@ -259,7 +260,7 @@ int8_t* s8vector_writable_elements(pmt_t vector, size_t& len) const std::string pmt_s8vector::string_ref(size_t k) const { - return boost::lexical_cast<std::string, int8_t>(ref(k)); + return std::to_string(ref(k)); } } /* namespace pmt */ @@ -1143,7 +1144,9 @@ float* f32vector_writable_elements(pmt_t vector, size_t& len) const std::string pmt_f32vector::string_ref(size_t k) const { - return boost::lexical_cast<std::string, float>(ref(k)); + char buf[128]; + snprintf(buf, sizeof(buf), "%.*f", std::numeric_limits<float>::digits10, ref(k)); + return buf; } } /* namespace pmt */ @@ -1268,7 +1271,9 @@ double* f64vector_writable_elements(pmt_t vector, size_t& len) const std::string pmt_f64vector::string_ref(size_t k) const { - return boost::lexical_cast<std::string, double>(ref(k)); + char buf[128]; + snprintf(buf, sizeof(buf), "%.*f", std::numeric_limits<double>::digits10, ref(k)); + return buf; } } /* namespace pmt */ @@ -1396,7 +1401,9 @@ std::complex<float>* c32vector_writable_elements(pmt_t vector, size_t& len) const std::string pmt_c32vector::string_ref(size_t k) const { - return boost::lexical_cast<std::string, std::complex<float>>(ref(k)); + std::stringstream ss; + ss << ref(k); + return ss.str(); } } /* namespace pmt */ @@ -1524,7 +1531,9 @@ std::complex<double>* c64vector_writable_elements(pmt_t vector, size_t& len) const std::string pmt_c64vector::string_ref(size_t k) const { - return boost::lexical_cast<std::string, std::complex<double>>(ref(k)); + std::stringstream ss; + ss << ref(k); + return ss.str(); } } /* namespace pmt */ |