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));
43std::pair<std::string_view, std::string_view>
SplitAt(std::string_view str,
char splitter)
45 auto pos = str.find(splitter);
46 if (pos == std::string_view::npos)
48 return {str.substr(0, pos), str.substr(pos + 1)};
65std::string
Round(
double value,
double error,
unsigned int cutoff, std::string_view delim)
68 return std::to_string(
value);
71 int error_exponent_base10_rounded = std::floor(std::log10(error));
72 const auto error_magnitude_base10 = std::pow(10., error_exponent_base10_rounded);
73 const auto error_first_digit =
static_cast<unsigned int>(error / error_magnitude_base10);
74 assert(error_first_digit > 0 && error_first_digit < 10);
75 if (error_first_digit <= cutoff) {
76 const double rescaled_error = error * std::pow(10., -1. * error_exponent_base10_rounded);
77 if (
static_cast<unsigned int>(std::round(rescaled_error * 10) / 10) <= cutoff)
78 error_exponent_base10_rounded--;
79 }
else if (cutoff == 0 && error_first_digit == 9) {
80 const double rounded_rescaled_error = std::round(error * std::pow(10., -1. * error_exponent_base10_rounded));
81 const int rounded_rescaled_error_exponent_base10_rounded = std::floor(std::log10(rounded_rescaled_error));
82 const auto rounded_rescaled_error_magnitude_base10 =
83 std::pow(10., rounded_rescaled_error_exponent_base10_rounded);
84 const auto rounded_rescaled_error_first_digit =
85 static_cast<int>(rounded_rescaled_error / rounded_rescaled_error_magnitude_base10);
86 if (rounded_rescaled_error_first_digit == 1)
87 error_exponent_base10_rounded++;
89 const int factored_out_exponent_base10 = error <= 1
e-3 ?
static_cast<int>(std::floor(std::log10(error) / 3)) * 3
90 :
static_cast<int>(std::log10(error) / 3) * 3;
93 result.setf(std::ios::fixed);
94 if (error_exponent_base10_rounded - factored_out_exponent_base10 < 0) {
95 result.precision(-error_exponent_base10_rounded + factored_out_exponent_base10);
99 if (factored_out_exponent_base10 != 0)
101 result << std::round(
value * std::pow(10., -error_exponent_base10_rounded)) /
102 std::pow(10., -error_exponent_base10_rounded + factored_out_exponent_base10);
104 result << std::round(error * std::pow(10., -error_exponent_base10_rounded)) /
105 std::pow(10., -error_exponent_base10_rounded + factored_out_exponent_base10);
106 if (factored_out_exponent_base10 != 0)
107 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.
std::pair< std::string_view, std::string_view > SplitAt(std::string_view str, char splitter)
Given a string str, returns a pair of string views into it: the first containing the substring preced...