26std::vector<std::string>
Split(std::string_view str, std::string_view delims,
bool skipEmpty )
28 std::vector<std::string> out;
32 while ((end = str.find_first_of(delims, beg)) != std::string::npos) {
33 if (!skipEmpty || end > beg)
34 out.emplace_back(str.substr(beg, end - beg));
37 if (!skipEmpty || str.size() > beg)
38 out.emplace_back(str.substr(beg, str.size() - beg));
57std::string
Round(
double value,
double error,
unsigned int cutoff, std::string_view delim)
60 return std::to_string(value);
63 int error_exponent_base10_rounded = std::floor(std::log10(error));
64 const auto error_magnitude_base10 = std::pow(10., error_exponent_base10_rounded);
65 const auto error_first_digit =
static_cast<unsigned int>(error / error_magnitude_base10);
66 assert(error_first_digit > 0 && error_first_digit < 10);
67 if (error_first_digit <= cutoff) {
68 const double rescaled_error = error * std::pow(10., -1. * error_exponent_base10_rounded);
69 if (
static_cast<unsigned int>(std::round(rescaled_error * 10) / 10) <= cutoff)
70 error_exponent_base10_rounded--;
71 }
else if (cutoff == 0 && error_first_digit == 9) {
72 const double rounded_rescaled_error = std::round(error * std::pow(10., -1. * error_exponent_base10_rounded));
73 const int rounded_rescaled_error_exponent_base10_rounded = std::floor(std::log10(rounded_rescaled_error));
74 const auto rounded_rescaled_error_magnitude_base10 =
75 std::pow(10., rounded_rescaled_error_exponent_base10_rounded);
76 const auto rounded_rescaled_error_first_digit =
77 static_cast<int>(rounded_rescaled_error / rounded_rescaled_error_magnitude_base10);
78 if (rounded_rescaled_error_first_digit == 1)
79 error_exponent_base10_rounded++;
81 const int factored_out_exponent_base10 = error <= 1
e-3 ?
static_cast<int>(std::floor(std::log10(error) / 3)) * 3
82 :
static_cast<int>(std::log10(error) / 3) * 3;
84 std::stringstream result;
85 result.setf(std::ios::fixed);
86 if (error_exponent_base10_rounded - factored_out_exponent_base10 < 0) {
87 result.precision(-error_exponent_base10_rounded + factored_out_exponent_base10);
91 if (factored_out_exponent_base10 != 0)
93 result << std::round(value * std::pow(10., -error_exponent_base10_rounded)) /
94 std::pow(10., -error_exponent_base10_rounded + factored_out_exponent_base10);
96 result << std::round(error * std::pow(10., -error_exponent_base10_rounded)) /
97 std::pow(10., -error_exponent_base10_rounded + factored_out_exponent_base10);
98 if (factored_out_exponent_base10 != 0)
99 result <<
")*1e" << factored_out_exponent_base10;
std::string Round(double value, double error, unsigned int cutoff=1, std::string_view delim="#pm")
Convert (round) a value and its uncertainty to string using one or two significant digits of the erro...
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.